<?php

namespace Modules\User\Controllers\Web;

use App\Http\Controllers\Controller;
use App\Services\Response\ResponseService;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Modules\City\Services\CityService;
use Modules\Country\Services\CountryService;
use Modules\Order\Services\CartService;
use Modules\Region\Services\RegionService;
use Modules\User\Enums\AccountStatusEnum;
use Modules\User\Enums\AddressStatusEnum;
use Modules\User\Requests\Web\AddressRequest;
use Modules\User\Requests\Web\ChangeCityRequest;
use Modules\User\Requests\Web\ChangeTypeRequest;
use Modules\User\Requests\Web\DeleteAddressRequest;
use Modules\User\Requests\Web\LoginRequest;
use Modules\User\Requests\Web\RegistRequest;
use Modules\User\Requests\Web\VerifyAddressRequest;
use Modules\User\Requests\Web\VerifyRequest;
use Modules\User\Requests\Web\EmailRequest;
use Modules\User\Services\AccountService;
use Modules\User\Services\AddressService;
use Modules\User\Services\UserService;

class AddressController extends Controller
{
    private $viewDirectory = 'web.pages.profile.addresses.';
    public function __construct(
        private AddressService $addressService,
        private AccountService $accountService,
        private CountryService $countryService,
        private CityService $cityService,
        private RegionService $regionService,
        private CartService $cartService,
        private UserService $userService,
    ){}

    public function index()
    {
        $title = __('web.all_addresses');
        $account = $this->accountService->getAccount();
        $user = $account['user'];

        $addresses  = $this->addressService->get([
            'user_status' => $account['status'],
            'user_id' => $user->id
        ]);
        return view($this->viewDirectory.'index',get_defined_vars());
    }

    public function create()
    {
        $account = $this->accountService->getAccount();
        $user = $account['user'];
        $countries = $this->countryService->get();
        $regions = [];
        $cities = [];
        $action = route('address.store');
        $title = __('web.address_create');
        $statuses = AddressStatusEnum::values();
        return view($this->viewDirectory.'form',get_defined_vars());
    }

    public function sendCode(AddressRequest $request)
    {
        $account = $this->accountService->getAccount();
        $data = [
            'email'=>$account['user']->email
        ];
        $this->userService->sendCode($data);
        $response =
            [
                'status'=>200,
                'data'=>[],
                'message'=>__('web.success')
            ];
        return ResponseService::response($response);
    }

    public function store(VerifyAddressRequest $request)
    {
        $account = $this->accountService->getAccount();
        $data = $request->validated();
        $data['user_status'] = $account['status'];
        $data['user_id'] = $account['user']->id;
        $address = $this->addressService->create($data);
        if ($request->setDefault)
        {
            $this->userService->update([
                'address_id'=>$address->id
            ],$account['user']->id);
        }
        return ResponseService::response([
            'status'=>200,
            'message'=>__('web.success'),
            'data'=>route('address.index')
        ]);
    }

    public function edit($id)
    {
        $address = $this->addressService->getItem($id);
        if (!$address)
        {
            return abort(404);
        }
        $account = $this->accountService->getAccount();
        $user = $account['user'];
        if ($address->user_id != $user->id || $address->user_status != $account['status'])
        {
            return abort(404);
        }
        $countries = $this->countryService->get();
        $regions = $this->regionService->get(['country_id'=>$address->country_id]);
        $cities = $this->cityService->get(['region_id'=>$address->region_id]);
        $statuses = AddressStatusEnum::values();
        $action = route('address.update',$id);
        $title = __('web.address_edit');
        return view($this->viewDirectory.'form',get_defined_vars());
    }

    public function update(VerifyAddressRequest $request,$id)
    {
        $address = $this->addressService->getItem($id);
        if (!$address)
        {
            return abort(404);
        }
        $account = $this->accountService->getAccount();
        $user = $account['user'];
        if ($address->user_id != $user->id || $address->user_status != $account['status'])
        {
            return abort(404);
        }
        $address = $this->addressService->update($request->validated(),$address->id);
        if ($request->setDefault)
        {
            $this->userService->update([
                'address_id'=>$id
            ],$user->id);
        }

        return ResponseService::response([
            'status'=>200,
            'message'=>__('web.success'),
            'data'=>route('address.index')
        ]);
    }

    public function destroy(DeleteAddressRequest $request,$id)
    {
        $this->addressService->destroy($id);
        return ResponseService::response([
            'status'=>200,
            'message'=>__('web.success'),
            'data'=>null
        ]);
    }

    public function changeCity(ChangeCityRequest $request)
    {
        $account = $this->accountService->getAccount();
        $this->addressService->changeCity([
            'city_id'=>$request->city_id,
            'address_id'=>$account['user']->address_id,
        ]);
        $this->cartService->emptyCart([
            'user_id'=>$account['user']->id,
            'user_status'=>$account['status']
        ]);
        return ResponseService::response([
           'status'=>200,
           'message'=>__('web.success'),
           'data'=>null
        ]);
    }

    public function regions(Request $request)
    {
        $rows = $this->regionService->get([
            'country_id'=>$request->country_id
        ]);
        return view($this->viewDirectory.'fetch',get_defined_vars());
    }
    public function cities(Request $request)
    {
        $rows = $this->cityService->get([
            'region_id'=>$request->region_id
        ]);
        return view($this->viewDirectory.'fetch',get_defined_vars());
    }
}
