<?php

namespace App\Services\Repositories;

use App\Models\Cobon;
use App\Models\CobonDescription;
use App\Models\Notification;
use App\Models\NotificationDescription;
use App\Models\User;
use App\Services\Interfaces\CobonRepositoryInterface;
use App\Services\Interfaces\NotificationRepositoryInterface;
use Illuminate\Http\Request;
use Mcamara\LaravelLocalization\Facades\LaravelLocalization;

class CobonRepository implements CobonRepositoryInterface
{
    public function get(Request $request)
    {
        $query = Cobon::orderBy('id', 'DESC');

        if ($request->code) {
            $query->where('code', $request->code);
        }
        if ($request->cobon) {
            $query->where('code', $request->cobon)->where('count','>',0)->whereDate('start_date','<=',date('Y-m-d'))->whereDate('end_date','>=',date('Y-m-d'));
        }
        if($request->discount_type)
        {
            $query->where('discount_type',$request->discount_type);
        }
        if ($request->date) {
            $query->whereDate('start_date','<=',$request->date)->whereDate('end_date','>=',$request->date);
        }
        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) {
            $clients= $query->paginate(50);
        }else{
            $clients=$query->cursor();
        }

        return $clients;
    }

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


    /**
     * Remove the specified client from storage.
     * @param Request $request
     * @param int $id
     * @return array
     * @throws Exception
     */
    public function destroy(Request $request, int $id) :array
    {
        $client= Cobon::withTrashed()->find($id);
        if ($client) {
            if ($client->deleted_at) {
                $client->forceDelete();
            } else {
                $client->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= Cobon::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
    {
        $client= Cobon::withTrashed()->find($id);
        if ($client) {
            $client->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;
        Cobon::whereIn('id', $ids)->onlyTrashed()->restore();
        return [['message'=> __('dashboard.saveDone')]];
    }
}
