<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\Models\Country;
use App\Models\Models\State;
use App\Models\Models\City;
class LocationController extends Controller
{

    public function getCountries()
    {
        $countries = Country::select('id', 'name', 'phonecode')->get();

        return response()->json([
            'data' => $countries
        ]);
    }

    // public function getState($id){
    //     $state = State::with('getCountryName')->select('id','name')
    // }


    // public function getState($countryName)
    // {
    //     $country = Country::where('name', $countryName)->first();

    //     if (!$country) {
    //         return response()->json([
    //             'status' => false,
    //             'message' => 'Country not found.'
    //         ], 404);
    //     }
    //     $states = State::with('getCountryName')
    //         ->where('country_xid', $country->id)
    //         ->select('id', 'name')
    //         ->get();

    //     return response()->json([
    //         // 'status' => true,
    //         'data' => $states
    //     ]);
    // }

    public function getState(Request $request)
    {
        $countryName = $request->query('country_name');

        $country = Country::where('name', $countryName)->first();

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

        $states = State::with('getCountryName')
            ->where('country_xid', $country->id)
            ->select('id', 'name')
            ->get();

        return response()->json([
            // 'status' => true,
            'data' => $states
        ]);
    }

    public function getCity(Request $request) {

        $stateName = $request->query('state_name');

        $state = State::where('name', $stateName)->first();

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

        $states = city::with('getCityName')
            ->where('state_xid', $state->id)
            ->select('id', 'name')
            ->get();

        return response()->json([
            // 'status' => true,
            'data' => $states
        ]);
    }


}
