<?php

namespace App\Http\Controllers;

use App\Models\City;
use App\Models\Continent;
use App\Models\Country;
use Illuminate\Http\Request;

class LocationLookupController extends Controller
{
    public function countriesByContinent(Request $request)
    {
        $request->validate([
            'continent_id' => 'required|exists:continents,id',
        ]);

        $continent = Continent::findOrFail($request->continent_id);

        $rows = Country::where('continent', $continent->name)
            ->active()
            ->orderBy('name')
            ->get(['id', 'name']);

        return response()->json($rows);
    }

    public function citiesByCountry(Request $request)
    {
        $request->validate([
            'country_id' => 'required|exists:countries,id',
        ]);

        $rows = City::where('country_id', $request->country_id)
            ->active()
            ->orderBy('name')
            ->get(['id', 'name']);

        return response()->json($rows);
    }
}
