<?php

namespace App\Http\Controllers\Admin;

use App\Coupon;
use Exception;
use Illuminate\Contracts\View\Factory;
use Illuminate\View\View;
use Illuminate\Http\Request;
use Illuminate\Http\JsonResponse;
use App\Http\Controllers\Controller;
use Illuminate\Http\RedirectResponse;
use App\Http\Requests\CouponRequest;

class CouponController extends Controller
{

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

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

    /**
     * return data of the coupons.
     *
     * @param Request $request
     * @return View
     */
    public function grid(Request $request) :View
    {
        $languageId = currentLanguage()->id;

        $query = Coupon::orderBy('id', 'desc')->whereNull('user_id');


        if ($request->date_from) {
            $query->whereDate('coupons.created_at', '>=', $request->date_from);
        }

        if ($request->date_to) {
            $query->whereDate('coupons.created_at', '<=', $request->date_to);
        }

        if ($request->code) {
            $query->where('coupons.code', 'LIKE', '%'.$request->code.'%');
        }

        if ($request->service_type) {
            $query->where('coupons.service_type', $request->service_type);
        }
        if ($request->discount_type) {
            $query->where('coupons.discount_type', $request->discount_type);
        }
        if ($request->coupon_status == 'active') {
            $query->where('coupons.status', 1);
        }
        if ($request->coupon_status == 'not_active') {
            $query->where('coupons.status', 0);
        }

        if ( !is_null($request->status) && $request->status == 0) {
            $query->onlyTrashed();
        }

        $coupons= $query->paginate(100);

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

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

    /**
     * Store a newly created resource in storage.
     *
     * @param CouponRequest $request
     * @return JsonResponse
     */
    public function store(CouponRequest $request) :JsonResponse
    {
        $data = $request->all();
        Coupon::create($data);
        session()->flash('message', __('dashboard.saveDone'));
        return response()->json(['url' => route('coupons.index')]);
    }


    /**
     * Show the form for editing the specified resource.
     *
     * @param Coupon $coupon
     * @return Factory|RedirectResponse|View
     */
    public function edit(Coupon $coupon)
    {
        if ($coupon->user_id){
            return  redirect()->route('coupons.index');
        }
        $action = route('coupons.update', $coupon->id);
        $title = metaFields('coupons', 'edit', getCurrentLocale()) ?? __('coupons.edit');
        $head = metaFields('coupons', 'edit', getCurrentLocale()) ?? __('coupons.edit');
        $local = getCurrentLocale();
        return view( $this->viewDirectory.'form', get_defined_vars());
    }

    /**
     * Update the specified resource in storage.
     *
     * @param CouponRequest $request
     * @param Coupon $coupon
     * @return JsonResponse
     */
    public function update(CouponRequest $request, Coupon $coupon) :JsonResponse
    {
        $data = $request->all();
        $coupon->update($data);
        session()->flash('message', __('dashboard.saveDone'));
        return response()->json(['url' => route('coupons.index')]);
    }

    public function activeCoupon(Coupon $coupon)
    {
        if ($coupon->status){
            $coupon->update(['status' => 0]);
            return response()->json(['message' => __('members.de_active_success')]);
        } else {
            $coupon->update(['status' => 1]);
            return response()->json(['message' => __('members.active_success')]);
        }

    }


    /**
     * Remove the specified resource from storage.
     *
     * @param Request $request
     * @param int $id
     * @return JsonResponse
     * @throws Exception
     */
    public function destroy(Request $request, int $id) :JsonResponse
    {
        $coupon = Coupon::withTrashed()->find($id);
        if ($coupon) {
            if ($coupon->deleted_at) {
                $coupon->forceDelete();
            } else {
                $coupon->delete();
            }
            return response()->json(['message'=> __('dashboard.deletedDone')]);
        } else{
            return response()->json(['message'=> __('dashboard.noResult')], 400);
        }
    }

    /**
     * Remove several coupons by IDs.
     *
     * @param Request $request
     * @return JsonResponse
     */
    public function destroyAll(Request $request) :JsonResponse
    {
        $ids= $request->ids;
        if ($request->force) {
            Coupon::onlyTrashed()->whereIn('id', $ids)->forceDelete();
        } else {
            Coupon::whereIn('id', $ids)->delete();
        }

        return response()->json(['message'=> __('dashboard.deletedDone')]);
    }

    /**
     * Restore the specified category from storage
     *
     * @param Request $request
     * @param int $id
     * @return JsonResponse
     */
    public function restore(Request $request, int $id) :JsonResponse
    {
        $coupon = Coupon::withTrashed()->find($id);

        if ($coupon){
            $coupon->restore();
            return response()->json(['message'=> __('dashboard.saveDone')]);
        }
        return response()->json(['message'=> __('dashboard.noResult')], 400);
    }

    /**
     * Restore several coupons by IDs.
     *
     * @param Request $request
     * @return JsonResponse
     */
    public function restoreAll(Request $request) :JsonResponse
    {
        $coupons = Coupon::whereIn('id', $request->ids)->onlyTrashed();
        if ($coupons){
            $coupons->restore();
            return response()->json(['message'=> __('dashboard.saveDone')]);
        }
    }
}
