<?php

namespace App\Http\Controllers\Web;

use App\Http\Controllers\Controller;
use App\Services\Response\ResponseService;
use Illuminate\Http\Request;
use Modules\City\Services\CityService;
use Modules\Country\Services\CountryService;
use Modules\User\Models\Address;
use Modules\User\Requests\Web\AddressRequest;
use Modules\User\Services\AccountService;
use Modules\User\Services\AddressService;

class AddressController extends Controller
{
    private $view = 'web.pages.profile.addresses.';
    private AddressService $addressService ;
    private AccountService $accountService ;
    private CountryService $countryService ;
    private CityService $cityService ;


    public function __construct(
        AddressService $addressService,
        AccountService $accountService,
        CountryService $countryService,
        CityService $cityService,
    )
    {
        $this->addressService = $addressService;
        $this->accountService = $accountService;
        $this->countryService = $countryService;
        $this->cityService = $cityService;
    }

    public function index(Request $request)
    {
        $type = $request->type;
        $account = $this->accountService->getAccount();
        $request->merge(['user_id'=>$account['user']->id,'user_status'=>$account['status']]);
        $addresses = $this->addressService->get($request);
        return view($this->view.$type,get_defined_vars());
    }

    public function create(Request $request)
    {
        $action = route('address.store');
        $countries = $this->countryService->get($request);
        $cities = [];
        $title = __('web.add_new_address');

        return view($this->view.'form',get_defined_vars());
    }

    public function store(AddressRequest $request)
    {
        $account = $this->accountService->getAccount();
        $request->merge(['user_id'=>$account['user']->id,'user_status'=>$account['status']]);
        $this->addressService->save($request,null);
        $response = [
            'status' => 200,
            'message' => __('web.success'),
            'data' => url()->previous()
        ];
        return ResponseService::response($response);
    }

    public function fetchCity(Request $request)
    {
        $cities = $this->cityService->get($request);
        return view($this->view.'cities',get_defined_vars());
    }


    public function edit(Request $request,Address $address)
    {
        $title = __('web.edit_address');
        $account = $this->accountService->getAccount();
        Address::where('id',$address->id)->where('user_status',$account['status'])->where('user_id',$account['user']->id)->firstOrFail();
        $action = route('address.update',$address->id);
        $countries = $this->countryService->get($request);
        $request->merge(['country_id'=>$address->country_id]);
        $cities = $this->cityService->get($request);

        return view($this->view.'form',get_defined_vars());
    }

    public function update(AddressRequest $request,Address $address)
    {
        $account = $this->accountService->getAccount();
        Address::where('id',$address->id)->where('user_status',$account['status'])->where('user_id',$account['user']->id)->firstOrFail();
        $request->merge(['user_id'=>$account['user']->id,'user_status'=>$account['status']]);
        $this->addressService->save($request,$address);
        $response = [
            'status' => 200,
            'message' => __('web.success'),
            'data' => url()->previous()
        ];
        return ResponseService::response($response);
    }

    public function destroy($id)
    {
        $account = $this->accountService->getAccount();
        Address::where('id',$id)->where('user_status',$account['status'])->where('user_id',$account['user']->id)->firstOrFail();
        $this->addressService->destroy(request(),$id);
        $response = [
            'status' => 200,
            'message' => __('web.success'),
            'data' => url()->previous()
        ];
        return ResponseService::response($response);
    }
}
