<?php

namespace Modules\Notification\Services;

use Illuminate\Http\Request;
use Modules\Notification\Enums\ManagementNotificationStatusEnum;
use Modules\Notification\Enums\NotificationStatusEnum;
use Modules\Notification\Interfaces\ManagementNotificationRepositoryInterface;
use Modules\Notification\Interfaces\NotificationRepositoryInterface;
use Modules\Notification\Models\ManagementNotification;
use Modules\Notification\Models\Notification;
use Modules\Order\Enums\OrderStatusEnum;
use Modules\User\Enums\UserRoleEnum;
use Modules\User\Services\UserService;

class ManagementNotificationService
{

    public function __construct
    (
        private ManagementNotificationRepositoryInterface $managementNotificationRepository
    )
    {}

    public function get(array $request = [])
    {
        $builder = $this->managementNotificationRepository->get($request);

        if (@$request['title'])
        {
            $builder->where('sd.title', 'LIKE', '%'.$request['title'].'%');
        }

        if (isset($request['type']))
        {
            $builder->where('management_notifications.status',$request['type']);
        }

        if (isset($request['user_id']))
        {
            if ($request['user_id'] == 'all')
            {
                $builder->whereNull('management_notifications.user_id');
            }
            else
            {
                $builder->where('management_notifications.user_id',$request['user_id']);
            }
        }

        if (@$request['date_from'])
        {
            $builder->whereDate('management_notifications.created_at', '>=', $request['date_from']);
        }

        if (@$request['date_to'])
        {
            $builder->whereDate('management_notifications.created_at', '<=', $request['date_to']);
        }

        if (isset($request['status']))
        {
            if (@$request['status'] == 0)
            {
                $builder->onlyTrashed();
            }
        }

        if (@$request['paginate'])
        {
            $notifications = $builder->paginate(50);
        }else{
            $notifications = $builder->cursor();
        }
        return $notifications;
    }

    public function create(array $request): void
    {
        $request['status'] = $request['type'];
        if($request['type'] == NotificationStatusEnum::ALL)
        {
            $request['user_id'] = null;
        }
        else
        {
            if (isset($request['target']))
            {
                $request['user_id'] = $request['target'];
            }
            else
            {
                if ($request['type'] == NotificationStatusEnum::PARENT->value)
                {
                    $request['user_id'] = null;
                }
                else
                {
                    $request['user_id'] = null;
                }
            }
        }
        $notification = $this->managementNotificationRepository->create($request);
        $this->managementNotificationRepository->saveData($request,$notification->id);
    }

    public function checkItem($id)
    {
        $canteen = $this->managementNotificationRepository->checkItem($id);
        return $canteen;
    }

    public function getItem(int $id)
    {
        $notification = $this->checkItem($id);
        $managementNotificationDescription = $this->managementNotificationRepository->getItem($id);
        foreach ($managementNotificationDescription as $row) {
            $notification[$row->local]= $row;
        }
        return $notification;
    }

}
