<?php

namespace Database\Seeders;

use App\Models\CategoryFeature;
use App\Models\ListingCategory;
use App\Models\ListingFeature;
use Illuminate\Database\Seeder;

class ListingCategoryFeatureSeeder extends Seeder
{
    public function run(): void
    {
        $categories = [
            [
                'name' => 'Villa',
                'slug' => 'villa',
                'description' => 'Luxury villas, detached houses and private residences.',
                'image' => 'https://images.unsplash.com/photo-1600585154340-be6161a56a0c?auto=format&fit=crop&w=1200&q=80',
                'image_alt' => 'Luxury modern villa with pool',
                'sort_order' => 1,
                'is_featured' => true,
            ],
            [
                'name' => 'Land',
                'slug' => 'land',
                'description' => 'Lands, plots and development parcels.',
                'image' => 'https://images.unsplash.com/photo-1500530855697-b586d89ba3ee?auto=format&fit=crop&w=1200&q=80',
                'image_alt' => 'Open land and natural landscape',
                'sort_order' => 2,
                'is_featured' => true,
            ],
            [
                'name' => 'Apartment',
                'slug' => 'apartment',
                'description' => 'Apartments and flats.',
                'image' => 'https://images.unsplash.com/photo-1494526585095-c41746248156?auto=format&fit=crop&w=1200&q=80',
                'image_alt' => 'Modern apartment building exterior',
                'sort_order' => 3,
                'is_featured' => true,
            ],
            [
                'name' => 'Commercial Property',
                'slug' => 'commercial-property',
                'description' => 'Shops, commercial buildings and business properties.',
                'image' => 'https://images.unsplash.com/photo-1497366754035-f200968a6e72?auto=format&fit=crop&w=1200&q=80',
                'image_alt' => 'Modern commercial office interior',
                'sort_order' => 4,
                'is_featured' => true,
            ],
            [
                'name' => 'Office',
                'slug' => 'office',
                'description' => 'Office spaces and corporate properties.',
                'image' => 'https://images.unsplash.com/photo-1497366811353-6870744d04b2?auto=format&fit=crop&w=1200&q=80',
                'image_alt' => 'Corporate office workspace',
                'sort_order' => 5,
                'is_featured' => false,
            ],
            [
                'name' => 'Residence',
                'slug' => 'residence',
                'description' => 'Residence apartments and managed living spaces.',
                'image' => 'https://images.unsplash.com/photo-1600566753190-17f0baa2a6c3?auto=format&fit=crop&w=1200&q=80',
                'image_alt' => 'Luxury residence living area',
                'sort_order' => 6,
                'is_featured' => false,
            ],
        ];

        // Kategori adları/açıklamaları için TR çevirileri (slug bazlı).
        $trCategory = [
            'villa' => ['name' => 'Villa', 'description' => 'Lüks villalar, müstakil evler ve özel konutlar.'],
            'land' => ['name' => 'Arsa', 'description' => 'Arsalar, parseller ve imarlı araziler.'],
            'apartment' => ['name' => 'Daire', 'description' => 'Daireler ve apartman katları.'],
            'commercial-property' => ['name' => 'Ticari Mülk', 'description' => 'Dükkanlar, ticari binalar ve iş yerleri.'],
            'office' => ['name' => 'Ofis', 'description' => 'Ofis alanları ve kurumsal mülkler.'],
            'residence' => ['name' => 'Rezidans', 'description' => 'Rezidans daireleri ve yönetimli yaşam alanları.'],
        ];

        foreach ($categories as $category) {
            $tr = $trCategory[$category['slug']] ?? [];

            ListingCategory::updateOrCreate(
                ['slug' => $category['slug']],
                [
                    'name' => ['en' => $category['name'], 'tr' => $tr['name'] ?? $category['name']],
                    'description' => ['en' => $category['description'], 'tr' => $tr['description'] ?? $category['description']],
                    'image' => $category['image'],
                    'image_alt' => $category['image_alt'],
                    'sort_order' => $category['sort_order'],
                    'is_active' => true,
                    'is_featured' => $category['is_featured']
                ]
            );
        }

        $features = [
            [
                'name' => 'Gross Area',
                'key' => 'gross_area',
                'input_type' => 'number',
                'unit' => 'm²',
                'is_filterable' => true,
                'sort_order' => 1,
            ],
            [
                'name' => 'Net Area',
                'key' => 'net_area',
                'input_type' => 'number',
                'unit' => 'm²',
                'is_filterable' => true,
                'sort_order' => 2,
            ],
            [
                'name' => 'Room Count',
                'key' => 'room_count',
                'input_type' => 'number',
                'unit' => null,
                'is_filterable' => true,
                'sort_order' => 3,
            ],
            [
                'name' => 'Bathroom Count',
                'key' => 'bathroom_count',
                'input_type' => 'number',
                'unit' => null,
                'is_filterable' => true,
                'sort_order' => 4,
            ],
            [
                'name' => 'Pool',
                'key' => 'pool',
                'input_type' => 'boolean',
                'unit' => null,
                'is_filterable' => true,
                'sort_order' => 5,
            ],
            [
                'name' => 'Garden',
                'key' => 'garden',
                'input_type' => 'boolean',
                'unit' => null,
                'is_filterable' => true,
                'sort_order' => 6,
            ],
            [
                'name' => 'Parking',
                'key' => 'parking',
                'input_type' => 'boolean',
                'unit' => null,
                'is_filterable' => true,
                'sort_order' => 7,
            ],
            [
                'name' => 'Sea View',
                'key' => 'sea_view',
                'input_type' => 'boolean',
                'unit' => null,
                'is_filterable' => true,
                'sort_order' => 8,
            ],
            [
                'name' => 'Heating Type',
                'key' => 'heating_type',
                'input_type' => 'select',
                'unit' => null,
                'options' => [
                    'Natural Gas',
                    'Electric',
                    'Floor Heating',
                    'Central Heating',
                    'None',
                ],
                'is_filterable' => true,
                'sort_order' => 9,
            ],
            [
                'name' => 'Zoning Status',
                'key' => 'zoning_status',
                'input_type' => 'select',
                'unit' => null,
                'options' => [
                    'Residential',
                    'Commercial',
                    'Agricultural',
                    'Tourism',
                    'Mixed Use',
                ],
                'is_filterable' => true,
                'sort_order' => 10,
            ],
            [
                'name' => 'Block Number',
                'key' => 'block_no',
                'input_type' => 'text',
                'unit' => null,
                'is_filterable' => false,
                'sort_order' => 11,
            ],
            [
                'name' => 'Parcel Number',
                'key' => 'parcel_no',
                'input_type' => 'text',
                'unit' => null,
                'is_filterable' => false,
                'sort_order' => 12,
            ],
            [
                'name' => 'Deed Status',
                'key' => 'deed_status',
                'input_type' => 'select',
                'unit' => null,
                'options' => [
                    'Freehold',
                    'Shared Title',
                    'Condominium',
                    'Allocation',
                ],
                'is_filterable' => true,
                'sort_order' => 13,
            ],
            [
                'name' => 'Furnishing Status',
                'key' => 'furnishing_status',
                'input_type' => 'select',
                'unit' => null,
                'options' => [
                    'Furnished',
                    'Semi-Furnished',
                    'Unfurnished',
                ],
                'is_filterable' => true,
                'sort_order' => 14,
            ],
            [
                'name' => 'Balcony',
                'key' => 'balcony',
                'input_type' => 'boolean',
                'unit' => null,
                'is_filterable' => true,
                'sort_order' => 15,
            ],
            [
                'name' => 'Elevator',
                'key' => 'elevator',
                'input_type' => 'boolean',
                'unit' => null,
                'is_filterable' => true,
                'sort_order' => 16,
            ],
            [
                'name' => 'Air Conditioning',
                'key' => 'air_conditioning',
                'input_type' => 'boolean',
                'unit' => null,
                'is_filterable' => true,
                'sort_order' => 17,
            ],
            [
                'name' => 'Smart Home',
                'key' => 'smart_home',
                'input_type' => 'boolean',
                'unit' => null,
                'is_filterable' => true,
                'sort_order' => 18,
            ],
            [
                'name' => '24/7 Security',
                'key' => 'security',
                'input_type' => 'boolean',
                'unit' => null,
                'is_filterable' => true,
                'sort_order' => 19,
            ],
            [
                'name' => 'Gym',
                'key' => 'gym',
                'input_type' => 'boolean',
                'unit' => null,
                'is_filterable' => true,
                'sort_order' => 20,
            ],
            [
                'name' => 'Building Age',
                'key' => 'building_age',
                'input_type' => 'number',
                'unit' => 'years',
                'is_filterable' => true,
                'sort_order' => 21,
            ],
            [
                'name' => 'Floor Number',
                'key' => 'floor_number',
                'input_type' => 'number',
                'unit' => null,
                'is_filterable' => true,
                'sort_order' => 22,
            ],
            [
                'name' => 'Total Floors',
                'key' => 'total_floors',
                'input_type' => 'number',
                'unit' => null,
                'is_filterable' => false,
                'sort_order' => 23,
            ],
            [
                'name' => 'Energy Class',
                'key' => 'energy_class',
                'input_type' => 'select',
                'unit' => null,
                'options' => [
                    'A',
                    'B',
                    'C',
                    'D',
                    'E',
                    'F',
                    'G',
                ],
                'is_filterable' => true,
                'sort_order' => 24,
            ],
            [
                'name' => 'Property View',
                'key' => 'property_view',
                'input_type' => 'multiselect',
                'unit' => null,
                'options' => [
                    'Sea',
                    'City',
                    'Mountain',
                    'Garden',
                    'Pool',
                    'Forest',
                    'Lake',
                ],
                'is_filterable' => true,
                'sort_order' => 25,
            ],
        ];

        // Feature adları için TR çevirileri (key bazlı).
        $trFeature = [
            'gross_area' => 'Brüt Alan',
            'net_area' => 'Net Alan',
            'room_count' => 'Oda Sayısı',
            'bathroom_count' => 'Banyo Sayısı',
            'pool' => 'Havuz',
            'garden' => 'Bahçe',
            'parking' => 'Otopark',
            'sea_view' => 'Deniz Manzarası',
            'heating_type' => 'Isıtma Tipi',
            'zoning_status' => 'İmar Durumu',
            'block_no' => 'Ada No',
            'parcel_no' => 'Parsel No',
            'deed_status' => 'Tapu Durumu',
            'furnishing_status' => 'Eşya Durumu',
            'balcony' => 'Balkon',
            'elevator' => 'Asansör',
            'air_conditioning' => 'Klima',
            'smart_home' => 'Akıllı Ev',
            'security' => '7/24 Güvenlik',
            'gym' => 'Spor Salonu',
            'building_age' => 'Bina Yaşı',
            'floor_number' => 'Bulunduğu Kat',
            'total_floors' => 'Toplam Kat',
            'energy_class' => 'Enerji Sınıfı',
            'property_view' => 'Manzara',
        ];

        // Seçenek (select/multiselect) etiketlerinin TR çevirileri (kanonik → TR).
        $trOptions = [
            'heating_type' => [
                'Natural Gas' => 'Doğal Gaz', 'Electric' => 'Elektrik',
                'Floor Heating' => 'Yerden Isıtma', 'Central Heating' => 'Merkezi Isıtma', 'None' => 'Yok',
            ],
            'zoning_status' => [
                'Residential' => 'Konut', 'Commercial' => 'Ticari', 'Agricultural' => 'Tarım',
                'Tourism' => 'Turizm', 'Mixed Use' => 'Karma',
            ],
            'deed_status' => [
                'Freehold' => 'Müstakil Tapu', 'Shared Title' => 'Hisseli Tapu',
                'Condominium' => 'Kat Mülkiyeti', 'Allocation' => 'Tahsis',
            ],
            'furnishing_status' => [
                'Furnished' => 'Eşyalı', 'Semi-Furnished' => 'Yarı Eşyalı', 'Unfurnished' => 'Eşyasız',
            ],
            'property_view' => [
                'Sea' => 'Deniz', 'City' => 'Şehir', 'Mountain' => 'Dağ', 'Garden' => 'Bahçe',
                'Pool' => 'Havuz', 'Forest' => 'Orman', 'Lake' => 'Göl',
            ],
        ];

        foreach ($features as $feature) {
            $optionLabels = null;

            if (! empty($feature['options'])) {
                // en = kanonik (kimlik), tr = çeviri (yoksa kanonik).
                $en = [];
                $tr = [];
                $trMap = $trOptions[$feature['key']] ?? [];
                foreach ($feature['options'] as $opt) {
                    $en[$opt] = $opt;
                    $tr[$opt] = $trMap[$opt] ?? $opt;
                }
                $optionLabels = ['en' => $en, 'tr' => $tr];
            }

            ListingFeature::updateOrCreate(
                ['key' => $feature['key']],
                [
                    'name' => ['en' => $feature['name'], 'tr' => $trFeature[$feature['key']] ?? $feature['name']],
                    'input_type' => $feature['input_type'],
                    'unit' => $feature['unit'] ?? null,
                    'options' => $feature['options'] ?? null,
                    'option_labels' => $optionLabels,
                    'is_filterable' => $feature['is_filterable'],
                    'is_active' => true,
                    'sort_order' => $feature['sort_order'],
                ]
            );
        }

        $this->attachFeaturesToCategories();
    }

    private function attachFeaturesToCategories(): void
    {
        $categoryFeatures = [
            'villa' => [
                'gross_area',
                'net_area',
                'room_count',
                'bathroom_count',
                'pool',
                'garden',
                'parking',
                'sea_view',
                'heating_type',
                'air_conditioning',
                'smart_home',
                'security',
                'furnishing_status',
                'building_age',
                'energy_class',
                'property_view',
                'deed_status',
            ],
            'land' => [
                'gross_area',
                'zoning_status',
                'block_no',
                'parcel_no',
                'property_view',
                'deed_status',
            ],
            'apartment' => [
                'gross_area',
                'net_area',
                'room_count',
                'bathroom_count',
                'parking',
                'sea_view',
                'heating_type',
                'balcony',
                'elevator',
                'air_conditioning',
                'furnishing_status',
                'building_age',
                'floor_number',
                'total_floors',
                'energy_class',
                'property_view',
                'deed_status',
            ],
            'commercial-property' => [
                'gross_area',
                'net_area',
                'parking',
                'heating_type',
                'air_conditioning',
                'elevator',
                'security',
                'building_age',
                'energy_class',
                'deed_status',
            ],
            'office' => [
                'gross_area',
                'net_area',
                'parking',
                'heating_type',
                'air_conditioning',
                'elevator',
                'security',
                'smart_home',
                'floor_number',
                'total_floors',
                'building_age',
                'energy_class',
                'furnishing_status',
                'deed_status',
            ],
            'residence' => [
                'gross_area',
                'net_area',
                'room_count',
                'bathroom_count',
                'pool',
                'parking',
                'sea_view',
                'heating_type',
                'balcony',
                'elevator',
                'air_conditioning',
                'smart_home',
                'security',
                'gym',
                'furnishing_status',
                'building_age',
                'floor_number',
                'total_floors',
                'energy_class',
                'property_view',
                'deed_status',
            ],
        ];

        foreach ($categoryFeatures as $categorySlug => $featureKeys) {
            $category = ListingCategory::where('slug', $categorySlug)->first();

            if (!$category) {
                continue;
            }

            foreach ($featureKeys as $index => $featureKey) {
                $feature = ListingFeature::where('key', $featureKey)->first();

                if (!$feature) {
                    continue;
                }

                CategoryFeature::updateOrCreate(
                    [
                        'category_id' => $category->id,
                        'feature_id' => $feature->id,
                    ],
                    [
                        'is_required' => in_array($featureKey, ['gross_area', 'deed_status']),
                        'sort_order' => $index + 1,
                    ]
                );
            }
        }
    }
}
