import 'package:sigap/src/features/personalization/data/models/models/profile_model.dart';
import 'package:sigap/src/features/personalization/data/models/models/roles_model.dart';

class UserModel {
  final String id;
  final String rolesId;
  final String email;
  final String? phone;
  final String? encryptedPassword;
  final DateTime? invitedAt;
  final DateTime? confirmedAt;
  final DateTime? emailConfirmedAt;
  final DateTime? recoverySentAt;
  final DateTime? lastSignInAt;
  final Map<String, dynamic>? appMetadata;
  final Map<String, dynamic>? userMetadata;
  final DateTime createdAt;
  final DateTime updatedAt;
  final DateTime? bannedUntil;
  final bool isAnonymous;
  final ProfileModel? profile;
  final RoleModel? role;

  UserModel({
    required this.id,
    required this.rolesId,
    required this.email,
    this.phone,
    this.encryptedPassword,
    this.invitedAt,
    this.confirmedAt,
    this.emailConfirmedAt,
    this.recoverySentAt,
    this.lastSignInAt,
    this.appMetadata,
    this.userMetadata,
    required this.createdAt,
    required this.updatedAt,
    this.bannedUntil,
    required this.isAnonymous,
    this.profile,
    this.role,
  });

  // Create a UserModel instance from a JSON object
  factory UserModel.fromJson(Map<String, dynamic> json) {
    return UserModel(
      id: json['id'] as String,
      rolesId: json['roles_id'] as String,
      email: json['email'] as String,
      phone: json['phone'] as String?,
      encryptedPassword: json['encrypted_password'] as String?,
      invitedAt:
          json['invited_at'] != null
              ? DateTime.parse(json['invited_at'] as String)
              : null,
      confirmedAt:
          json['confirmed_at'] != null
              ? DateTime.parse(json['confirmed_at'] as String)
              : null,
      emailConfirmedAt:
          json['email_confirmed_at'] != null
              ? DateTime.parse(json['email_confirmed_at'] as String)
              : null,
      recoverySentAt:
          json['recovery_sent_at'] != null
              ? DateTime.parse(json['recovery_sent_at'] as String)
              : null,
      lastSignInAt:
          json['last_sign_in_at'] != null
              ? DateTime.parse(json['last_sign_in_at'] as String)
              : null,
      appMetadata: json['app_metadata'] as Map<String, dynamic>?,
      userMetadata: json['user_metadata'] as Map<String, dynamic>?,
      createdAt: DateTime.parse(json['created_at'] as String),
      updatedAt: DateTime.parse(json['updated_at'] as String),
      bannedUntil:
          json['banned_until'] != null
              ? DateTime.parse(json['banned_until'] as String)
              : null,
      isAnonymous: json['is_anonymous'] as bool? ?? false,
      profile:
          json['profiles'] != null
              ? ProfileModel.fromJson(json['profiles'] as Map<String, dynamic>)
              : null,
      role:
          json['role'] != null
              ? RoleModel.fromJson(json['role'] as Map<String, dynamic>)
              : null,
    );
  }

  // Convert a UserModel instance to a JSON object
  Map<String, dynamic> toJson() {
    return {
      'id': id,
      'roles_id': rolesId,
      'email': email,
      'phone': phone,
      'encrypted_password': encryptedPassword,
      'invited_at': invitedAt?.toIso8601String(),
      'confirmed_at': confirmedAt?.toIso8601String(),
      'email_confirmed_at': emailConfirmedAt?.toIso8601String(),
      'recovery_sent_at': recoverySentAt?.toIso8601String(),
      'last_sign_in_at': lastSignInAt?.toIso8601String(),
      'app_metadata': appMetadata,
      'user_metadata': userMetadata,
      'created_at': createdAt.toIso8601String(),
      'updated_at': updatedAt.toIso8601String(),
      'banned_until': bannedUntil?.toIso8601String(),
      'is_anonymous': isAnonymous,
      // if (profile != null) 'profiles': profile!.toJson(),
      // if (role != null) 'role': role!.toJson(),
    };
  }

  // Create a copy of the UserModel with updated fields
  UserModel copyWith({
    String? id,
    String? rolesId,
    String? email,
    String? phone,
    String? encryptedPassword,
    DateTime? invitedAt,
    DateTime? confirmedAt,
    DateTime? emailConfirmedAt,
    DateTime? recoverySentAt,
    DateTime? lastSignInAt,
    Map<String, dynamic>? appMetadata,
    Map<String, dynamic>? userMetadata,
    DateTime? createdAt,
    DateTime? updatedAt,
    DateTime? bannedUntil,
    bool? isAnonymous,
    ProfileModel? profile,
    RoleModel? role,
  }) {
    return UserModel(
      id: id ?? this.id,
      rolesId: rolesId ?? this.rolesId,
      email: email ?? this.email,
      phone: phone ?? this.phone,
      encryptedPassword: encryptedPassword ?? this.encryptedPassword,
      invitedAt: invitedAt ?? this.invitedAt,
      confirmedAt: confirmedAt ?? this.confirmedAt,
      emailConfirmedAt: emailConfirmedAt ?? this.emailConfirmedAt,
      recoverySentAt: recoverySentAt ?? this.recoverySentAt,
      lastSignInAt: lastSignInAt ?? this.lastSignInAt,
      appMetadata: appMetadata ?? this.appMetadata,
      userMetadata: userMetadata ?? this.userMetadata,
      createdAt: createdAt ?? this.createdAt,
      updatedAt: updatedAt ?? this.updatedAt,
      bannedUntil: bannedUntil ?? this.bannedUntil,
      isAnonymous: isAnonymous ?? this.isAnonymous,
      profile: profile ?? this.profile,
      role: role ?? this.role,
    );
  }

  // Check if user is an officer based on metadata
  bool get isOfficer {
    if (userMetadata != null && userMetadata!.containsKey('is_officer')) {
      return userMetadata!['is_officer'] == true;
    }
    return false;
  }

  @override
  String toString() {
    return 'UserModel(id: $id, email: $email)';
  }
}
