<?php

namespace App\Http\Controllers\Web;

use App\Http\Controllers\Controller;
use App\Services\Response\ResponseService;
use Illuminate\Http\Request;
use Modules\Area\Services\AreaService;
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 CityService $cityService ;
    private AreaService $areaService ;


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

    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');
        $cities = $this->cityService->get($request);
        $areas = [];
        $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' => route('web.profile')
        ];
        return ResponseService::response($response);
    }

    public function fetchCity(Request $request)
    {
        $cities = $this->cityService->get($request);
        return view($this->view.'cities',get_defined_vars());
    }
    public function fetchArea(Request $request)
    {
        $areas = $this->areaService->get($request);
        return view($this->view.'areas',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);
        $cities = $this->cityService->get($request);
        $request->merge(['city_id'=>$address->city_id]);
        $areas = $this->areaService->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' => route('web.profile')
        ];
        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);
    }
}
