<?php

declare(strict_types=1);

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Http\Requests\Admin\StoreTaxRateRequest;
use App\Http\Requests\Admin\UpdateTaxRateRequest;
use App\Models\TaxRate;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Inertia\Inertia;
use Inertia\Response;

class TaxRateController extends Controller
{
    public function index(Request $request): Response
    {
        $query = TaxRate::query();

        if ($request->filled('search')) {
            $search = $request->input('search');
            $query->where(function ($q) use ($search): void {
                $q->where('name', 'like', '%'.$search.'%')
                    ->orWhere('country_code', 'like', '%'.$search.'%')
                    ->orWhere('region_code', 'like', '%'.$search.'%');
            });
        }

        if ($request->filled('country') && $request->input('country') !== 'all') {
            $query->where('country_code', $request->input('country'));
        }

        if ($request->filled('status') && $request->input('status') !== 'all') {
            $query->where('is_active', $request->input('status') === 'active');
        }

        $taxRates = $query
            ->orderBy('country_code')
            ->orderBy('region_code')
            ->orderBy('priority')
            ->paginate(25);

        $countries = TaxRate::query()
            ->select('country_code')
            ->distinct()
            ->orderBy('country_code')
            ->pluck('country_code');

        return Inertia::render('Admin/TaxRates/Index', [
            'taxRates' => $taxRates,
            'countries' => $countries,
            'filters' => [
                'search' => $request->input('search', ''),
                'country' => $request->input('country', 'all'),
                'status' => $request->input('status', 'all'),
            ],
        ]);
    }

    public function create(): Response
    {
        return Inertia::render('Admin/TaxRates/Create');
    }

    public function store(StoreTaxRateRequest $request): RedirectResponse
    {
        TaxRate::query()->create([
            'name' => $request->validated('name'),
            'country_code' => strtoupper($request->validated('country_code')),
            'region_code' => $request->validated('region_code'),
            'rate' => $request->validated('rate'),
            'type' => $request->validated('type'),
            'priority' => $request->validated('priority', 0),
            'is_active' => $request->boolean('is_active', true),
        ]);

        return redirect()
            ->route('admin.tax-rates.index')
            ->with('success', 'Tax rate created successfully.');
    }

    public function edit(TaxRate $taxRate): Response
    {
        return Inertia::render('Admin/TaxRates/Edit', [
            'taxRate' => $taxRate,
        ]);
    }

    public function update(UpdateTaxRateRequest $request, TaxRate $taxRate): RedirectResponse
    {
        $taxRate->update([
            'name' => $request->validated('name'),
            'country_code' => strtoupper($request->validated('country_code')),
            'region_code' => $request->validated('region_code'),
            'rate' => $request->validated('rate'),
            'type' => $request->validated('type'),
            'priority' => $request->validated('priority', 0),
            'is_active' => $request->boolean('is_active', true),
        ]);

        return redirect()
            ->route('admin.tax-rates.index')
            ->with('success', 'Tax rate updated successfully.');
    }

    public function destroy(TaxRate $taxRate): RedirectResponse
    {
        $taxRate->delete();

        return redirect()
            ->route('admin.tax-rates.index')
            ->with('success', 'Tax rate deleted successfully.');
    }

    public function toggleActive(TaxRate $taxRate): RedirectResponse
    {
        $taxRate->update(['is_active' => ! $taxRate->is_active]);

        $status = $taxRate->is_active ? 'activated' : 'deactivated';

        return redirect()
            ->back()
            ->with('success', "Tax rate {$status} successfully.");
    }
}
