<?php

namespace Core\App\Controllers;

use Core\App\Response;

class CitiesController
{
    /**
     * Return list of cities for a given Wilaya (state code)
     */
    public function getByState($stateCode)
    {
        $stateCode = trim($stateCode);

        $file = ROOT_PATH . '/storage/data/cities_dz.json';

        if (!file_exists($file)) {
            return Response::json([
                'status'  => 'error',
                'message' => 'Cities data not found'
            ], 404);
        }

        $json = json_decode(file_get_contents($file), true);

        if (!$json || !isset($json[$stateCode])) {
            return Response::json([
                'status'  => 'error',
                'message' => 'Invalid state code'
            ], 404);
        }

        return Response::json([
            'status' => 'success',
            'state'  => $stateCode,
            'cities' => $json[$stateCode]
        ], 200);
    }
}
