<?php

namespace Modules\Notification\Services;

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

class NotificationService
{
    private NotificationRepositoryInterface $notificationRepository ;
    private UserService $userService ;

    public function __construct(NotificationRepositoryInterface $notificationRepository , UserService $userService)
    {
        $this->notificationRepository = $notificationRepository;
        $this->userService = $userService;
    }

    public function get(Request $request)
    {
        $countries = $this->notificationRepository->get($request);
        return $countries;
    }
    public function getItem(Notification $notification)
    {
        $notification = $this->notificationRepository->getItem($notification);
        return $notification;
    }
    public function saveAlert(Request $request, ?Notification $notification): void
    {
        $this->notificationRepository->saveAlert($request,$notification);
    }
    public function save(Request $request, ?Notification $notification): void
    {
        $this->saveAlert($request,$notification);
        $users = $request->user_id ? [$request->user_id] : $this->userService->get($request)->pluck('id')->toArray();
        foreach ($users as $user_id)
        {
            $request->merge(['user_id'=>$user_id]);
            $this->notificationRepository->save($request,$notification);
        }
    }
    public function destroy(Request $request, int $id): array
    {
        $response = $this->notificationRepository->destroy($request,$id);
        return $response;
    }
    public function destroyAll(Request $request): array
    {
        $response = $this->notificationRepository->destroyAll($request);
        return $response;
    }
    public function restore(Request $request, int $id): array
    {
        $response = $this->notificationRepository->restore($request,$id);
        return $response;
    }
    public function restoreAll(Request $request): array
    {
        $response = $this->notificationRepository->restoreAll($request);
        return $response;
    }
    public function orderChangeStatus(Request $request,$requestData) :void
    {
        $status = 'orders.statuses.'.$requestData['status'];
        $user_id = $requestData['user_id'];
        $target_id = $requestData['order_id'];
        switch ($requestData['status'])
        {
            case OrderStatusEnum::ACCREDITATION->value :
                $title_ar = 'تم تغيير طلبك رقم ' . $requestData['order_id'] . ' الي جاري التحضير ';
                $title_en = 'Your order No. ' . $requestData['order_id'] . ' has been changed to Accreditation';
                $description_ar =  'تم تغيير طلبك رقم ' . $requestData['order_id'] . ' الي جاري التحضير ';
                $description_en = 'Your order No. ' . $requestData['order_id'] . ' has been changed to Accreditation';
                break;
            case OrderStatusEnum::PREPARING->value :
                $title_ar = 'تم تغيير طلبك رقم ' . $requestData['order_id'] . ' الي جاري التجهيز ';
                $title_en = 'Your order No. ' . $requestData['order_id'] . ' has been changed to Preparing';
                $description_ar =  'تم تغيير طلبك رقم ' . $requestData['order_id'] . ' الي جاري التجهيز ';
                $description_en = 'Your order No. ' . $requestData['order_id'] . ' has been changed to Preparing';
                break;
            case OrderStatusEnum::SHIPPING->value :
                $title_ar = 'تم تغيير طلبك رقم ' . $requestData['order_id'] . ' الي جاري الشحن ';
                $title_en = 'Your order No. ' . $requestData['order_id'] . ' has been changed to Shipping';
                $description_ar =  'تم تغيير طلبك رقم ' . $requestData['order_id'] . ' الي جاري الشحن ';
                $description_en = 'Your order No. ' . $requestData['order_id'] . ' has been changed to Shipping';
                break;
            case OrderStatusEnum::RECEIVED->value :
                $title_ar = 'تم تغيير طلبك رقم ' . $requestData['order_id'] . ' الي تم التوصيل ';
                $title_en = 'Your order No. ' . $requestData['order_id'] . ' has been changed to Delivered';
                $description_ar =  'تم تغيير طلبك رقم ' . $requestData['order_id'] . ' الي تم التوصيل ';
                $description_en = 'Your order No. ' . $requestData['order_id'] . ' has been changed to Delivered';
                break;
            default:
                $title_ar = 'تم انشاء طلب جديد برقم ' . $requestData['order_id'];
                $title_en = 'New order had been created with No. ' . $requestData['order_id'];
                $description_ar = 'تم انشاء طلب جديد برقم ' . $requestData['order_id'];
                $description_en = 'New order had been created with No. ' . $requestData['order_id'];

        }
        $request->merge(['status'=>$status,'user_id'=>$user_id,'target_id'=>$target_id,'title_ar'=>$title_ar,'title_en'=>$title_en,'description_ar'=>$description_ar,'description_en'=>$description_en]);
        $this->save($request,null);
    }

    public function deleteAllUser(Request $request) :void
    {
        $this->notificationRepository->deleteAllUser($request);
    }
}
