<?php

namespace Modules\Subscription\Services;

use Illuminate\Http\Request;
use Modules\Notification\Services\NotificationService;
use Modules\Payment\Services\PaymentService;
use Modules\PaymentLog\Services\PaymentLogService;
use Modules\Subscription\Interfaces\SubscriptionRepositoryInterface;
use Modules\Subscription\Models\Subscription;
use Modules\User\Services\UserService;

class SubscriptionService
{
    private SubscriptionRepositoryInterface $subscriptionRepository;
    private PaymentLogService $paymentLogService;
    private UserService $userService;
    private NotificationService $notificationService;


    public function __construct(
        SubscriptionRepositoryInterface $subscriptionRepository,
        PaymentLogService $paymentLogService,
        UserService $userService,
        NotificationService $notificationService,
    ) {
        $this->subscriptionRepository = $subscriptionRepository;
        $this->paymentLogService = $paymentLogService;
        $this->userService = $userService;
        $this->notificationService = $notificationService;
    }

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


        if (@$request['user']) {
            $builder->whereHas('user', function ($query) use ($request) {
                $query->where('name', 'LIKE', '%' . $request['user'] . '%');
            });
        }

        if (@$request['phone']) {
            $builder->whereHas('user', function ($query) use ($request) {
                $query->where('phone', $request['phone']);
            });
        }

        if (@$request['package_id']) {
            $builder->where('package_id', $request['package_id']);
        }
        if (@$request['user_id']) {
            $builder->where('user_id', $request['user_id']);
        }

        if (@$request['points']) {
            $builder->where('points', $request['points']);
        }

        if (@$request['remain_points']) {
            $builder->where('remain_points', $request['remain_points']);
        }

        if (@$request['package_cost']) {
            $builder->whereHas('package', function ($query) use ($request) {
                $query->where('cost', $request['package_cost'])->orWhere('cost_discount', $request['package_cost']);
            });
        }

        if (isset($request['type'])) {
            if ($request['type'] == 0) {
                $builder->where('remain_points', 0);
            } else {
                $builder->where('remain_points', '>', 0);
            }
        }


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

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

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

        if (@$request['orderBy']) {
            $builder->orderBy($request['orderBy']['key'], $request['orderBy']['value']);
        } else {
            $builder->orderBy('id', 'DESC');
        }

        if (@$request['paginate']) {
            $subscriptions = $builder->paginate(50);
        } elseif (@$request['api']) {
            $subscriptions = $builder;
        } else {
            $subscriptions = $builder->cursor();
        }
        return $subscriptions;
    }
    public function checkItem($id)
    {
        $subscription = $this->subscriptionRepository->checkItem($id);
        return $subscription;
    }

    public function getItem($id)
    {
        $subscription = $this->checkItem($id);

        return $subscription;
    }

    public function getItemTrashed($id)
    {
        $subscription = $this->subscriptionRepository->getItemTrashed($id);
        return $subscription;
    }
    public function createNewSubscription(array $request)
    {
        $id = $request['user_id'];
        $user = $this->userService->getItem($id);
        $credentials['package_id'] = $request['package_id'];
        $credentials['user_id'] = $id;
        $credentials['total'] = $request['total'];
        $credentials['remain_points'] = $request['remain_points'];
        $credentials['points'] = $request['points'];
        $subscription = $this->create($credentials);
        $this->paymentLogService->create([
            'status' => 1,
            'price' => $request['total'],
            'user_id' => $id,
            'subscription_id' => $subscription->id
        ]);
        $this->notificationService->create([
            'target' => $id,
            'user_id' => $id,
            'title_ar' => 'الاشتراك في باقة',
            'title_en' => 'Subscribe to package',
            'description_ar' => 'تم اشتراكك بنجاح',
            'description_en' => 'You have successfully subscribed',

        ]);
        $this->userService->update(['points' => $user->points + $request['points'], 'unread_notification' => $user->unread_notification + 1], $id);
        return $subscription;
    }
    public function create(array $request): Subscription
    {
        $subscription = $this->subscriptionRepository->create($request);
        return $subscription;
    }
    public function update(array $request, int $id): Subscription
    {
        $subscription = $this->checkItem($id);


        $subscription = $this->subscriptionRepository->update($request, $subscription);
        return $subscription;
    }
    public function destroy(int $id): void
    {
        $subscription = $this->checkItem($id);
        if ($subscription) {
            $this->subscriptionRepository->destroy($subscription);
        } else {
            $subscription = $this->getItemTrashed($id);
            $this->subscriptionRepository->restore($subscription);
        }
    }
    public function destroyAll(array $request): void
    {
        if ($request['status'] == 1) {
            $this->subscriptionRepository->destroyAll($request['ids']);
        } else {
            $this->subscriptionRepository->restoreAll($request['ids']);
        }
    }
}
