<?php

namespace Modules\Store\Controllers\Admin;

/** Ajax Request */

use App\Services\Response\ResponseService;
use Exception;
use Illuminate\View\View;
use Illuminate\Http\Request;
use Illuminate\Http\JsonResponse;
use Illuminate\Routing\Redirector;
use Illuminate\Http\RedirectResponse;
use App\Http\Controllers\Controller;
use Modules\Area\Services\AreaService;
use Modules\City\Services\CityService;
use Modules\Store\Interfaces\StoreRepositoryInterface;
use Modules\Store\Models\Branch;
use Modules\Store\Models\Store;
use Modules\Store\Requests\Admin\BranchRequest;
use Modules\Store\Services\BranchService;
use Modules\Store\Services\StoreService;

class BranchController extends Controller
{

    /** Redirect to this path after each operation success*/
    private $redirectSuccessPath= '/admin/branches';
    /** View folder */
    private $viewDirectory= 'admin.branches.';
    //
    private BranchService $branchService;
    private StoreService $storeService;
    private CityService $cityService;
    private AreaService $areaService;

    public function __construct(BranchService $branchService , StoreService $storeService ,CityService $cityService,AreaService $areaService)
    {
        $this->branchService = $branchService;
        $this->storeService = $storeService;
        $this->cityService = $cityService;
        $this->areaService = $areaService;
    }

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

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

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

    /**
     * Show the form for creating a new Branch.
     *
     * @return View
     */
    public function create(Request $request) :View
    {
        $action= route('branches.store');
        $head= __('branches.new');
        $stores = $this->storeService->get($request);
        $cities = $this->cityService->get($request);
        $areas = [];
        return view( $this->viewDirectory.'form', get_defined_vars() );
    }

    /**
     * Branch a newly created Branch in storage.
     * @param BranchRequest $request
     * @return RedirectResponse|Redirector
     */
    public function store(BranchRequest $request) :JsonResponse
    {
        $branch= $this->branchService->save($request, null);

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

    /**
     * Show the form for editing the specified Branch.
     *
     * @param  Branch  $branch
     * @return View
     */
    public function edit(Request $request,Branch $branch) :View
    {
        // dd($branch->id);
        $action= route('branches.update', $branch->id);
        $branch= $this->branchService->getItem($branch);
        $stores = $this->storeService->get($request);
        $cities = $this->cityService->get($request);
        $request->merge(['city_id'=>$branch->city_id]);
        $areas = $this->areaService->get($request);
        $head= __('branches.edit');
        return view( $this->viewDirectory.'form', get_defined_vars() );
    }

    /**
     * Update the specified Branch in storage.
     * @param BranchRequest $request
     * @param Branch $branch
     * @return RedirectResponse|Redirector
     */
    public function update(BranchRequest $request, Branch $branch) :JsonResponse
    {
        $branch= $this->branchService->save($request, $branch);
        session()->flash('message', __('dashboard.saveDone'));
        $response = [
            'status'=>200,
            'message'=>__('web.success'),
            'data'=> route('branches.index'),
        ];
        return ResponseService::response($response);
    }

    /**
     * Remove the specified Branch from storage.
     * @param Request $request
     * @param int $id
     * @return JsonResponse
     * @throws Exception
     */
    public function destroy(Request $request, int $id) :JsonResponse
    {
        $return= $this->branchService->destroy($request, $id);
        return response()->json($return[0], $return[1] ?? 200);
    }

    /**
     * Remove several branches by IDs.
     *
     * @param Request $request
     * @return JsonResponse
     */
    public function destroyAll(Request $request) :JsonResponse
    {
        $return= $this->branchService->destroyAll($request);
        return response()->json($return[0], $return[1] ?? 200);
    }

    /**
     * Restore the specified Branch from storage.
     *
     * @param Request $request
     * @param int $id
     * @return JsonResponse
     */
    public function restore(Request $request, int $id) :JsonResponse
    {
        $return= $this->branchService->restore($request, $id);
        return response()->json($return[0], $return[1] ?? 200);
    }

    /**
     * Restore several branches by IDs.
     *
     * @param Request $request
     * @return JsonResponse
     */
    public function restoreAll(Request $request) :JsonResponse
    {
        $return= $this->branchService->restoreAll($request);
        return response()->json($return[0], $return[1] ?? 200);
    }
}
