import 'package:sigap/src/features/daily-ops/data/models/models/units_model.dart';
import 'package:sigap/src/features/map/data/models/models/districts_model.dart';

class CityModel {
  final String id;
  final String name;
  final DateTime? createdAt;
  final DateTime? updatedAt;
  final List<DistrictModel>? districts;
  final List<UnitModel>? units;

  CityModel({
    required this.id,
    required this.name,
    this.createdAt,
    this.updatedAt,
    this.districts,
    this.units,
  });

  // Create a CityModel instance from a JSON object
  factory CityModel.fromJson(Map<String, dynamic> json) {
    return CityModel(
      id: json['id'] as String,
      name: json['name'] as String,
      createdAt:
          json['created_at'] != null
              ? DateTime.parse(json['created_at'])
              : null,
      updatedAt:
          json['updated_at'] != null
              ? DateTime.parse(json['updated_at'])
              : null,
      districts:
          json['districts'] != null
              ? (json['districts'] as List)
                  .map((e) => DistrictModel.fromJson(e as Map<String, dynamic>))
                  .toList()
              : null,
      units:
          json['units'] != null
              ? (json['units'] as List)
                  .map((e) => UnitModel.fromJson(e as Map<String, dynamic>))
                  .toList()
              : null,
    );
  }

  // Convert a CityModel instance to a JSON object
  Map<String, dynamic> toJson() {
    return {
      'id': id,
      'name': name,
      'created_at': createdAt?.toIso8601String(),
      'updated_at': updatedAt?.toIso8601String(),
    };
  }

  // Create a copy of the CityModel with updated fields
  CityModel copyWith({
    String? id,
    String? name,
    DateTime? createdAt,
    DateTime? updatedAt,
    List<DistrictModel>? districts,
    List<UnitModel>? units,
  }) {
    return CityModel(
      id: id ?? this.id,
      name: name ?? this.name,
      createdAt: createdAt ?? this.createdAt,
      updatedAt: updatedAt ?? this.updatedAt,
      districts: districts ?? this.districts,
      units: units ?? this.units,
    );
  }

  @override
  String toString() {
    return 'CityModel(id: $id, name: $name)';
  }
}
