import 'package:sigap/src/features/map/data/models/models/districts_model.dart';

class GeographicModel {
  final String id;
  final String districtId;
  final String? address;
  final double longitude;
  final double latitude;
  final double? landArea;
  final DateTime? createdAt;
  final DateTime? updatedAt;
  final String? description;
  final String? type;
  final int? year;
  final DistrictModel? district;

  GeographicModel({
    required this.id,
    required this.districtId,
    this.address,
    required this.longitude,
    required this.latitude,
    this.landArea,
    this.createdAt,
    this.updatedAt,
    this.description,
    this.type,
    this.year,
    this.district,
  });

  // Create a GeographicModel instance from a JSON object
  factory GeographicModel.fromJson(Map<String, dynamic> json) {
    return GeographicModel(
      id: json['id'] as String,
      districtId: json['district_id'] as String,
      address: json['address'] as String?,
      longitude: json['longitude'].toDouble(),
      latitude: json['latitude'].toDouble(),
      landArea: json['land_area']?.toDouble(),
      createdAt:
          json['created_at'] != null
              ? DateTime.parse(json['created_at'])
              : null,
      updatedAt:
          json['updated_at'] != null
              ? DateTime.parse(json['updated_at'])
              : null,
      description: json['description'] as String?,
      type: json['type'] as String?,
      year: json['year'] as int?,
      district:
          json['districts'] != null
              ? DistrictModel.fromJson(
                json['districts'] as Map<String, dynamic>,
              )
              : null,
    );
  }

  // Convert a GeographicModel instance to a JSON object
  Map<String, dynamic> toJson() {
    return {
      'id': id,
      'district_id': districtId,
      'address': address,
      'longitude': longitude,
      'latitude': latitude,
      'land_area': landArea,
      'created_at': createdAt?.toIso8601String(),
      'updated_at': updatedAt?.toIso8601String(),
      'description': description,
      'type': type,
      'year': year,
    };
  }

  // Create a copy of the GeographicModel with updated fields
  GeographicModel copyWith({
    String? id,
    String? districtId,
    String? address,
    double? longitude,
    double? latitude,
    double? landArea,
    DateTime? createdAt,
    DateTime? updatedAt,
    String? description,
    String? type,
    int? year,
    DistrictModel? district,
  }) {
    return GeographicModel(
      id: id ?? this.id,
      districtId: districtId ?? this.districtId,
      address: address ?? this.address,
      longitude: longitude ?? this.longitude,
      latitude: latitude ?? this.latitude,
      landArea: landArea ?? this.landArea,
      createdAt: createdAt ?? this.createdAt,
      updatedAt: updatedAt ?? this.updatedAt,
      description: description ?? this.description,
      type: type ?? this.type,
      year: year ?? this.year,
      district: district ?? this.district,
    );
  }

  @override
  String toString() {
    return 'GeographicModel(id: $id, districtId: $districtId, type: $type)';
  }
}
