// To parse this JSON data, do
//
//     final registerResponseModel = registerResponseModelFromJson(jsonString);

import 'dart:convert';

import 'package:meta/meta.dart';

RegisterResponseModel registerResponseModelFromJson(String str) =>
    RegisterResponseModel.fromJson(json.decode(str));

String registerResponseModelToJson(RegisterResponseModel data) =>
    json.encode(data.toJson());

class RegisterResponseModel {
  RegisterResponseModel({
    required this.token,
    required this.expires,
    required this.user,
  });

  final String token;
  final DateTime expires;
  final User user;

  factory RegisterResponseModel.fromJson(Map<String, dynamic> json) =>
      RegisterResponseModel(
        token: json['data']["token"],
        expires: DateTime.parse(json['data']["expires"]),
        user: User.fromJson(json['data']["user"]),
      );

  Map<String, dynamic> toJson() => {
        "token": token,
        "expires": expires.toIso8601String(),
        "user": user.toJson(),
      };
}

class User {
  final int id;
  final String email;
  final String name;
  final String lastName;
  final String middleName;
  final String phone;
  final String phoneShort;
  final bool isActivated;
  final DateTime lastLogin;
  final int isSuperuser;
  final DateTime createdAt;
  final DateTime updatedAt;
  final List<String> phoneList;
  User({
    required this.id,
    required this.email,
    required this.name,
    required this.lastName,
    required this.middleName,
    required this.phone,
    required this.phoneShort,
    required this.isActivated,
    required this.lastLogin,
    required this.isSuperuser,
    required this.createdAt,
    required this.updatedAt,
    required this.phoneList,
  });

  Map<String, dynamic> toMap() {
    return {
      'id': id,
      'email': email,
      'name': name,
      'lastName': lastName,
      'middleName': middleName,
      'phone': phone,
      'phoneShort': phoneShort,
      'isActivated': isActivated,
      'lastLogin': lastLogin.millisecondsSinceEpoch,
      'isSuperuser': isSuperuser,
      'createdAt': createdAt.millisecondsSinceEpoch,
      'updatedAt': updatedAt.millisecondsSinceEpoch,
      'phone_list': phoneList,
    };
  }

  factory User.fromMap(Map<String, dynamic> map) {
    return User(
      id: map['id']?.toInt() ?? 0,
      email: map['email'] ?? '',
      name: map['name'] ?? '',
      lastName: map['lastName'] ?? '',
      middleName: map['middleName'] ?? '',
      phone: map['phone'] ?? '',
      phoneShort: map['phoneShort'] ?? '',
      isActivated: map['isActivated'] ?? false,
      lastLogin: DateTime.fromMillisecondsSinceEpoch(map['lastLogin']),
      isSuperuser: map['isSuperuser']?.toInt() ?? 0,
      createdAt: DateTime.fromMillisecondsSinceEpoch(map['createdAt']),
      updatedAt: DateTime.fromMillisecondsSinceEpoch(map['updatedAt']),
      phoneList: List<String>.from(map['phone_list']),
    );
  }

  String toJson() => json.encode(toMap());

  factory User.fromJson(String source) => User.fromMap(json.decode(source));
}
