import 'package:tugas_akhir_supabase/domain/entities/field.dart';

class FieldModel {
  final String id;
  final String userId;
  final String name;
  final int plotCount;
  final String? region;
  final String? location;
  final double? latitude;
  final double? longitude;
  final double? areaSize;
  final String? areaUnit;
  final String? ownershipType;
  final String? ownerName;
  final Map<String, dynamic>? regionSpecificData;
  final DateTime createdAt;
  final DateTime updatedAt;

  FieldModel({
    required this.id,
    required this.userId,
    required this.name,
    required this.plotCount,
    this.region,
    this.location,
    this.latitude,
    this.longitude,
    this.areaSize,
    this.areaUnit,
    this.ownershipType,
    this.ownerName,
    this.regionSpecificData,
    required this.createdAt,
    required this.updatedAt,
  });

  Field toEntity() {
    return Field(
      id: id,
      userId: userId,
      name: name,
      plotCount: plotCount,
      region: region,
      location: location,
      latitude: latitude,
      longitude: longitude,
      areaSize: areaSize,
      areaUnit: areaUnit,
      ownershipType: ownershipType,
      ownerName: ownerName,
      regionSpecificData: regionSpecificData,
      createdAt: createdAt,
      updatedAt: updatedAt,
    );
  }

  factory FieldModel.fromJson(Map<String, dynamic> json) {
    return FieldModel(
      id: json['id'] as String,
      name: json['name'] as String,
      userId: json['user_id'] as String,
      plotCount:
          json['plot_count'] is int
              ? json['plot_count']
              : int.tryParse(json['plot_count']?.toString() ?? '1') ?? 1,
      region: json['region'] as String?,
      location: json['location'] as String?,
      latitude:
          json['latitude'] is double
              ? json['latitude']
              : double.tryParse(json['latitude']?.toString() ?? '0'),
      longitude:
          json['longitude'] is double
              ? json['longitude']
              : double.tryParse(json['longitude']?.toString() ?? '0'),
      areaSize:
          json['area_size'] is double
              ? json['area_size']
              : double.tryParse(json['area_size']?.toString() ?? '0'),
      areaUnit: json['area_unit'] as String? ?? 'm²',
      ownershipType: json['ownership_type'] as String? ?? 'Milik Sendiri',
      ownerName: json['owner_name'] as String?,
      regionSpecificData:
          json['region_specific_data'] is Map
              ? Map<String, dynamic>.from(json['region_specific_data'])
              : null,
      createdAt:
          json['created_at'] != null
              ? json['created_at'] is DateTime
                  ? json['created_at']
                  : DateTime.parse(json['created_at'])
              : DateTime.now(),
      updatedAt:
          json['updated_at'] != null
              ? json['updated_at'] is DateTime
                  ? json['updated_at']
                  : DateTime.parse(json['updated_at'])
              : DateTime.now(),
    );
  }

  Map<String, dynamic> toJson() {
    return {
      'id': id,
      'name': name,
      'user_id': userId,
      'plot_count': plotCount,
      'region': region,
      'location': location,
      'latitude': latitude,
      'longitude': longitude,
      'area_size': areaSize,
      'area_unit': areaUnit,
      'ownership_type': ownershipType,
      'owner_name': ownerName,
      'region_specific_data': regionSpecificData,
      'created_at': createdAt.toIso8601String(),
      'updated_at': updatedAt.toIso8601String(),
    };
  }

  factory FieldModel.fromEntity(Field entity) {
    return FieldModel(
      id: entity.id,
      name: entity.name,
      userId: entity.userId ?? '',
      plotCount: entity.plotCount,
      region: entity.region,
      location: entity.location,
      latitude: entity.latitude,
      longitude: entity.longitude,
      areaSize: entity.areaSize,
      areaUnit: entity.areaUnit,
      ownershipType: entity.ownershipType,
      ownerName: entity.ownerName,
      regionSpecificData: entity.regionSpecificData,
      createdAt: entity.createdAt ?? DateTime.now(),
      updatedAt: entity.updatedAt ?? DateTime.now(),
    );
  }
}
