<?php

namespace Modules\Subscription\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\Package\Services\PackageService;
use Modules\Subscription\Requests\Admin\SubscriptionRequest;
use Modules\Subscription\Services\SubscriptionService;
use Modules\Currency\Services\CurrencyService;

class SubscriptionController extends Controller
{

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

    public function __construct(
        private SubscriptionService $subscriptionService,
        private PackageService $packageService
    )
    {}

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

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

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

    /**
     * Show the form for editing the specified Subscription.
     *
     * @param  int $id
     * @return View
     */
    public function edit(int $id) :View
    {
        $subscription = $this->subscriptionService->getItem($id);
        if (!$subscription)
        {
            abort(404);
        }

        $head= __('subscriptions.edit');
        return view( $this->viewDirectory.'form', get_defined_vars() );
    }


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

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

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

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