import 'package:syncrow_web/pages/auth/model/project_model.dart';
import 'package:syncrow_web/pages/auth/model/token.dart';

class UserModel {
  static String userUuidKey = 'userUuid';
  final String? uuid;
  final String? email;
  final String? firstName;
  final String? lastName;
  final String? photoUrl;
  final String? phoneNumber;
  final bool? isEmailVerified;
  final bool? isAgreementAccepted;
  final bool? hasAcceptedWebAgreement;
  final DateTime? webAgreementAcceptedAt;
  final UserRole? role;
  final Project? project;

  UserModel({
    required this.uuid,
    required this.email,
    required this.firstName,
    required this.lastName,
    required this.photoUrl,
    required this.phoneNumber,
    required this.isEmailVerified,
    required this.isAgreementAccepted,
    required this.hasAcceptedWebAgreement,
    required this.webAgreementAcceptedAt,
    required this.role,
    required this.project,
  });

  factory UserModel.fromJson(Map<String, dynamic> json) {
    return UserModel(
      uuid: json['id'],
      email: json['email'],
      firstName: json['firstName'],
      lastName: json['lastName'],
      photoUrl: json['photoUrl'],
      phoneNumber: json['phoneNumber'],
      isEmailVerified: json['isEmailVerified'],
      isAgreementAccepted: json['isAgreementAccepted'],
      hasAcceptedWebAgreement: json['hasAcceptedWebAgreement'],
      webAgreementAcceptedAt: json['webAgreementAcceptedAt'] != null
          ? DateTime.parse(json['webAgreementAcceptedAt'])
          : null,
      role: json['role'] != null ? UserRole.fromJson(json['role']) : null,
      project:
          json['project'] != null ? Project.fromJson(json['project']) : null,
    );
  }

  //uuid to json

  //from token
  factory UserModel.fromToken(Token token) {
    Map<String, dynamic> tempJson = Token.decodeToken(token.accessToken);

    return UserModel(
      hasAcceptedWebAgreement: null,
      role: null,
      webAgreementAcceptedAt: null,
      uuid: tempJson['uuid'].toString(),
      email: tempJson['email'],
      firstName: null,
      lastName: null,
      photoUrl: null,
      phoneNumber: null,
      isEmailVerified: null,
      isAgreementAccepted: null,
      project: null
    );
  }

  Map<String, dynamic> toJson() {
    return {
      'id': uuid,
      'email': email,
      'firstName': firstName,
      'lastName': lastName,
      'photoUrl': photoUrl,
      'phoneNumber': phoneNumber,
      'isEmailVerified': isEmailVerified,
      'isAgreementAccepted': isAgreementAccepted,
    };
  }
}

class UserRole {
  final String uuid;
  final DateTime createdAt;
  final DateTime updatedAt;
  final String type;

  UserRole({
    required this.uuid,
    required this.createdAt,
    required this.updatedAt,
    required this.type,
  });

  factory UserRole.fromJson(Map<String, dynamic> json) {
    return UserRole(
      uuid: json['uuid'],
      createdAt: DateTime.parse(json['createdAt']),
      updatedAt: DateTime.parse(json['updatedAt']),
      type: json['type'],
    );
  }
}
