<?php

namespace Modules\Newsletter\Repositories;

use Modules\Newsletter\Models\NewsletterReply;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Modules\Newsletter\Interfaces\NewsletterRepositoryInterface;
use Modules\Newsletter\Models\Newsletter;

class NewsletterRepository implements NewsletterRepositoryInterface
{

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

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

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

        return $newsletters;
    }


    public function getItem(Newsletter $newsletter)
    {
        return $newsletter;
    }

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

    public function reply(Request $request, Newsletter $newsletter): void
    {
        NewsletterReply::create([
            'reply' => $request->reply,
            'visitor_email' => $request->visitor_email,
            'user_id' => Auth::id(),
            'newsletter_id' => $newsletter ? $newsletter->id : null,
        ]);

    }

    public function replies(Newsletter $newsletter)
    {
        $replies = NewsletterReply::where('newsletter_id',$newsletter->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= Newsletter::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= Newsletter::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= Newsletter::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;
        Newsletter::whereIn('id', $ids)->onlyTrashed()->restore();
        return [['message'=> __('dashboard.saveDone')]];
    }
}
