<?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 Illuminate\Routing\Redirector;
use Illuminate\Http\RedirectResponse;
use App\Http\Controllers\Controller;
use Modules\Coupon\Enums\CouponDiscountStatusEnum;
use Modules\Coupon\Enums\CouponStatusEnum;
use Modules\Coupon\Models\Coupon;
use Modules\Coupon\Requests\Admin\CouponRequest;
use Modules\Coupon\Services\CouponService;
use Modules\Product\Services\ProductService;

class CouponController extends Controller
{

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

    public function __construct(CouponService $couponService, ProductService $productService)
    {
        $this->couponService = $couponService;
        $this->productService = $productService;
    }

    /**
     * Display a listing of the coupons.
     *
     * @param Request $request
     * @return View
     */
    public function index(Request $request) :View
    {
        $coupons = $this->couponService->get($request);
        $title= __('coupons.head');
        $discount_statuses = CouponDiscountStatusEnum::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->merge(['paginate'=> true]);
        $coupons= $this->couponService->get($request);

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

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

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

        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  Coupon  $coupon
     * @return View
     */
    public function edit(Request $request,Coupon $coupon) :View
    {
        // dd($coupon->id);
        $action= route('coupons.update', $coupon->id);
        $data= $this->couponService->getItem($coupon);
        $head= __('coupons.edit');
        $coupons = $this->couponService->get($request);
        $discount_statuses = CouponDiscountStatusEnum::values();
        $statuses = CouponStatusEnum::values();
        $products = $this->couponService->products($coupon);
        return view( $this->viewDirectory.'form', get_defined_vars() );
    }
    public function products(Request $request)
    {
        $products = $this->productService->get($request);
        return json_encode($products);
    }
    /**
     * Update the specified Coupon in storage.
     * @param CouponRequest $request
     * @param Coupon $coupon
     * @return RedirectResponse|Redirector
     */
    public function update(CouponRequest $request, Coupon $coupon) :JsonResponse
    {
        $coupon= $this->couponService->save($request, $coupon);
        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 Request $request
     * @param int $id
     * @return JsonResponse
     * @throws Exception
     */
    public function destroy(Request $request, int $id) :JsonResponse
    {
        $return= $this->couponService->destroy($request, $id);
        return response()->json($return[0], $return[1] ?? 200);
    }

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

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

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