class Employee {
  const Employee({
    required this.id,
    required this.name,
    required this.phoneNumber,
    this.phoneDisplay,
    this.email,
    required this.tenantId,
    required this.tenantName,
    required this.tenantSlug,
    this.department,
    this.grade,
    this.position,
    this.jobTitle,
    this.status,
    this.profileImageUrl,
    this.sortOrder,
    this.isManager = false,
  });

  final String id;
  final String name;
  final String phoneNumber;
  final String? phoneDisplay;
  final String? email;
  final String tenantId;
  final String tenantName;
  final String tenantSlug;
  final String? department;
  final String? grade;
  final String? position;
  final String? jobTitle;
  final String? status;
  final String? profileImageUrl;
  final int? sortOrder;
  final bool isManager;

  factory Employee.fromJson(Map<String, dynamic> json) {
    return Employee(
      id: json['id'] as String? ?? '',
      name: json['name'] as String? ?? '',
      phoneNumber: json['phoneNumber'] as String? ?? '',
      phoneDisplay: json['phoneDisplay'] as String?,
      email: json['email'] as String?,
      tenantId: json['tenantId'] as String? ?? '',
      tenantName: json['tenantName'] as String? ?? '',
      tenantSlug: json['tenantSlug'] as String? ?? '',
      department: json['department'] as String?,
      grade: json['grade'] as String?,
      position: json['position'] as String?,
      jobTitle: json['jobTitle'] as String?,
      status: json['status'] as String?,
      profileImageUrl: json['profileImageUrl'] as String?,
      sortOrder: json['sortOrder'] as int?,
      isManager: json['isManager'] as bool? ?? false,
    );
  }

  Map<String, dynamic> toJson() {
    return {
      'id': id,
      'name': name,
      'phoneNumber': phoneNumber,
      'phoneDisplay': phoneDisplay,
      'email': email,
      'tenantId': tenantId,
      'tenantName': tenantName,
      'tenantSlug': tenantSlug,
      'department': department,
      'grade': grade,
      'position': position,
      'jobTitle': jobTitle,
      'status': status,
      'profileImageUrl': profileImageUrl,
      'sortOrder': sortOrder,
      'isManager': isManager,
    };
  }
}

class TenantRef {
  const TenantRef({
    required this.id,
    required this.name,
    required this.slug,
    required this.type,
    this.parentId,
  });

  final String id;
  final String name;
  final String slug;
  final String type;
  final String? parentId;

  factory TenantRef.fromJson(Map<String, dynamic> json) {
    return TenantRef(
      id: json['id'] as String? ?? '',
      name: json['name'] as String? ?? '',
      slug: json['slug'] as String? ?? '',
      type: json['type'] as String? ?? '',
      parentId: json['parentId'] as String?,
    );
  }

  Map<String, dynamic> toJson() {
    return {
      'id': id,
      'name': name,
      'slug': slug,
      'type': type,
      'parentId': parentId,
    };
  }
}

class EmployeeActions {
  const EmployeeActions({
    required this.call,
    required this.sms,
    required this.email,
  });

  final bool call;
  final bool sms;
  final bool email;

  factory EmployeeActions.fromJson(Map<String, dynamic> json) {
    return EmployeeActions(
      call: json['call'] as bool? ?? false,
      sms: json['sms'] as bool? ?? false,
      email: json['email'] as bool? ?? false,
    );
  }

  Map<String, dynamic> toJson() {
    return {'call': call, 'sms': sms, 'email': email};
  }
}

class EmployeeDetail extends Employee {
  const EmployeeDetail({
    required super.id,
    required super.name,
    required super.phoneNumber,
    super.phoneDisplay,
    super.email,
    required super.tenantId,
    required super.tenantName,
    required super.tenantSlug,
    super.department,
    super.grade,
    super.position,
    super.jobTitle,
    super.status,
    super.profileImageUrl,
    super.sortOrder,
    super.isManager,
    required this.joinedTenants,
    required this.actions,
  });

  final List<TenantRef> joinedTenants;
  final EmployeeActions actions;

  factory EmployeeDetail.fromJson(Map<String, dynamic> json) {
    return EmployeeDetail(
      id: json['id'] as String? ?? '',
      name: json['name'] as String? ?? '',
      phoneNumber: json['phoneNumber'] as String? ?? '',
      phoneDisplay: json['phoneDisplay'] as String?,
      email: json['email'] as String?,
      tenantId: json['tenantId'] as String? ?? '',
      tenantName: json['tenantName'] as String? ?? '',
      tenantSlug: json['tenantSlug'] as String? ?? '',
      department: json['department'] as String?,
      grade: json['grade'] as String?,
      position: json['position'] as String?,
      jobTitle: json['jobTitle'] as String?,
      status: json['status'] as String?,
      profileImageUrl: json['profileImageUrl'] as String?,
      sortOrder: json['sortOrder'] as int?,
      isManager: json['isManager'] as bool? ?? false,
      joinedTenants: (json['joinedTenants'] as List<dynamic>? ?? [])
          .map((item) => TenantRef.fromJson(item as Map<String, dynamic>))
          .toList(),
      actions: EmployeeActions.fromJson(
        json['actions'] as Map<String, dynamic>? ?? {},
      ),
    );
  }

  @override
  Map<String, dynamic> toJson() {
    return {
      ...super.toJson(),
      'joinedTenants': joinedTenants.map((tenant) => tenant.toJson()).toList(),
      'actions': actions.toJson(),
    };
  }
}

class EmployeeListResponse {
  const EmployeeListResponse({
    required this.items,
    required this.limit,
    required this.offset,
    required this.total,
    required this.nextCursor,
  });

  final List<Employee> items;
  final int limit;
  final int offset;
  final int total;
  final String nextCursor;

  factory EmployeeListResponse.fromJson(Map<String, dynamic> json) {
    return EmployeeListResponse(
      items: (json['items'] as List<dynamic>? ?? [])
          .map((item) => Employee.fromJson(item as Map<String, dynamic>))
          .toList(),
      limit: json['limit'] as int? ?? 0,
      offset: json['offset'] as int? ?? 0,
      total: json['total'] as int? ?? 0,
      nextCursor: json['nextCursor'] as String? ?? '',
    );
  }

  Map<String, dynamic> toJson() {
    return {
      'items': items.map((employee) => employee.toJson()).toList(),
      'limit': limit,
      'offset': offset,
      'total': total,
      'nextCursor': nextCursor,
    };
  }
}
