<?php

namespace Modules\Address\Controllers\Admin;

/** Ajax Request */

use App\Services\Response\ResponseService;
use Exception;
use Illuminate\View\View;
use Illuminate\Http\Request;
use Illuminate\Http\JsonResponse;
use App\Http\Controllers\Controller;
use Modules\Address\Requests\Admin\AddressRequest;
use Modules\City\Services\CityService;
use Modules\Address\Services\AddressService;

class AddressController extends Controller
{

    /** Redirect to this path after each operation success*/
    private $redirectSuccessPath= '/admin/addresses';
    /** View folder */
    private $viewDirectory= 'admin.addresses.';
    //

    public function __construct(
        private AddressService $addressService,
        private CityService $cityService,
    )
    {}

    /**
     * Display a listing of the addresses.
     *
     * @param Request $request
     * @return View
     */
    public function index(Request $request) :View
    {
        $title= __('addresses.head');
        $cities = $this->cityService->get([]);
        return view($this->viewDirectory.'index', get_defined_vars());
    }

    /**
     * return data of the addresses.
     *
     * @param Request $request
     * @return View
     */
    public function grid(Request $request) :View
    {
        $request->merge(['paginate'=>true]);
        $addresses= $this->addressService->get($request->all());

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

    /**
     * Show the form for creating a new address.
     *
     * @return View
     */
    public function create(Request $request) :View
    {
        $action= route('addresses.store');
        $head= __('addresses.new');
        $cities = $this->cityService->get([]);

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

    /**
     * Store a newly created address in storage.
     * @param AddressRequest $request
     * @return JsonResponse
     */
    public function store(AddressRequest $request) :JsonResponse
    {
        $this->addressService->create($request->validated());

        session()->flash('message', __('dashboard.saveDone'));
        $response = [
            'status'=>200,
            'message'=>__('web.success'),
            'data'=> route('addresses.index').'?user_id='.$request->user_id,
        ];
        return ResponseService::response($response);
    }

    /**
     * Show the form for editing the specified address.
     *
     * @param  int $id
     * @return View
     */
    public function edit(Request $request,int $id) :View
    {
        $address = $this->addressService->getItem($id);
        if (!$address)
        {
            abort(404);
        }
        $action= route('addresses.update', $address->id);
        $head= __('addresses.edit');
        $cities = $this->cityService->get([]);
        return view( $this->viewDirectory.'form', get_defined_vars() );
    }

    /**
     * Update the specified address in storage.
     * @param AddressRequest $request
     * @param int $id
     * @return JsonResponse
     */
    public function update(AddressRequest $request, int $id) :JsonResponse
    {
        $address = $this->addressService->checkItem($id);
        if (!$address)
        {
            abort(404);
        }
        $this->addressService->update($request->validated(), $id);
        session()->flash('message', __('dashboard.saveDone'));
        $response = [
            'status'=>200,
            'message'=>__('web.success'),
            'data'=> route('addresses.index').'?user_id='.$request->user_id,
        ];
        return ResponseService::response($response);
    }

    /**
     * Remove the specified address from storage.
     * @param int $id
     * @return JsonResponse
     * @throws Exception
     */
    public function destroy(int $id) :JsonResponse
    {
        $this->addressService->destroy($id);
        session()->flash('message', __('dashboard.saveDone'));
        $response = [
            'status'=>200,
            'message'=>__('web.success'),
            'data'=> route('addresses.index'),
        ];
        return ResponseService::response($response);
    }

    /**
     * Remove several addresses by IDs.
     *
     * @param Request $request
     * @return JsonResponse
     */
    public function destroyAll(Request $request) :JsonResponse
    {
        $this->addressService->destroyAll([
            'ids' => $request->ids,
            'status' => 1
        ]);
        $response = [
            'status'=>200,
            'message'=>__('web.success'),
            'data'=> route('addresses.index'),
        ];
        return ResponseService::response($response);
    }

    /**
     * Restore the specified address from storage.
     *
     * @param int $id
     * @return JsonResponse
     */
    public function restore(int $id) :JsonResponse
    {
        $this->addressService->destroy($id);
        $response = [
            'status'=>200,
            'message'=>__('web.success'),
            'data'=> route('addresses.index'),
        ];
        return ResponseService::response($response);
    }

    /**
     * Restore several addresses by IDs.
     *
     * @param Request $request
     * @return JsonResponse
     */
    public function restoreAll(Request $request) :JsonResponse
    {
        $this->addressService->destroyAll([
            'ids' => $request->ids,
            'status' => 0
        ]);
        $response = [
            'status'=>200,
            'message'=>__('web.success'),
            'data'=> route('addresses.index'),
        ];
        return ResponseService::response($response);
    }
}
