<?php

namespace App\Http\Controllers\Api;
use App\Http\Controllers\Controller;
use App\Http\Requests\StoreCommuneRequest;
use App\Http\Resources\CommuneResource;
use App\Models\Commune;
use Illuminate\Http\Request;

class CommuneController extends Controller
{
    public function index()
    {
        $communes = Commune::all();
        return CommuneResource::collection($communes);
    }

    public function store(StoreCommuneRequest $request)
    {
        $commune = Commune::create($request->validated());
        return new CommuneResource($commune);
    }

    public function show(Commune $commune)
    {
        return new CommuneResource($commune);
    }

    public function update(StoreCommuneRequest $request, Commune $commune)
    {
        $commune->update($request->validated());
        return new CommuneResource($commune);
    }

    public function destroy(Commune $commune)
    {
        $commune->delete();
        return response()->json(['message' => 'Commune supprimé'], 200);
    }
}
