<?php

namespace Modules\Refund\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\Refund\Enums\RefundStatusEnum;
use Modules\Refund\Requests\Admin\RefundRequest;
use Modules\Refund\Services\RefundService;

class RefundController extends Controller
{

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

    public function __construct(
        private RefundService $refundService,
    )
    {}

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

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

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

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

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

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

    /**
     * Show the form for editing the specified Refund.
     *
     * @param  int $id
     * @return View
     */
    public function edit(int $id) :View
    {
        $refund = $this->refundService->getItem($id);
        if (!$refund)
        {
            abort(404);
        }
        $statuses = RefundStatusEnum::values();
        $action= route('refunds.update', $refund->id);
        $head= __('refunds.edit');
        return view( $this->viewDirectory.'form', get_defined_vars() );
    }

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

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

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

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

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