<?php
namespace Modules\Newsletter\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Services\Response\ResponseService;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\View\View;
use Modules\Newsletter\Requests\NewsletterRequest;
use Modules\Newsletter\Services\NewsletterService;
use Modules\Newsletter\Models\Newsletter;

class NewsletterController extends Controller
{
    /** Redirect to this path after each operation success*/
    private $redirectSuccessPath = '/admin/newsletters';
    /** View folder */
    private $viewDirectory = 'admin.newsletters.';

    private $newsletterService;

    public function __construct(NewsletterService $newsletterService )
    {
        $this->newsletterService = $newsletterService;
    }


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

    public function grid(Request $request)
    {
        $request->merge(['paginate'=>true]);
        $newsletters = $this->newsletterService->get($request);
        if($request->export) {
        }
        return view($this->viewDirectory.'grid',compact('newsletters'));
    }

    public function create() :View
    {
        $action= route('newsletters.store');
        $head = metaFields('newsletters', 'add_new', getCurrentLocale()) ?? __('newsletters.edit');

        return view( $this->viewDirectory.'form', compact('action', 'head') );
    }

    public function store(NewsletterRequest $request)
    {
        $this->newsletterService->reply($request,null);

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

    public function edit(Newsletter $newsletter): View
    {
        $action= route('newsletters.update', $newsletter->id);
        $head = metaFields('newsletters', 'edit', getCurrentLocale()) ?? __('newsletters.edit');
        $replies = $this->newsletterService->replies($newsletter);
        return view( $this->viewDirectory.'form', compact('newsletter', 'action', 'head' ,'replies') );
    }

    public function update(NewsletterRequest $request, Newsletter $newsletter)
    {
        $newsletter = $this->newsletterService->reply($request,$newsletter);

        session()->flash('message', __('dashboard.saveDone'));
        $response = [
            'status'=>200,
            'message'=>__('web.success'),
            'data'=> route('newsletters.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->newsletterService->destroy($request, $id);
        return response()->json($return[0], $return[1] ?? 200);
    }

    /**
     * Remove several coupons by IDs.
     *
     * @param Request $request
     * @return JsonResponse
     */
    public function destroyAll(Request $request) :JsonResponse
    {
        $return= $this->newsletterService->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->newsletterService->restore($request, $id);
        return response()->json($return[0], $return[1] ?? 200);
    }

    /**
     * Restore several coupons by IDs.
     *
     * @param Request $request
     * @return JsonResponse
     */
    public function restoreAll(Request $request) :JsonResponse
    {
        $return= $this->newsletterService->restoreAll($request);
        return response()->json($return[0], $return[1] ?? 200);
    }
}
