<?php

namespace App\Http\Livewire\Mailings;

use App\Models\Locations\City;
use App\Models\Locations\Country;
use App\Models\Locations\District;
use App\Models\Locations\Division;
use Livewire\Component;

class LocationFilter extends Component
{
    public $selectedCountries = [];
    public $selectedDivisions = [];
    public $selectedCities = [];
    public $selectedDistricts = [];

    public $countries = [];
    public $divisions = [];
    public $cities = [];
    public $districts = [];

    protected $listeners = ['resetLocationFilter', 'loadLocationFilterData'];

    public function mount($selectedCountries = [], $selectedDivisions = [], $selectedCities = [], $selectedDistricts = [])
    {
        $this->selectedCountries = $selectedCountries;
        $this->selectedDivisions = $selectedDivisions;
        $this->selectedCities = $selectedCities;
        $this->selectedDistricts = $selectedDistricts;

        $this->loadLocationData();
    }

    public function loadLocationData()
    {
        // Always load all countries
        $this->countries = Country::with(['translations'])->get();

        // Load divisions if any countries are selected
        if (!empty($this->selectedCountries)) {
            $this->divisions = Division::with(['translations'])
                ->whereIn('country_id', $this->selectedCountries)
                ->get();
        } else {
            $this->divisions = collect();
        }

        // Load cities if any countries are selected
        if (!empty($this->selectedCountries)) {
            $this->cities = City::with(['translations'])
                ->whereIn('country_id', $this->selectedCountries)
                ->get();
        } else {
            $this->cities = collect();
        }

        // Load districts if any cities are selected
        if (!empty($this->selectedCities)) {
            $this->districts = District::with(['translations'])
                ->whereIn('city_id', $this->selectedCities)
                ->get();
        } else {
            $this->districts = collect();
        }
    }

    public function updatedSelectedCountries()
    {
        // Reset lower levels when countries change
        $this->selectedDivisions = [];
        $this->selectedCities = [];
        $this->selectedDistricts = [];

        $this->loadLocationData();
        $this->emitSelectionUpdate();
    }

    public function updatedSelectedDivisions()
    {
        $this->emitSelectionUpdate();
    }

    public function updatedSelectedCities()
    {
        // Reset districts when cities change
        $this->selectedDistricts = [];
        $this->loadLocationData();
        $this->emitSelectionUpdate();
    }

    public function updatedSelectedDistricts()
    {
        $this->emitSelectionUpdate();
    }

    public function resetLocationFilter()
    {
        $this->selectedCountries = [];
        $this->selectedDivisions = [];
        $this->selectedCities = [];
        $this->selectedDistricts = [];
        $this->loadLocationData();
        $this->emitSelectionUpdate();
    }

    public function loadLocationFilterData($data)
    {
        $this->selectedCountries = $data['countries'] ?? [];
        $this->selectedDivisions = $data['divisions'] ?? [];
        $this->selectedCities = $data['cities'] ?? [];
        $this->selectedDistricts = $data['districts'] ?? [];
        $this->loadLocationData();
    }

    private function emitSelectionUpdate()
    {
        $this->dispatch('locationFilterUpdated', [
            'countries' => $this->selectedCountries,
            'divisions' => $this->selectedDivisions,
            'cities' => $this->selectedCities,
            'districts' => $this->selectedDistricts,
        ]);
    }

    public function render()
    {
        return view('livewire.mailings.location-filter');
    }
}