import 'package:hive_ce/hive.dart';

import '../../domain/entities/user_entity.dart';

part 'user_model.g.dart';

@HiveType(typeId: 1)
class UserModel extends UserEntity {
  @HiveField(0)
  @override
  final int id;

  @HiveField(1)
  @override
  final String firstName;

  @HiveField(2)
  @override
  final String name;

  @HiveField(3)
  @override
  final String? plateNumber;

  @HiveField(4)
  @override
  final String email;

  @HiveField(5)
  @override
  final String phone;

  @HiveField(6)
  @override
  final bool isParent;

  @HiveField(7)
  @override
  final String fullName;

  @HiveField(8)
  @override
  final String fullNameEmail;

  @HiveField(9)
  @override
  final String? referralCode;

  @HiveField(10)
  @override
  final String? avatar;

  @HiveField(11)
  @override
  final int departmentId;

  @HiveField(12)
  @override
  final bool isWareHouseUser;

  @HiveField(13)
  @override
  final int? wareHouseId;

  @HiveField(14)
  @override
  final int roleId;

  const UserModel({
    required this.id,
    required this.firstName,
    required this.name,
    this.plateNumber,
    required this.email,
    required this.phone,
    this.isParent = false,
    required this.fullName,
    required this.fullNameEmail,
    this.referralCode,
    this.avatar,
    required this.departmentId,
    this.isWareHouseUser = false,
    this.wareHouseId,
    required this.roleId,
  }) : super(
          id: id,
          firstName: firstName,
          name: name,
          plateNumber: plateNumber,
          email: email,
          phone: phone,
          isParent: isParent,
          fullName: fullName,
          fullNameEmail: fullNameEmail,
          referralCode: referralCode,
          avatar: avatar,
          departmentId: departmentId,
          isWareHouseUser: isWareHouseUser,
          wareHouseId: wareHouseId,
          roleId: roleId,
        );

  factory UserModel.fromJson(Map<String, dynamic> json) {
    return UserModel(
      id: json['Id'] ?? 0,
      firstName: json['FirstName'] ?? '',
      name: json['Name'] ?? '',
      plateNumber: json['PlateNumber'],
      email: json['Email'] ?? '',
      phone: json['Phone'] ?? '',
      isParent: json['IsParent'] ?? false,
      fullName: json['FullName'] ?? '',
      fullNameEmail: json['FullNameEmail'] ?? '',
      referralCode: json['ReferralCode'],
      avatar: json['Avatar'],
      departmentId: json['DepartmentId'] ?? 0,
      isWareHouseUser: json['IsWareHouseUser'] ?? false,
      wareHouseId: json['WareHouseId'],
      roleId: json['RoleId'] ?? 0,
    );
  }

  Map<String, dynamic> toJson() {
    return {
      'Id': id,
      'FirstName': firstName,
      'Name': name,
      'PlateNumber': plateNumber,
      'Email': email,
      'Phone': phone,
      'IsParent': isParent,
      'FullName': fullName,
      'FullNameEmail': fullNameEmail,
      'ReferralCode': referralCode,
      'Avatar': avatar,
      'DepartmentId': departmentId,
      'IsWareHouseUser': isWareHouseUser,
      'WareHouseId': wareHouseId,
      'RoleId': roleId,
    };
  }

  UserEntity toEntity() {
    return UserEntity(
      id: id,
      firstName: firstName,
      name: name,
      plateNumber: plateNumber,
      email: email,
      phone: phone,
      isParent: isParent,
      fullName: fullName,
      fullNameEmail: fullNameEmail,
      referralCode: referralCode,
      avatar: avatar,
      departmentId: departmentId,
      isWareHouseUser: isWareHouseUser,
      wareHouseId: wareHouseId,
      roleId: roleId,
    );
  }
}

