<?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\SelllerRequest;
use Modules\Coupon\Services\CouponService;
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.';
    //

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

    /**
     * Display a listing of the sellers.
     *
     * @param Request $request
     * @return View
     */
    public function index(Request $request) :View
    {
        $title= __('sellers.head');
        $discount_statuses = DiscountStatusEnum::values();
        $users = $this->userService->get(['role'=>UserRoleEnum::SELLER->value]);
        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,'seller'=>true]);
        $sellers= $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('sellers.store');
        $head= __('sellers.new');
        $discount_statuses = DiscountStatusEnum::values();
        $users = $this->userService->get(['role'=>UserRoleEnum::SELLER->value]);
        return view( $this->viewDirectory.'form', get_defined_vars() );
    }

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

        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  int $id
     * @return View
     */
    public function edit(int $id) :View
    {
        $seller = $this->couponService->getItem($id);
        if (!$seller)
        {
            abort(404);
        }
        $action= route('sellers.update', $seller->id);
        $head= __('sellers.edit');
        $discount_statuses = DiscountStatusEnum::values();
        $users = $this->userService->get(['role'=>UserRoleEnum::SELLER->value]);
        return view( $this->viewDirectory.'form', get_defined_vars() );
    }

    /**
     * Update the specified Coupon in storage.
     * @param SelllerRequest $request
     * @param int $id
     * @return JsonResponse
     */
    public function update(SelllerRequest $request, int $id) :JsonResponse
    {
        $seller = $this->couponService->checkItem($id);
        if (!$seller)
        {
            abort(404);
        }
        $this->couponService->update($request->validated(), $id);
        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 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('sellers.index'),
        ];
        return ResponseService::response($response);
    }

    /**
     * Remove several sellers 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('sellers.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('sellers.index'),
        ];
        return ResponseService::response($response);
    }

    /**
     * Restore several sellers 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('sellers.index'),
        ];
        return ResponseService::response($response);
    }
}
