import 'package:flutter/material.dart';

/// Types of administrative divisions in Indonesia
enum DivisionType {
  province,
  regency,
  district,
  village
}

/// Model for representing administrative divisions in Indonesia
class AdministrativeDivision {
  final String id;
  final String name;
  final DivisionType type;
  final String? parentId;
  final String? code;
  final Map<String, dynamic>? populationData;

  AdministrativeDivision({
    required this.id,
    required this.name,
    required this.type,
    this.parentId,
    this.code,
    this.populationData,
  });

  /// Creates a copy with the given fields replaced with the new values
  AdministrativeDivision copyWith({
    String? id,
    String? name,
    DivisionType? type,
    String? parentId,
    String? code,
    Map<String, dynamic>? populationData,
  }) {
    return AdministrativeDivision(
      id: id ?? this.id,
      name: name ?? this.name,
      type: type ?? this.type,
      parentId: parentId ?? this.parentId,
      code: code ?? this.code,
      populationData: populationData ?? this.populationData,
    );
  }

  /// Factory method to create from provinsi.json structure
  factory AdministrativeDivision.fromProvinceJson(Map<String, dynamic> json) {
    return AdministrativeDivision(
      id: json['kode_wilayah'],
      name: json['nama_wilayah'],
      type: DivisionType.province,
      code: json['kode_wilayah'],
      populationData: json['populasi'],
    );
  }

  /// Factory method to create from kabupaten_kota.json structure
  factory AdministrativeDivision.fromRegencyJson(Map<String, dynamic> json) {
    return AdministrativeDivision(
      id: json['kode_wilayah'],
      name: json['nama_wilayah'],
      type: DivisionType.regency,
      parentId: json['kode_provinsi'],
      code: json['kode_wilayah'],
      populationData: json['populasi'],
    );
  }

  /// Factory method to create from kecamatan.json structure
  factory AdministrativeDivision.fromDistrictJson(Map<String, dynamic> json) {
    return AdministrativeDivision(
      id: json['kode_wilayah'],
      name: json['nama_wilayah'],
      type: DivisionType.district,
      parentId: json['kode_kabupaten'],
      code: json['kode_wilayah'],
      populationData: json['populasi'],
    );
  }

  /// Factory method to create from desa_kelurahan.json structure
  factory AdministrativeDivision.fromVillageJson(Map<String, dynamic> json) {
    return AdministrativeDivision(
      id: json['kode_wilayah'],
      name: json['nama_wilayah'],
      type: DivisionType.village,
      parentId: json['kode_kecamatan'],
      code: json['kode_wilayah'],
      populationData: json['populasi'],
    );
  }
}
