import '../config/api_config.dart';

class Destination {
  final int id;
  final String nama;
  final String deskripsi;
  final String lokasi;
  final double rating;
  final String kategori;
  final String gambar;
  final String modelPath;
  final String deskripsiLengkap;
  final String ketinggian;
  final String tiketParkir;
  final String jamOperasional;
  final bool isVideo;
  final String videoPath;

  const Destination({
    required this.id,
    required this.nama,
    required this.deskripsi,
    required this.lokasi,
    required this.rating,
    required this.kategori,
    required this.gambar,
    required this.modelPath,
    required this.deskripsiLengkap,
    required this.ketinggian,
    required this.tiketParkir,
    required this.jamOperasional,
    required this.isVideo,
    required this.videoPath,
  });

  factory Destination.fromJson(Map<String, dynamic> json) {
    return Destination(
      id: _parseInt(json['id']),
      nama: (json['nama'] ?? '').toString(),
      deskripsi: (json['deskripsi'] ?? '').toString(),
      lokasi: _normalizeLokasi(
        (json['lokasi'] ?? '').toString(),
      ),
      rating: _parseDouble(json['rating']),
      kategori: (json['kategori'] ?? 'Wisata').toString(),
      gambar: _resolveImageUrl(
        (json['image'] ??
                json['gambar'] ??
                json['image_url'] ??
                '')
            .toString(),
      ),
      modelPath: _resolveStorageUrl(
        (json['model_path'] ?? '').toString(),
      ),
      deskripsiLengkap:
          (json['overview'] ??
                  json['deskripsi_lengkap'] ??
                  json['deskripsi'] ??
                  '')
              .toString(),
      ketinggian:
          (json['ketinggian'] ?? '').toString(),
      tiketParkir:
          (json['tiket_parkir'] ?? '').toString(),
      jamOperasional:
          (json['jam_operasional'] ?? '').toString(),
      isVideo: json['is_video'] == 1 ||
          json['is_video'] == true,
      videoPath: _resolveStorageUrl(
        (json['video_path'] ?? '').toString(),
      ),
    );
  }

  static int _parseInt(dynamic value) {
    if (value == null) return 0;

    if (value is int) return value;

    return int.tryParse(value.toString()) ?? 0;
  }

  static double _parseDouble(dynamic value) {
    if (value == null) return 0.0;

    if (value is double) return value;

    if (value is int) return value.toDouble();

    return double.tryParse(value.toString()) ?? 0.0;
  }

  static String _normalizeLokasi(String lokasi) {
    final value = lokasi.trim();

    if (value.isEmpty || value.toLowerCase() == 'lokasi') {
      return 'Lumajang, Jawa Timur';
    }

    return value;
  }

  static String _resolveImageUrl(String path) {
    if (path.trim().isEmpty) {
      return '';
    }

    final cleaned = path.replaceAll('\\', '/');

    if (cleaned.startsWith('http')) {
      return cleaned;
    }

    if (cleaned.startsWith('storage/')) {
      return '${ApiConfig.storageUrl}${cleaned.replaceFirst('storage/', '')}';
    }

    return '${ApiConfig.storageUrl}$cleaned';
  }

  static String _resolveStorageUrl(String path) {
    if (path.trim().isEmpty) {
      return '';
    }

    final cleaned = path.replaceAll('\\', '/');

    if (cleaned.startsWith('http')) {
      return cleaned;
    }

    if (cleaned.startsWith('storage/')) {
      return '${ApiConfig.storageUrl}${cleaned.replaceFirst('storage/', '')}';
    }

    return '${ApiConfig.storageUrl}$cleaned';
  }
}