<?php

namespace Modules\Store\Controllers\Admin;

/** Ajax Request */

use App\Exports\StoreExport;
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 Maatwebsite\Excel\Facades\Excel;
use Modules\Category\Services\CategoryService;
use Modules\Store\Enums\StoreDayEnum;
use Modules\Store\Enums\StoreDiscountStatusEnum;
use Modules\Store\Enums\StoreStatusAccountEnum;
use Modules\Store\Enums\StoreStatusActiveEnum;
use Modules\Store\Enums\StoreStatusEnum;
use Modules\Store\Models\Store;
use Modules\Store\Requests\Admin\StoreRequest;
use Modules\Store\Services\StoreService;
use Modules\Order\Services\OrderService;
use Modules\Product\Services\ProductService;

class StoreController extends Controller
{

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

    public function __construct(StoreService $storeService,CategoryService $categoryService)
    {
        $this->storeService = $storeService;
        $this->categoryService = $categoryService;
    }

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

    /**
     * return data of the stores.
     *
     * @param Request $request
     * @return View
     */
    public function grid(Request $request) //:View
    {
        if (!$request->export)
        {
            $request->merge(['paginate'=>true]);
        }
        $stores= $this->storeService->get($request);
        if ($request->export)
        {
            return Excel::download(new StoreExport($stores), 'Stores.xlsx');
        }
        return view($this->viewDirectory.'grid',compact('stores'));
    }

    /**
     * Show the form for creating a new Store.
     *
     * @return View
     */
    public function create(Request $request) :View
    {
        $action= route('stores.store');
        $head= __('stores.new');
        $statuses_account = StoreStatusAccountEnum::values();
        $statuses_active = StoreStatusActiveEnum::values();
        $request = $request->merge(['category_id'=>'main']);
        $categories = $this->categoryService->get($request);
        $days = StoreDayEnum::values();
        return view( $this->viewDirectory.'form', get_defined_vars() );
    }

    /**
     * Store a newly created Store in storage.
     * @param StoreRequest $request
     * @return RedirectResponse|Redirector
     */
    public function store(StoreRequest $request) :JsonResponse
    {
        $this->storeService->save($request, null);

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

    public function show(Request $request,Store $store) :View
    {
        $request->merge(['store_id'=>$store->id]);
        $orders = $this->orderService->get($request);
        return view( $this->viewDirectory.'orders', get_defined_vars() );
    }
    /**
     * Show the form for editing the specified Store.
     *
     * @param  Store  $store
     * @return View
     */
    public function edit(Request $request,Store $store) :View
    {
        // dd($store->id);
        $action= route('stores.update', $store->id);
        $store= $this->storeService->getItem($store);
        $head= __('stores.edit');
        $stores = $this->storeService->get($request);
        $statuses_account = StoreStatusAccountEnum::values();
        $statuses_active = StoreStatusActiveEnum::values();
        $request = $request->merge(['category_id'=>'main']);
        $categories = $this->categoryService->get($request);
        $request = $request->merge(['category_id'=>$store->category_id]);
        $sub_categories = $this->categoryService->get($request);
        $days = StoreDayEnum::values();
        return view( $this->viewDirectory.'form', get_defined_vars() );
    }
    /**
     * Update the specified Store in storage.
     * @param StoreRequest $request
     * @param Store $store
     * @return RedirectResponse|Redirector
     */
    public function update(StoreRequest $request, Store $store) :JsonResponse
    {
        $store= $this->storeService->save($request, $store);
        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 Request $request
     * @param int $id
     * @return JsonResponse
     * @throws Exception
     */
    public function destroy(Request $request, int $id) :JsonResponse
    {
        $return= $this->storeService->destroy($request, $id);
        return response()->json($return[0], $return[1] ?? 200);
    }

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

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

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