<?php

namespace App\Http\Controllers\Admin;

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

class ZoneRuleController extends Controller
{
    public function index(Request $request)
    {
        $query = ZoneRule::with(['zone.state', 'city']);
        if ($request->filled('zone_id')) {
            $query->where('zone_id', $request->zone_id);
        }
        if ($request->filled('city_id')) {
            $query->where('city_id', $request->city_id);
        }
        $zoneRules = $query->latest()->paginate(25)->withQueryString();
        $zones     = Zone::with('state')->orderBy('name')->get();
        $cities    = City::orderBy('name')->get();
        return view('admin.zone-rules.index', compact('zoneRules', 'zones', 'cities'));
    }

    public function create()
    {
        $zones  = Zone::with('state')->where('is_active', true)->orderBy('name')->get();
        $cities = City::where('is_active', true)->orderBy('name')->get();
        return view('admin.zone-rules.form', compact('zones', 'cities'));
    }

    public function store(Request $request)
    {
        $data = $request->validate([
            'zone_id'              => ['required', 'exists:zones,id'],
            'city_id'              => ['nullable', 'exists:cities,id'],
            'road_width_min_m'     => ['required', 'numeric', 'min:0'],
            'road_width_max_m'     => ['nullable', 'numeric', 'min:0'],
            'max_far'              => ['nullable', 'numeric', 'min:0', 'max:20'],
            'max_height_m'         => ['nullable', 'numeric', 'min:0'],
            'max_ground_coverage'  => ['nullable', 'numeric', 'min:0', 'max:100'],
            'open_space_pct'       => ['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'],
            'parking_ratio_ecs'    => ['nullable', 'numeric', 'min:0'],
            'rule_source'          => ['nullable', 'string', 'max:500'],
            'special_conditions'   => ['nullable', 'string'],
            'is_active'            => ['boolean'],
        ]);
        $data['is_active'] = $request->boolean('is_active', true);
        ZoneRule::create($data);
        return redirect()->route('admin.zone-rules.index')->with('success', 'Zone rule created successfully.');
    }

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

    public function edit(ZoneRule $zoneRule)
    {
        $zones  = Zone::with('state')->where('is_active', true)->orderBy('name')->get();
        $cities = City::where('is_active', true)->orderBy('name')->get();
        return view('admin.zone-rules.form', compact('zoneRule', 'zones', 'cities'));
    }

    public function update(Request $request, ZoneRule $zoneRule)
    {
        $data = $request->validate([
            'zone_id'              => ['required', 'exists:zones,id'],
            'city_id'              => ['nullable', 'exists:cities,id'],
            'road_width_min_m'     => ['required', 'numeric', 'min:0'],
            'road_width_max_m'     => ['nullable', 'numeric', 'min:0'],
            'max_far'              => ['nullable', 'numeric', 'min:0', 'max:20'],
            'max_height_m'         => ['nullable', 'numeric', 'min:0'],
            'max_ground_coverage'  => ['nullable', 'numeric', 'min:0', 'max:100'],
            'open_space_pct'       => ['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'],
            'parking_ratio_ecs'    => ['nullable', 'numeric', 'min:0'],
            'rule_source'          => ['nullable', 'string', 'max:500'],
            'special_conditions'   => ['nullable', 'string'],
            'is_active'            => ['boolean'],
        ]);
        $data['is_active'] = $request->boolean('is_active', false);
        $zoneRule->update($data);
        return redirect()->route('admin.zone-rules.index')->with('success', 'Zone rule updated successfully.');
    }

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