<?php

namespace App\Http\Controllers\Admin;

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

class RegulationController extends Controller
{
    public function index(Request $request)
    {
        $query = Regulation::with('state');
        if ($request->filled('type')) {
            $query->where('type', $request->type);
        }
        if ($request->filled('state_id')) {
            $query->where('state_id', $request->state_id);
        }
        $regulations = $query->latest()->paginate(20)->withQueryString();
        $states = State::orderBy('name')->get();
        return view('admin.regulations.index', compact('regulations', 'states'));
    }

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

    public function store(Request $request)
    {
        $data = $request->validate([
            'title'          => ['required', 'string', 'max:255'],
            'type'           => ['required', 'string', 'max:100'],
            'state_id'       => ['nullable', 'exists:states,id'],
            'authority'      => ['nullable', 'string', 'max:255'],
            'effective_date' => ['nullable', 'date'],
            'description'    => ['nullable', 'string'],
            'reference_url'  => ['nullable', 'url', 'max:500'],
            'is_active'      => ['boolean'],
        ]);
        $data['is_active'] = $request->boolean('is_active', true);
        Regulation::create($data);
        return redirect()->route('admin.regulations.index')->with('success', 'Regulation created successfully.');
    }

    public function show(Regulation $regulation)
    {
        return redirect()->route('admin.regulations.edit', $regulation);
    }

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

    public function update(Request $request, Regulation $regulation)
    {
        $data = $request->validate([
            'title'          => ['required', 'string', 'max:255'],
            'type'           => ['required', 'string', 'max:100'],
            'state_id'       => ['nullable', 'exists:states,id'],
            'authority'      => ['nullable', 'string', 'max:255'],
            'effective_date' => ['nullable', 'date'],
            'description'    => ['nullable', 'string'],
            'reference_url'  => ['nullable', 'url', 'max:500'],
            'is_active'      => ['boolean'],
        ]);
        $data['is_active'] = $request->boolean('is_active', true);
        $regulation->update($data);
        return redirect()->route('admin.regulations.index')->with('success', 'Regulation updated successfully.');
    }

    public function destroy(Regulation $regulation)
    {
        $regulation->delete();
        return redirect()->route('admin.regulations.index')->with('success', 'Regulation deleted.');
    }
}
