<?php

namespace Modules\Coupon\Services;

use Illuminate\Http\Request;
use Modules\Coupon\Enums\CouponDiscountStatusEnum;
use Modules\Coupon\Interfaces\CouponRepositoryInterface;
use Modules\Coupon\Models\Coupon;
use Modules\Order\Models\Order;
use Modules\Order\Services\OrderService;
use Modules\User\Services\AccountService;

class CouponService
{
    private CouponRepositoryInterface $couponRepository ;
    private AccountService $accountService ;

    public function __construct(CouponRepositoryInterface $couponRepository,AccountService $accountService)
    {
        $this->couponRepository = $couponRepository;
        $this->accountService = $accountService;
    }

    public function get(Request $request)
    {
        $categories = $this->couponRepository->get($request);
        return $categories;
    }
    public function getItem(Coupon $coupon)
    {
        $coupon = $this->couponRepository->getItem($coupon);
        return $coupon;
    }
    public function getItemByCode(string $coupon)
    {
        $coupon = $this->couponRepository->getItemByCode($coupon);
        return $coupon;
    }
    public function save(Request $request, ?Coupon $coupon): Coupon
    {
        $coupon = $this->couponRepository->save($request,$coupon);
        return $coupon;
    }

    public function products(Coupon $coupon)
    {
        $products = $this->couponRepository->products($coupon);
        return $products;
    }

    public function destroy(Request $request, int $id): array
    {
        $response = $this->couponRepository->destroy($request,$id);
        return $response;
    }
    public function destroyAll(Request $request): array
    {
        $response = $this->couponRepository->destroyAll($request);
        return $response;
    }
    public function restore(Request $request, int $id): array
    {
        $response = $this->couponRepository->restore($request,$id);
        return $response;
    }
    public function restoreAll(Request $request): array
    {
        $response = $this->couponRepository->restoreAll($request);
        return $response;
    }

    public function discount(string $coupon , $total)
    {
        $discount = 0 ;
        $coupon = $this->couponRepository->getItemByCode($coupon);
        if ($coupon)
        {
            $request = request();
            $request->merge(['coupon_id'=>$coupon->id]);
            $account = $this->accountService->getAccount();
            $count_user_usage = Order::where('status',$account['status'])->where('user_id',$account['user']->id)->where('coupon_id',$coupon->id)->count();
            if ($coupon->count > 0 && $count_user_usage <= $coupon->user_count && $coupon->start_date <= date('Y-m-d') && $coupon->end_date >= date('Y-m-d'))
            {
                if ($coupon->discount_status == CouponDiscountStatusEnum::PERCENTAGE->value)
                {
                    $discount = $total * ($coupon->discount/100);
                }
                else
                {
                    $discount = ($coupon->discount > $total) ? $total : $coupon->discount;
                }
            }
        }
        return $discount;
    }
    public function decrease(int $coupon_id) :void
    {
        $coupon = Coupon::find($coupon_id);
        $coupon->update(['count'=>($coupon->count - 1)]);
    }
}
