<?php

namespace Modules\PaymentLog\Services;

use Illuminate\Http\Request;
use Modules\Notification\Services\NotificationService;
use Modules\PaymentLog\Enums\PaymentLogStatusEnum;
use Modules\PaymentLog\Interfaces\PaymentLogRepositoryInterface;
use Modules\PaymentLog\Models\PaymentLog;
use Modules\User\Services\UserService;

class PaymentLogService
{
    private PaymentLogRepositoryInterface $paymentLogRepository;

    public function __construct(
        PaymentLogRepositoryInterface $paymentLogRepository,
        private NotificationService $notificationService,
        private UserService $userService,
    ) {
        $this->paymentLogRepository = $paymentLogRepository;
    }

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

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

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

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

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

        if (@$request['date_to']) {
            $builder->whereDate('cities.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']) {
            $paymentLogs = $builder->paginate(50);
        } elseif (@$request['api']) {
            $paymentLogs = $builder;
        } else {
            $paymentLogs = $builder->cursor();
        }
        return $paymentLogs;
    }

    public function getTotal($id)
    {
        $payment_logs = $this->get(['canteen_id' => $id]);
        $total = 0;
        foreach ($payment_logs as $row) {
            if ($row->paid_status != PaymentLogStatusEnum::RETURN ->value) {
                if ($row->status == PaymentLogStatusEnum::PLUS->value) {
                    $total += $row->price;
                } else {
                    $total -= $row->price;
                }
            }
        }
        return $total;
    }

    public function returnMoney($id)
    {
        $total = $this->getTotal($id);
        $data =
            [
                'price' => $total,
                'canteen_id' => $id,
                'status' => PaymentLogStatusEnum::RETURN ->value,
                'description' => ''
            ];
        $this->create($data);
    }

    public function checkItem($id)
    {
        $paymentLog = $this->paymentLogRepository->checkItem($id);
        return $paymentLog;
    }

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

        return $paymentLog;
    }

    public function getItemTrashed($id)
    {
        $paymentLog = $this->paymentLogRepository->getItemTrashed($id);
        return $paymentLog;
    }

    public function create(array $request): PaymentLog
    {


        $paymentLog = $this->paymentLogRepository->create($request);
        if (@$request['canteen_id']) {
            $user = $this->userService->canteenUser($request['canteen_id']);
            if ($user) {
                $data['target'] = $user->id;
                $data['user_id'] = $user->id;
                if ($request['status'] == PaymentLogStatusEnum::PLUS->value) {
                    $data['title_ar'] = ' إضافة رصيد';
                    $data['title_en'] = 'Add balance';
                    $data['description_ar'] = ' تم إضافة رصيد من الإدارة بقيمة (' . $request['price'] . '). شكراً لتعاونكم';
                    $data['description_en'] = 'A balance has been added from the administration with a value of (' . $request['price'] . '). Thank you for your cooperation';
                }
                if ($request['status'] == PaymentLogStatusEnum::MINUS->value) {
                    $data['title_ar'] = 'خصم رصيد';
                    $data['title_en'] = 'Deduct balance';
                    $data['description_ar'] = ' تم خصم رصيد من الإدارة بقيمة (' . $request['price'] . '). شكراً لتعاونكم';
                    $data['description_en'] = 'A balance has been deducted from the administration with a value of (' . $request['price'] . '). Thank you for your cooperation';
                }
                if ($request['status'] == PaymentLogStatusEnum::RETURN ->value) {
                    $data['title_ar'] = 'رد رصيد';
                    $data['title_en'] = 'Return balance';
                    $data['description_ar'] = 'تم رد رصيد مستحق بقيمة (' . $request['price'] . '). شكراً لتعاونكم';
                    $data['description_en'] = 'A balance has been returned with a value of (' . $request['price'] . '). Thank you for your cooperation';
                }

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