<?php

namespace App\Modules\Platform\Geography\Controllers;

use App\Http\Controllers\Controller;
use App\Modules\Platform\Geography\Models\GeoCity;
use App\Modules\Platform\Geography\Models\GeoRegion;
use App\Modules\Platform\Geography\Repositories\GeoCountryRepository;
use App\Modules\Platform\Geography\Requests\StoreTenantCityRequest;
use App\Modules\Platform\Geography\Requests\StoreTenantRegionRequest;
use App\Modules\Platform\Geography\Requests\UpdateTenantCityRequest;
use App\Modules\Platform\Geography\Requests\UpdateTenantRegionRequest;
use App\Modules\Platform\Geography\Services\GeographyCatalogService;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\View\View;

class GeographyPageController extends Controller
{
    public function __construct(
        private readonly GeographyCatalogService $catalog,
        private readonly GeoCountryRepository $countries,
    ) {}

    public function index(): RedirectResponse
    {
        return redirect()->route('settings.geography.countries.index');
    }

    public function countriesIndex(): View
    {
        return view('pages.settings.geography.countries.index', [
            'countries' => $this->catalog->paginateCountries(),
            'activeTab' => 'countries',
        ]);
    }

    public function regionsIndex(Request $request): View
    {
        $tenantId = (int) auth()->user()->tenant_id;
        $countryId = $request->integer('country_id') ?: null;

        return view('pages.settings.geography.regions.index', [
            'regions' => $this->catalog->paginateRegions($tenantId, $countryId),
            'countryOptions' => $this->countries->listForSelect(),
            'countryId' => $countryId,
            'activeTab' => 'regions',
        ]);
    }

    public function regionsCreate(): View
    {
        return view('pages.settings.geography.regions.create', [
            'countryOptions' => $this->countries->listForSelect(),
            'activeTab' => 'regions',
        ]);
    }

    public function regionsStore(StoreTenantRegionRequest $request): RedirectResponse
    {
        $this->catalog->createRegion((int) auth()->user()->tenant_id, $request->validated());

        return redirect()
            ->route('settings.geography.regions.index')
            ->with('status', __('geography.flash.region_created'));
    }

    public function regionsEdit(GeoRegion $region): View
    {
        $this->catalog->assertTenantOwned($region, (int) auth()->user()->tenant_id);

        return view('pages.settings.geography.regions.edit', [
            'region' => $region->loadMissing('country'),
            'countryOptions' => $this->countries->listForSelect(),
            'activeTab' => 'regions',
        ]);
    }

    public function regionsUpdate(UpdateTenantRegionRequest $request, GeoRegion $region): RedirectResponse
    {
        $this->catalog->assertTenantOwned($region, (int) auth()->user()->tenant_id);
        $this->catalog->updateRegion($region, $request->validated());

        return redirect()
            ->route('settings.geography.regions.index')
            ->with('status', __('geography.flash.region_updated'));
    }

    public function regionsDeactivate(GeoRegion $region): RedirectResponse
    {
        $this->catalog->assertTenantOwned($region, (int) auth()->user()->tenant_id);
        $this->catalog->deactivateRegion($region);

        return redirect()
            ->route('settings.geography.regions.index')
            ->with('status', __('geography.flash.region_deactivated'));
    }

    public function citiesIndex(Request $request): View
    {
        $tenantId = (int) auth()->user()->tenant_id;
        $countryId = $request->integer('country_id') ?: null;
        $regionId = $request->integer('region_id') ?: null;

        return view('pages.settings.geography.cities.index', [
            'cities' => $this->catalog->paginateCities($tenantId, $countryId, $regionId),
            'countryOptions' => $this->countries->listForSelect(),
            'countryId' => $countryId,
            'regionId' => $regionId,
            'activeTab' => 'cities',
        ]);
    }

    public function citiesCreate(): View
    {
        return view('pages.settings.geography.cities.create', [
            'countryOptions' => $this->countries->listForSelect(),
            'activeTab' => 'cities',
        ]);
    }

    public function citiesStore(StoreTenantCityRequest $request): RedirectResponse
    {
        $this->catalog->createCity((int) auth()->user()->tenant_id, $request->validated());

        return redirect()
            ->route('settings.geography.cities.index')
            ->with('status', __('geography.flash.city_created'));
    }

    public function citiesEdit(GeoCity $city): View
    {
        $this->catalog->assertTenantOwned($city, (int) auth()->user()->tenant_id);

        return view('pages.settings.geography.cities.edit', [
            'city' => $city->loadMissing('region.country'),
            'countryOptions' => $this->countries->listForSelect(),
            'activeTab' => 'cities',
        ]);
    }

    public function citiesUpdate(UpdateTenantCityRequest $request, GeoCity $city): RedirectResponse
    {
        $this->catalog->assertTenantOwned($city, (int) auth()->user()->tenant_id);
        $this->catalog->updateCity($city, $request->validated());

        return redirect()
            ->route('settings.geography.cities.index')
            ->with('status', __('geography.flash.city_updated'));
    }

    public function citiesDeactivate(GeoCity $city): RedirectResponse
    {
        $this->catalog->assertTenantOwned($city, (int) auth()->user()->tenant_id);
        $this->catalog->deactivateCity($city);

        return redirect()
            ->route('settings.geography.cities.index')
            ->with('status', __('geography.flash.city_deactivated'));
    }
}
