<?php

namespace Modules\VisitorMessage\Repositories;

use Modules\VisitorMessage\Models\VisitorMessageReply;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Modules\Page\Models\Page;
use Modules\Page\Models\PageDescription;
use Modules\VisitorMessage\Interfaces\VisitorMessageRepositoryInterface;
use Modules\VisitorMessage\Models\VisitorMessage;

class VisitorMessageRepository implements VisitorMessageRepositoryInterface
{

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

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

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

        if ($request->phone) {
            $query->where('phone', $request->phone);
        }
        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) {
            $visitorMessages = $query->paginate(50);
        } else {
            $visitorMessages = $query->cursor();
        }

        return $visitorMessages;
    }


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

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

    public function reply(Request $request, VisitorMessage $visitorMessage): void
    {
        VisitorMessageReply::create([
            'reply' => $request->reply,
            'visitor_email' => $request->visitor_email,
            'user_id' => Auth::id(),
            'visitor_message_id' => $visitorMessage ? $visitorMessage->id : null,
        ]);

        $visitorMessage ? $visitorMessage->update(['reply' => 1]) : null;
    }

    public function replies(VisitorMessage $visitorMessage)
    {
        $replies = VisitorMessageReply::where('visitor_message_id',$visitorMessage->id)->cursor();
        return $replies;
    }

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