<?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\SellerRequest;
use Modules\Coupon\Services\CouponService;
use Modules\Product\Services\ProductService;
use Modules\User\Enums\UserRoleEnum;
use Modules\User\Services\UserService;

class SellerController extends Controller
{

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

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

    /**
     * Display a listing of the sellers.
     *
     * @param Request $request
     * @return View
     */
    public function index(Request $request) :View
    {
        $sellers = $this->couponService->get($request);
        $title= __('sellers.head');
        $request->merge(['role'=>UserRoleEnum::SELLER->value]);
        $users = $this->userService->get($request);
        $discount_statuses = CouponDiscountStatusEnum::values();
        return view($this->viewDirectory.'index', get_defined_vars());
    }

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

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

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

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

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

    /**
     * Show the form for editing the specified Coupon.
     *
     * @param  Coupon  $coupon
     * @return View
     */
    public function edit(Request $request,Coupon $seller) :View
    {
        // dd($coupon->id);
        $action= route('sellers.update', $seller->id);
        $data= $this->couponService->getItem($seller);
        $head= __('sellers.edit');
        $sellers = $this->couponService->get($request);
        $discount_statuses = CouponDiscountStatusEnum::values();
        $statuses = CouponStatusEnum::values();
        $products = $this->couponService->products($seller);
        $request->merge(['role'=>UserRoleEnum::SELLER->value]);
        $users = $this->userService->get($request);
        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 SellerRequest $request
     * @param Coupon $seller
     * @return RedirectResponse|Redirector
     */
    public function update(SellerRequest $request, Coupon $seller) :JsonResponse
    {
        $seller= $this->couponService->save($request, $seller);
        session()->flash('message', __('dashboard.saveDone'));
        $response = [
            'status'=>200,
            'message'=>__('web.success'),
            'data'=> route('sellers.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 sellers 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 sellers 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);
    }
}
