<?php

namespace App\Http\Controllers\Web;

use App\Http\Controllers\Controller;
use App\Models\Region;

class RegionController extends Controller
{
    public function provincesByCountry($countryId)
    {
        $provinces = Region::query()
            ->where('type', Region::$province)
            ->where('country_id', $countryId)
            ->select('id', 'title', 'country_id')
            ->orderBy('title')
            ->get();

        return response()->json([
            'code' => 200,
            'provinces' => $provinces,
        ]);
    }

    public function citiesByProvince($provinceId)
    {
        $cities = Region::query()
            ->where('type', Region::$city)
            ->where('province_id', $provinceId)
            ->select('id', 'title', 'province_id', 'country_id')
            ->orderBy('title')
            ->get();

        return response()->json([
            'code' => 200,
            'cities' => $cities,
        ]);
    }

    public function districtsByCity($cityId)
    {
        $districts = Region::query()
            ->where('type', Region::$district)
            ->where('city_id', $cityId)
            ->select('id', 'title', 'city_id')
            ->orderBy('title')
            ->get();

        return response()->json([
            'code' => 200,
            'districts' => $districts,
        ]);
    }

    public function citiesByCountry($countryId)
    {
        $cities = Region::query()
            ->where('type', Region::$city)
            ->where('country_id', $countryId)
            ->select('id', 'title', 'province_id', 'country_id')
            ->orderBy('title')
            ->get();

        return response()->json([
            'code' => 200,
            'cities' => $cities,
        ]);
    }
}
