<?php

namespace App\Http\Controllers\Admin;

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

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

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

    public function store(Request $request)
    {
        $data = $request->validate([
            'state_id'           => ['required', 'exists:states,id'],
            'name'               => ['required', 'string', 'max:150'],
            'code'               => ['nullable', 'string', 'max:20'],
            'planning_authority' => ['nullable', 'string', 'max:255'],
            'dcr_version'        => ['nullable', 'string', 'max:100'],
            'master_plan_year'   => ['nullable', 'integer', 'min:1900', 'max:2100'],
            'is_active'          => ['boolean'],
        ]);
        $data['is_active'] = $request->boolean('is_active', true);
        $city = City::create($data);
        ActivityLog::record('create', $city, 'Created city: ' . $city->name);
        return redirect()->route('admin.cities.index')->with('success', 'City added successfully.');
    }

    public function show(City $city)
    {
        return redirect()->route('admin.cities.edit', $city);
    }

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

    public function update(Request $request, City $city)
    {
        $data = $request->validate([
            'state_id'           => ['required', 'exists:states,id'],
            'name'               => ['required', 'string', 'max:150'],
            'code'               => ['nullable', 'string', 'max:20'],
            'planning_authority' => ['nullable', 'string', 'max:255'],
            'dcr_version'        => ['nullable', 'string', 'max:100'],
            'master_plan_year'   => ['nullable', 'integer', 'min:1900', 'max:2100'],
            'is_active'          => ['boolean'],
        ]);
        $data['is_active'] = $request->boolean('is_active', false);
        $city->update($data);
        ActivityLog::record('update', $city, 'Updated city: ' . $city->name);
        return redirect()->route('admin.cities.index')->with('success', 'City updated successfully.');
    }

    public function destroy(City $city)
    {
        ActivityLog::record('delete', $city, 'Deleted city: ' . $city->name);
        $city->delete();
        return redirect()->route('admin.cities.index')->with('success', 'City deleted.');
    }
}
