import '../../directory/domain/employee.dart';

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

  final String id;
  final String name;
  final String slug;
  final String type;
  final String? parentId;
  final int memberCount;
  final int totalMemberCount;

  factory TenantSummary.fromJson(Map<String, dynamic> json) {
    return TenantSummary(
      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?,
      memberCount: json['memberCount'] as int? ?? 0,
      totalMemberCount: json['totalMemberCount'] as int? ?? 0,
    );
  }

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

class TenantListResponse {
  const TenantListResponse({required this.items, required this.generatedAt});

  final List<TenantSummary> items;
  final DateTime generatedAt;

  factory TenantListResponse.fromJson(Map<String, dynamic> json) {
    return TenantListResponse(
      items: (json['items'] as List<dynamic>? ?? [])
          .map((item) => TenantSummary.fromJson(item as Map<String, dynamic>))
          .toList(),
      generatedAt: DateTime.parse(json['generatedAt'] as String),
    );
  }

  Map<String, dynamic> toJson() {
    return {
      'items': items.map((tenant) => tenant.toJson()).toList(),
      'generatedAt': generatedAt.toUtc().toIso8601String(),
    };
  }
}

class OrgChartCacheInfo {
  const OrgChartCacheInfo({
    required this.source,
    required this.hit,
    this.ttlSeconds,
  });

  final String source;
  final bool hit;
  final int? ttlSeconds;

  factory OrgChartCacheInfo.fromJson(Map<String, dynamic> json) {
    return OrgChartCacheInfo(
      source: json['source'] as String? ?? '',
      hit: json['hit'] as bool? ?? false,
      ttlSeconds: json['ttlSeconds'] as int?,
    );
  }

  Map<String, dynamic> toJson() {
    return {'source': source, 'hit': hit, 'ttlSeconds': ttlSeconds};
  }
}

class OrgChartSnapshot {
  const OrgChartSnapshot({
    required this.tenants,
    required this.employees,
    required this.generatedAt,
    this.cache,
  });

  final List<TenantSummary> tenants;
  final List<Employee> employees;
  final DateTime generatedAt;
  final OrgChartCacheInfo? cache;

  factory OrgChartSnapshot.fromJson(Map<String, dynamic> json) {
    return OrgChartSnapshot(
      tenants: (json['tenants'] as List<dynamic>? ?? [])
          .map((item) => TenantSummary.fromJson(item as Map<String, dynamic>))
          .toList(),
      employees: (json['employees'] as List<dynamic>? ?? [])
          .map((item) => Employee.fromJson(item as Map<String, dynamic>))
          .toList(),
      generatedAt: DateTime.parse(json['generatedAt'] as String),
      cache: json['cache'] == null
          ? null
          : OrgChartCacheInfo.fromJson(json['cache'] as Map<String, dynamic>),
    );
  }

  Map<String, dynamic> toJson() {
    return {
      'tenants': tenants.map((tenant) => tenant.toJson()).toList(),
      'employees': employees.map((employee) => employee.toJson()).toList(),
      'generatedAt': generatedAt.toUtc().toIso8601String(),
      'cache': cache?.toJson(),
    };
  }
}
