import '../../models/tenant.dart';

class TenantLocationData {
  const TenantLocationData({
    this.address,
    this.city,
    this.district,
    this.latitude,
    this.longitude,
  });

  final String? address;
  final String? city;
  final String? district;
  final double? latitude;
  final double? longitude;

  bool get hasCoordinates => latitude != null && longitude != null;
  bool get hasDetails =>
      (address ?? '').trim().isNotEmpty ||
      (city ?? '').trim().isNotEmpty ||
      (district ?? '').trim().isNotEmpty ||
      hasCoordinates;

  String get shortLabel {
    final parts = [
      if ((district ?? '').trim().isNotEmpty) district!.trim(),
      if ((city ?? '').trim().isNotEmpty) city!.trim(),
    ];
    if (parts.isNotEmpty) return parts.join(' / ');
    return (address ?? '').trim();
  }

  String get fullLabel {
    final parts = [
      if ((address ?? '').trim().isNotEmpty) address!.trim(),
      if ((district ?? '').trim().isNotEmpty) district!.trim(),
      if ((city ?? '').trim().isNotEmpty) city!.trim(),
    ];
    return parts.join(', ');
  }

  TenantLocationData copyWith({
    String? address,
    String? city,
    String? district,
    double? latitude,
    double? longitude,
    bool clearAddress = false,
  }) {
    return TenantLocationData(
      address: clearAddress ? null : (address ?? this.address),
      city: city ?? this.city,
      district: district ?? this.district,
      latitude: latitude ?? this.latitude,
      longitude: longitude ?? this.longitude,
    );
  }

  Map<String, dynamic> toTenantBody() => {
        'company_address': address,
        'city': city,
        'district': district,
        'latitude': latitude,
        'longitude': longitude,
      };

  static TenantLocationData fromTenant(Tenant tenant) => TenantLocationData(
        address: tenant.companyAddress,
        city: tenant.city,
        district: tenant.district,
        latitude: tenant.latitude,
        longitude: tenant.longitude,
      );
}
