<?php

namespace App\Services\Repositories;

use App\Models\VisitorMessage;
use App\Models\VisitorMessageDescription;
use App\Services\Interfaces\VisitorMessageRepositoryInterface;
use Illuminate\Http\Request;
use Mcamara\LaravelLocalization\Facades\LaravelLocalization;

class VisitorMessageRepository implements VisitorMessageRepositoryInterface
{
    public function get(Request $request)
    {
        $query = VisitorMessage::orderBy('id', 'DESC');

        if ($request->ids)
        {
            $query->whereIn('visitor_messages.id',$request->ids);
        }
        if ($request->name) {
            $query->where('pd.title', 'LIKE', '%'.$request->name.'%');
        }
        if ($request->date_from) {
            $query->whereDate('visitor_messages.created_at', '>=', $request->date_from);
        }

        if ($request->date_to) {
            $query->whereDate('visitor_messages.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->get();
        }

        return $clients;
    }

    public function getItem(VisitorMessage $visitor_message)
    {
        return $visitor_message ;
    }

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

    /**
     * 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= VisitorMessage::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= VisitorMessage::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= VisitorMessage::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;
        VisitorMessage::whereIn('id', $ids)->onlyTrashed()->restore();
        return [['message'=> __('dashboard.saveDone')]];
    }
}
