<?php

namespace Modules\Service\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\Feature\Enums\FeatureTypeEnum;
use Modules\Feature\Services\FeatureService;
use Modules\Service\Requests\Admin\ServiceRequest;
use Modules\Service\Services\ServiceService;
use Modules\Currency\Services\CurrencyService;

class ServiceController extends Controller
{

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

    public function __construct(
        private ServiceService $serviceService,
        private FeatureService $featureService
    )
    {}

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

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

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

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

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

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

    /**
     * Show the form for editing the specified Package.
     *
     * @param  int $id
     * @return View
     */
    public function edit(int $id) :View
    {
        $service = $this->serviceService->getItem($id);
        if (!$service)
        {
            abort(404);
        }
        $action= route('services.update', $service->id);
        $head= __('services.edit');
        $features = $this->featureService->get([
            'featureable_id'=>$service->id,
            'featureable_type'=>FeatureTypeEnum::SERVICE->value
        ]);
        return view( $this->viewDirectory.'form', get_defined_vars() );
    }

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

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

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

    /**
     * Restore the specified Package from storage.
     *
     * @param int $id
     * @return JsonResponse
     */
    public function restore(int $id) :JsonResponse
    {
        $this->serviceService->restore($id);
        session()->flash('message', __('dashboard.restoreDone'));
        $response = [
            'status'=>200,
            'message'=>__('web.success'),
            'data'=> route('services.index'),
        ];
        return ResponseService::response($response);
    }

    /**
     * Permanently delete the specified service from storage.
     *
     * @param int $id
     * @return JsonResponse
     */
    public function forceDelete(int $id) :JsonResponse
    {
        $this->serviceService->forceDelete($id);
        session()->flash('message', __('dashboard.deleteDone'));
        $response = [
            'status'=>200,
            'message'=>__('web.success'),
            'data'=> route('services.index'),
        ];
        return ResponseService::response($response);
    }

    /**
     * Permanently delete several services by IDs.
     *
     * @param Request $request
     * @return JsonResponse
     */
    public function forceDeleteAll(Request $request) :JsonResponse
    {
        $this->serviceService->forceDeleteAll($request->ids);
        session()->flash('message', __('dashboard.deleteDone'));
        $response = [
            'status'=>200,
            'message'=>__('web.success'),
            'data'=> route('services.index'),
        ];
        return ResponseService::response($response);
    }

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