<?php

namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;
use App\Models\Province;
use App\Models\District;
use App\Models\Subdistrict;
use App\Models\City;
use Illuminate\Http\Request;

class AddressController extends Controller
{
    public function index(Request $request)
    {
        $addresses = auth()->user()->addresses;
        return view('account.addresses', compact('addresses'));
    }

    public function provinces()
    {
        $provinces = Province::orderBy('name')->get();


        return [
            'data'  => $provinces,
        ];
    }

    public function cities($provinceId)
    {
        $cities = City::where('province_id', $provinceId)->orderBy('name')->get();

        return [
            'data' => $cities,
        ];
    }

    public function districts($city_id)
    {
        $districts = District::where('city_id', $city_id)->orderBy('name')->get();

        return [
            'data' => $districts,
        ];
    }

    public function villages($districtId)
    {
        $villages = Subdistrict::where('district_id', $districtId)->orderBy('name')->get();

        return [
            'data' => $villages,
        ];
    }
}
