<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use App\Models\Province;
use Illuminate\Http\JsonResponse;


   
class ProvinceController extends Controller
{
    public function index(): JsonResponse
    {
        $provinces = Province::with('communes')->get();

        return response()->json([
            'success' => true,
            'data' => $provinces,
            'message' => 'Provinces retrieved successfully.'
        ]);
    }

    /**
     * Show a specific province by slug
     */
    public function show(string $slug): JsonResponse
    {
        $province = Province::with('communes')->where('slug', $slug)->first();

        if (!$province) {
            return response()->json([
                'success' => false,
                'message' => 'Province not found.'
            ], 404);
        }

        return response()->json([
            'success' => true,
            'data' => $province,
            'message' => 'Province retrieved successfully.'
        ]);
    }

    /**
     * Get communes for a specific province by slug
     */
    public function communes(string $slug): JsonResponse
    {
        $province = Province::where('slug', $slug)->first();

        if (!$province) {
            return response()->json([
                'success' => false,
                'message' => 'Province not found.'
            ], 404);
        }

        $communes = $province->communes;

        return response()->json([
            'success' => true,
            'data' => [
                'province' => $province->name,
                'communes' => $communes
            ],
            'message' => 'Communes retrieved successfully.'
        ]);
    }
}