<?php

namespace Modules\Notification\Services;

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

class NotificationService
{

    public function __construct
    (
        private NotificationRepositoryInterface $notificationRepository,
        private UserService $userService,
        private SendNotificationService $sendNotificationService
    ) {
    }

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

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

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

        if (@$request['api']) {
            $user = auth()->user();
            $status = [NotificationStatusEnum::ALL->value];
            if ($user->role == UserRoleEnum::PARENT->value) {
                array_push($status, NotificationStatusEnum::PARENT->value);
            }
            if ($user->role == UserRoleEnum::CANTEEN->value) {
                array_push($status, NotificationStatusEnum::CANTEENS->value);
            }

            $builder->where(function ($query) use ($user, $status) {
                $query->where(function ($q) use ($user) {
                    $q->where('user_id', $user->id)->whereNull('notifications.status');
                })->orWhere(function ($q) use ($status) {
                    $q->whereIn('notifications.status', $status)->whereNull('notifications.user_id');
                });
            })->whereDate('notifications.created_at', '>=', $user->created_at);
        }
        if (@$request['user_id']) {
            $builder->where('user_id', $request['user_id']);
        }

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

        if (@$request['paginate']) {
            $notifications = $builder->paginate(50);
        } elseif (@$request['api']) {
            $notifications = $builder->orderBy('notifications.created_at', 'desc');
        } else {
            $notifications = $builder->cursor();
        }

        return $notifications;
    }


    public function create(array $request)
    {
        if (isset($request['type']) && $request['type'] == NotificationStatusEnum::ALL->value) {

            $user_ids = $this->userService->get()->pluck('id')->toArray();
        } else {
            if (isset($request['target'])) {
                $request['status'] = null;
                $user_ids = [$request['target']];
            } else {
                $request['status'] = $request['type'];
                if ($request['type'] == NotificationStatusEnum::PARENT->value) {
                    $user_ids = $this->userService->get(['role' => UserRoleEnum::PARENT->value])->pluck('id')->toArray();
                } else {
                    $user_ids = $this->userService->get(['role' => UserRoleEnum::CANTEEN->value])->pluck('id')->toArray();
                }
            }
        }
        if (!isset($request['type']) || isset($request['target'])) {
            foreach ($user_ids as $user_id) {
                $request['user_id'] = $user_id;
                $notification = $this->notificationRepository->create($request);
                $this->notificationRepository->saveData($request, $notification->id);
            }
        } else {
            $notification = $this->notificationRepository->create($request);
            $this->notificationRepository->saveData($request, $notification->id);
        }
        $tokens = $this->userService->get()->whereIn('id', $user_ids)->whereNotNull('device_token')->pluck('device_token')->toArray();
        if (is_array($tokens) && count($tokens) > 0) {
            $target['registration_ids'] = $tokens;
            $data=$this->sendNotificationService->sendNotification([
                'title' => $request['title_' . currentLanguage()->local],
                'body' => $request['description_' . currentLanguage()->local]
            ], $target);
        
            // dd($data);
        }
    }
}
