<?php

namespace App\Http\Controllers\Web;

use App\Http\Requests\Web\LocationRequest;
use App\Models\Area;
use App\Models\City;
use App\Models\Country;
use App\Models\Address;
use App\Services\Auth\User\AccountService;
use App\Services\Response\ResponseService;
use Illuminate\Support\Facades\Auth;
use Illuminate\View\View;
use App\Http\Controllers\Controller;

class LocationController extends Controller
{
    private $view = 'web.pages.profile.components.locations.';

    public function index()
    {
        $user = Auth::user();
        $locations = Address::where('status','user')->where('user_id',$user->id)->cursor();
        return view($this->view.'index', get_defined_vars());
    }

    public function create()
    {
        $action = route('profile.location.store');
        $countries = Country::withDescription()->cursor();
        return view($this->view.'form', get_defined_vars());
    }

    public function store(LocationRequest $request)
    {
        $user = (new AccountService())->getAccount();
        $status = $user['status'];
        $user = $user['user'];
        Address::create([
            'country_id'=>$request->country_id,
            'city_id'=>$request->city_id,
            'area_id'=>$request->area_id,
            'country_address'=>$request->country_address,
            'status'=>$status,
            'address'=>$request->address,
            'user_id'=>$user->id,
        ]);
        $response =
            [
                'status' => 200,
                'message' => __('web.success'),
                'data' => route('profile').'?locations=on',
            ];
        return ResponseService::response($response);
    }
    public function edit($id)
    {
        $user = Auth::user();
        $location = Address::where('status','user')->where('user_id',$user->id)->where('id',$id)->first();
        if(!$location)
        {
            return ResponseService::notFound();
        }
        $action = route('profile.location.update',$id);
        $countries = Country::withDescription()->cursor();
        $cities = City::withDescription()->where('cities.country_id',$location->country_id)->cursor();
        $areas = Area::withDescription()->where('areas.city_id',$location->city_id)->cursor();
        return view($this->view.'form', get_defined_vars());

    }

    public function update(LocationRequest $request,$id)
    {
        $user = Auth::user();
        $location = Address::where('status','user')->where('user_id',$user->id)->where('id',$id)->first();
        if(!$location)
        {
            return ResponseService::notFound();
        }

        $location->update([
            'country_id'=>$request->country_id,
            'city_id'=>$request->city_id,
            'area_id'=>$request->area_id,
            'address'=>$request->address,
            'country_address'=>$request->country_address,
        ]);

        $response =
            [
                'status' => 200,
                'message' => __('web.success'),
                'data' => route('profile').'?locations=on',
            ];
        return ResponseService::response($response);
    }

    public function delete($id)
    {
        $user = Auth::user();
        $location = Address::where('status','user')->where('user_id',$user->id)->where('id',$id)->first();
        if(!$location)
        {
            return ResponseService::notFound();
        }
        $location->delete();
        $response =
            [
                'status' => 200,
                'message' => __('web.success'),
                'data' => '',
            ];
        return ResponseService::response($response);
    }
}
