<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\State;
use App\Models\Zone;
use Illuminate\Http\Request;

class ZoneController extends Controller
{
    public function index(Request $request)
    {
        $query = Zone::with('state');
        if ($request->filled('state_id')) {
            $query->where('state_id', $request->state_id);
        }
        if ($request->filled('zone_type')) {
            $query->where('zone_type', $request->zone_type);
        }
        $zones  = $query->withCount('zoneRules')->latest()->paginate(25)->withQueryString();
        $states = State::orderBy('name')->get();
        return view('admin.zones.index', compact('zones', 'states'));
    }

    public function create()
    {
        $states = State::where('is_active', true)->orderBy('name')->get();
        return view('admin.zones.form', compact('states'));
    }

    public function store(Request $request)
    {
        $data = $request->validate([
            'name'                 => ['required', 'string', 'max:150'],
            'state_id'             => ['required', 'exists:states,id'],
            'zone_type'            => ['required', 'in:Residential,Commercial,Industrial,Mixed,Agricultural,Green Belt,Heritage'],
            'far_fsi'              => ['nullable', 'numeric', 'min:0', 'max:10'],
            'max_height_m'         => ['nullable', 'numeric', 'min:0'],
            'max_ground_coverage'  => ['nullable', 'numeric', 'min:0', 'max:100'],
            'front_setback_m'      => ['nullable', 'numeric', 'min:0'],
            'side_setback_m'       => ['nullable', 'numeric', 'min:0'],
            'rear_setback_m'       => ['nullable', 'numeric', 'min:0'],
            'open_space_pct'       => ['nullable', 'numeric', 'min:0', 'max:100'],
            'permitted_uses'       => ['nullable', 'string'],
            'description'          => ['nullable', 'string'],
            'regulation_ref'       => ['nullable', 'string', 'max:500'],
            'notes'                => ['nullable', 'string'],
            'is_active'            => ['boolean'],
        ]);
        $data['is_active'] = $request->boolean('is_active', true);
        Zone::create($data);
        return redirect()->route('admin.zones.index')->with('success', 'Zone rule created successfully.');
    }

    public function show(Zone $zone)
    {
        return redirect()->route('admin.zones.edit', $zone);
    }

    public function edit(Zone $zone)
    {
        $states = State::where('is_active', true)->orderBy('name')->get();
        return view('admin.zones.form', compact('zone', 'states'));
    }

    public function update(Request $request, Zone $zone)
    {
        $data = $request->validate([
            'name'                 => ['required', 'string', 'max:150'],
            'state_id'             => ['required', 'exists:states,id'],
            'zone_type'            => ['required', 'in:Residential,Commercial,Industrial,Mixed,Agricultural,Green Belt,Heritage'],
            'far_fsi'              => ['nullable', 'numeric', 'min:0', 'max:10'],
            'max_height_m'         => ['nullable', 'numeric', 'min:0'],
            'max_ground_coverage'  => ['nullable', 'numeric', 'min:0', 'max:100'],
            'front_setback_m'      => ['nullable', 'numeric', 'min:0'],
            'side_setback_m'       => ['nullable', 'numeric', 'min:0'],
            'rear_setback_m'       => ['nullable', 'numeric', 'min:0'],
            'open_space_pct'       => ['nullable', 'numeric', 'min:0', 'max:100'],
            'permitted_uses'       => ['nullable', 'string'],
            'description'          => ['nullable', 'string'],
            'regulation_ref'       => ['nullable', 'string', 'max:500'],
            'notes'                => ['nullable', 'string'],
            'is_active'            => ['boolean'],
        ]);
        $data['is_active'] = $request->boolean('is_active', true);
        $zone->update($data);
        return redirect()->route('admin.zones.index')->with('success', 'Zone rule updated successfully.');
    }

    public function destroy(Zone $zone)
    {
        $zone->delete();
        return redirect()->route('admin.zones.index')->with('success', 'Zone deleted.');
    }
}
