import 'dart:convert';

import 'package:flutter/material.dart';

// To parse this JSON data, do
//
//     final userResponseModel = userResponseModelFromJson(jsonString);

class LoginResponseModel {
  final String token;
  final String expires;
  final User user;
  LoginResponseModel({
    required this.token,
    required this.expires,
    required this.user,
  });

  Map<String, dynamic> toMap() {
    return {
      'token': token,
      'expires': expires,
      'user': user.toMap(),
    };
  }

  factory LoginResponseModel.fromMap(Map<String, dynamic> map) {
    return LoginResponseModel(
      token: map['data']['token'] ?? '',
      expires: map['data']['expires'] ?? '',
      user: User.fromMap(map['data']['user']),
    );
  }

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

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

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 String lastLogin;
  final int isSuperuser;
  final String createdAt;
  final String 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,
      'isSuperuser': isSuperuser,
      'createdAt': createdAt,
      'updatedAt': updatedAt,
      'phone_list': phoneList,
    };
  }

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

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

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