<?php

namespace App\Services\Repositories;

use App\Models\BlockDate;
use App\Models\WorkDay;
use App\Services\Interfaces\NotificationRepositoryInterface;
use App\Services\Interfaces\WorkDayRepositoryInterface;
use Illuminate\Http\Request;

class WorkDayRepository implements WorkDayRepositoryInterface
{
    public function get(Request $request)
    {
        $query = WorkDay::orderBy('id', 'DESC');

        if ($request->day)
        {
            $query->where('day',$request->day);
        }

        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) {
            $workDays= $query->paginate(50);
        }else{
            $workDays=$query->cursor();
        }

        return $workDays;
    }

    //
    public function save(Request $request, ?WorkDay $workDay) :WorkDay
    {
        $data= $request->all();
        unset($data['id']);// for security reasons
        if (!$workDay) {
            $workDay = WorkDay::create($data);
        } else {
            BlockDate::where('id','!=',0)->delete();
            $workDay->update($data);
        }
        if ($request->dates)
        {
            $block = BlockDate::pluck('date')->toArray();
            $dates=array_diff($request->dates,$block);
            foreach ($dates as $date)
            {
                BlockDate::create([
                    'date' => $date,
                ]);
            }
        }
        return $workDay;
    }

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