<?php

namespace Modules\Refund\Services;

use Modules\Refund\Enums\RefundStatusEnum;
use Modules\Refund\Interfaces\RefundRepositoryInterface;
use Modules\Refund\Models\Refund;

class RefundService
{
    private RefundRepositoryInterface $refundRepository ;

    public function __construct(RefundRepositoryInterface $refundRepository)
    {
        $this->refundRepository = $refundRepository;
    }

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

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

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

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

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

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

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

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

        if (@$request['date_to'])
        {
            $builder->whereDate('Refunds.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']) {
            $refunds= $builder->paginate(50);
        }else{
            $refunds=$builder->get();
        }
        return $refunds;
    }
    public function checkItem($id)
    {
        $refund = $this->refundRepository->checkItem($id);
        return $refund;
    }

    public function getItem($id)
    {
        return $this->refundRepository->getItem($id);
    }

    public function getItemTrashed($id)
    {
        $refund = $this->refundRepository->getItemTrashed($id);
        return $refund;
    }

    public function create(array $request): Refund
    {
        $request['status'] = RefundStatusEnum::HOLD->value;
        $request['uuid'] = $this->UUID(25);
        $refund = $this->refundRepository->create($request);
        $this->createStatus([
            'status' => RefundStatusEnum::HOLD->value,
            'refund_id' => $refund->id
        ]);
        return $refund;
    }
    public function createStatus(array $request):void
    {
        $this->refundRepository->createStatus($request);
    }

    public function UUID($length = 10)
    {
        $number = substr(str_shuffle(str_repeat(
            implode('', array_map('chr', array_merge(
                range(48, 57),
                range(65, 90),
                range(97, 122)
            ))), ceil($length / 62))),
            0, $length
        );

        if ($this->checkUUIDExists($number)) {
            return $this->UUID($length);
        }

        return $number;
    }
    public function checkUUIDExists($number)
    {
        return $this->refundRepository->checkUUIDExists($number);
    }

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