<?php

namespace Modules\Coupon\Repositories;

use Illuminate\Http\Request;
use Modules\Coupon\Interfaces\CouponRepositoryInterface;
use Modules\Coupon\Models\Coupon;
use Modules\Coupon\Models\CouponProduct;
use Modules\Order\Services\OrderService;

class CouponRepository implements CouponRepositoryInterface
{

    public function get(Request $request)
    {
        $query = Coupon::orderBy('id', 'DESC');

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

        if ($request->code) {
            $query->where('code', $request->code);
        }
        if ($request->discount_status) {
            $query->where('discount_status', $request->discount_status);
        }
        if ($request->discount) {
            $query->where('discount', $request->discount);
        }
        if ($request->count) {
            $query->where('count', $request->count);
        }
        if ($request->user_count) {
            $query->where('user_count', $request->user_count);
        }

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

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


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

        if ($request->paginate ?? null) {
            $coupons= $query->paginate(50);
        }else{
            $coupons=$query->cursor();
        }

        return $coupons;
    }

    public function getItem(Coupon $coupon)
    {
        return $coupon ;
    }
    public function getItemByCode(string $code)
    {
        $coupon = Coupon::where('code',$code)->first();
        return $coupon;
    }

    //
    public function save(Request $request, ?Coupon $coupon): Coupon
    {
        $data= $request->all();
        unset($data['id']);// for security reasons
        if (!$coupon) {
            $coupon = Coupon::create($data);
        } else {
            $coupon->update($data);
        }
        $coupon->products()->sync($request->product_ids);
        return $coupon;
    }

    public function products(Coupon $coupon)
    {
        $products = CouponProduct::where('coupon_id',$coupon->id)->cursor();
        return $products;
    }

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

    /**
     * Remove several clients by IDs.
     *
     * @param Request $request
     * @return array
     */
    public function destroyAll(Request $request) :array
    {
        $ids= $request->ids;
        $query= Coupon::whereIn('id', $ids);
        if ($request->force) {
            $deleted= $query->onlyTrashed()->forceDelete();
        } else {
            $deleted= $query->delete();
        }
        if($deleted){
            return [['message'=> __('dashboard.deletedDone')]];
        }
        return [ ['message'=> __('dashboard.noResult')], 400];
    }

    /**
     * Restore the specified client from storage.
     *
     * @param Request $request
     * @param int $id
     * @return array
     */
    public function restore(Request $request, int $id) :array
    {
        $coupon= Coupon::withTrashed()->find($id);
        if ($coupon) {
            $coupon->restore();
            return [['message'=> __('dashboard.saveDone')]];
        }
        return [['message'=> __('dashboard.noResult')], 400];

    }


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

}
