<?php

namespace Modules\Coupon\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\Coupon\Enums\DiscountStatusEnum;
use Modules\Coupon\Requests\Admin\CouponRequest;
use Modules\Coupon\Services\CouponService;

class CouponController extends Controller
{

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

    public function __construct(
        private CouponService $couponService,
    )
    {}

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

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

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

    /**
     * Show the form for creating a new Coupon.
     *
     * @return View
     */
    public function create() :View
    {
        $action= route('coupons.store');
        $head= __('coupons.new');
        $discount_statuses = DiscountStatusEnum::values();
        return view( $this->viewDirectory.'form', get_defined_vars() );
    }

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

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

    /**
     * Show the form for editing the specified Coupon.
     *
     * @param  int $id
     * @return View
     */
    public function edit(int $id) :View
    {
        $coupon = $this->couponService->getItem($id);
        if (!$coupon)
        {
            abort(404);
        }
        $action= route('coupons.update', $coupon->id);
        $head= __('coupons.edit');
        $discount_statuses = DiscountStatusEnum::values();
        return view( $this->viewDirectory.'form', get_defined_vars() );
    }

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

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

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

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

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