<?php

namespace App\Http\Controllers;

use App\Models\Dictionary\Area;
use Illuminate\Http\Request;

class AreaController extends Controller
{
    public function index()
    {
        return Area::all();
    }

    public function store(Request $request)
    {
        $request->validate([
            'name' => ['required'],
            'district_id' => ['required', 'integer'],
        ]);

        return Area::create($request->validated());
    }

    public function show(Area $area)
    {
        return $area;
    }

    public function update(Request $request, Area $area)
    {
        $request->validate([
            'name' => ['required'],
            'district_id' => ['required', 'integer'],
        ]);

        $area->update($request->validated());

        return $area;
    }

    public function destroy(Area $area)
    {
        $area->delete();

        return response()->json();
    }

    public function ajaxGetAreasByDistrict($district_id = null)
    {
        return Area::query()->where('district_id', '=', (int) $district_id)->pluck('name', 'id')->toArray();
    }
}
