<?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 App\Http\Controllers\Controller;
use Modules\Brand\Services\BrandService;
use Modules\Store\Enums\StatusShowSoldEnum;
use Modules\Store\Requests\Admin\StoreRequest;
use Modules\Store\Services\StoreService;
use Modules\Currency\Services\CurrencyService;
use Modules\User\Enums\UserRoleEnum;
use Modules\User\Services\UserService;

class StoreController extends Controller
{

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

    public function __construct(
        private StoreService $storeService,
        private BrandService $brandService,
        private UserService $userService,
    )
    {}

    /**
     * Display a listing of the stores.
     *
     * @param Request $request
     * @return View
     */
    public function index(Request $request) :View
    {
        $title= __('stores.head');
        $statuses = StatusShowSoldEnum::values();
        return view($this->viewDirectory.'index', get_defined_vars());
    }

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

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

    /**
     * Show the form for creating a new Store.
     *
     * @return View
     */
    public function create() :View
    {
        $action= route('stores.store');
        $head= __('stores.new');
        $statuses = StatusShowSoldEnum::values();
        $brands = $this->brandService->get();
        $brand_ids = [];
        return view( $this->viewDirectory.'form', get_defined_vars() );
    }

    /**
     * Store a newly created Store in storage.
     * @param StoreRequest $request
     * @return JsonResponse
     */
    public function store(StoreRequest $request) :JsonResponse
    {
        $store = $this->storeService->create($request->validated());
        $content =
        [
            'name'=>$request->title_ar,
            'email'=>$request->email,
            'phone'=>$request->phone,
            'password'=>$request->phone,
            'role'=>UserRoleEnum::STORE->value,
            'store_id'=>$store->id
        ];
        $this->userService->create($content);
        session()->flash('message', __('dashboard.saveDone'));
        $response = [
            'status'=>200,
            'message'=>__('web.success'),
            'data'=> route('stores.index'),
        ];
        return ResponseService::response($response);
    }

    /**
     * Show the form for editing the specified Store.
     *
     * @param  int $id
     * @return View
     */
    public function edit(int $id) :View
    {
        $store = $this->storeService->getItem($id);
        if (!$store)
        {
            abort(404);
        }
        $action= route('stores.update', $store->id);
        $head= __('stores.edit');
        $statuses = StatusShowSoldEnum::values();
        $brands = $this->brandService->get();
        $brand_ids = $store->brands->pluck('id')->toArray();

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

    /**
     * Update the specified Store in storage.
     * @param StoreRequest $request
     * @param int $id
     * @return JsonResponse
     */
    public function update(StoreRequest $request, int $id) :JsonResponse
    {
        $store = $this->storeService->checkItem($id);
        if (!$store)
        {
            abort(404);
        }
        $this->storeService->update($request->validated(), $id);
        $user = $this->userService->get([
            'role'=>UserRoleEnum::STORE->value,
            'store_id'=>$id
        ])->first();
        $content =
            [
                'name'=>$request->title_ar,
                'email'=>$request->email,
                'phone'=>$request->phone,
                'password'=>$request->password,
                'role'=>UserRoleEnum::STORE->value,
                'store_id'=>$store->id
            ];
        if ($user)
        {
            $this->userService->update($content,$user->id);
        }
        else
        {
            $this->userService->create($content);
        }

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

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

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

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

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