<?php

namespace Modules\Offer\Controllers\Store;

/** 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\Category\Services\CategoryService;
use Modules\Offer\Enums\OfferStatusEnum;
use Modules\Offer\Requests\Store\OfferRequest;
use Modules\Offer\Services\OfferService;

class OfferController extends Controller
{

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

    public function __construct(
        private OfferService $offerService,
        private CategoryService $categoryService,
    )
    {}

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

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

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

    /**
     * Show the form for creating a new Offer.
     *
     * @return View
     */
    public function create() :View
    {
        $action= route('store.offers.store');
        $head= __('offers.new');
        $statuses = OfferStatusEnum::values();
        $status_day = OfferStatusEnum::DAY->value;
        $categories = $this->categoryService->get(['category_id'=>'main']);
        $sub_categories = [];
        $products = [];
        return view( $this->viewDirectory.'form', get_defined_vars() );
    }

    /**
     * Store a newly created Offer in storage.
     * @param OfferRequest $request
     * @return JsonResponse
     */
    public function store(OfferRequest $request) :JsonResponse
    {
        $data = $request->validated();
        $data['store_id'] = auth()->user()->store_id;
        $this->offerService->create($data);

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

    /**
     * Show the form for editing the specified Offer.
     *
     * @param  int $id
     * @return View
     */
    public function edit(int $id) :View
    {
        $offer = $this->offerService->getItem($id);
        if (!$offer)
        {
            abort(404);
        }
        $action= route('store.offers.update', $offer->id);
        $head= __('offers.edit');
        $statuses = OfferStatusEnum::values();
        $status_day = OfferStatusEnum::DAY->value;
        $categories = $this->categoryService->get(['category_id'=>'main']);
        $sub_categories = $this->categoryService->get(['category_id'=>$offer->category_id]);
        $products = $this->offerService->getProduct($id);
        return view( $this->viewDirectory.'form', get_defined_vars() );
    }

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

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

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

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

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