<?php

namespace Modules\Coupon\Services;

use Illuminate\Http\Request;
use Modules\Coupon\Enums\DiscountStatusEnum;
use Modules\Coupon\Interfaces\CouponRepositoryInterface;
use Modules\Coupon\Models\Coupon;

class CouponService
{
    private CouponRepositoryInterface $couponRepository ;

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

    public function get(array $request)
    {
        $builder = $this->couponRepository->get($request);

        if (@$request['title'])
        {
            $builder->where('sd.title', 'LIKE', '%'.$request['title'].'%');
        }

        if (@$request['seller'])
        {
            $builder->whereNotNull('coupons.user_id');
        }

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

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

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

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

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

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

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

        if (@$request['date_to'])
        {
            $builder->whereDate('coupons.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']) {
            $coupons= $builder->paginate(50);
        }else{
            $coupons=$builder->cursor();
        }
        return $coupons;
    }
    public function checkItem($id)
    {
        $coupon = $this->couponRepository->checkItem($id);
        return $coupon;
    }

    public function getItem($id)
    {
        $coupon = $this->checkItem($id);
        $couponDescription = $this->couponRepository->getItem($id);
        foreach ($couponDescription as $row) {
            $coupon[$row->local]= $row;
        }
        return $coupon;
    }

    public function getItemByCode(string $code)
    {
        return $this->couponRepository->getItemByCode($code);
    }

    public function discount(array $request)
    {
        $discount = 0;
        $coupon = $this->getItemByCode($request['coupon']);
        if ($coupon)
        {
            if ($coupon->status_discount == DiscountStatusEnum::PERCENTAGE->value)
            {
                $discount = $request['total'] * ( $coupon->discount / 100);
            }
            else
            {
                $discount = $coupon->discount;
            }
        }

        return $discount;
    }

    public function getItemTrashed($id)
    {
        $coupon = $this->couponRepository->getItemTrashed($id);
        return $coupon;
    }

    public function create(array $request): Coupon
    {
        $coupon = $this->couponRepository->create($request);
        $this->couponRepository->saveData($request,$coupon->id);
        return $coupon;
    }
    public function update(array $request,int $id): Coupon
    {
        $coupon = $this->checkItem($id);


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

    public function decrease($coupon_id)
    {
        $coupon = $this->getItem($coupon_id);
        $this->update([
            'count'=>($coupon->count - 1)
        ],$coupon);
    }
}
