<?php

namespace Modules\Order\Services;

use Carbon\Carbon;
use Modules\Order\Enums\OrderStatusEnum;
use Modules\Order\Interfaces\OrderRepositoryInterface;

class OrderService
{

    public function __construct(
        private OrderRepositoryInterface $orderRepository,
    ){}

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

        if (isset($request['name']))
        {
            $builder->where('name','LIKE', '%'.$request['name'].'%');
        }

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

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


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

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

        if (@$request['reservation_date_to'])
        {
            $builder->whereDate('reservation_date', '<=', $request['reservation_date_to']);
        }

        if (@$request['reservation_date_from'])
        {
            $builder->whereDate('reservation_date', '>=', $request['reservation_date_from']);
        }

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

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


        if (@$request['paginate']) {
            $orders = $builder->paginate(is_numeric($request['paginate'] ? $request['paginate'] : 50));
        }else{
            $orders = $builder->get();
        }
        return $orders;
    }

    public function getItem(int $id)
    {
        $order = $this->orderRepository->getItem($id);
        return $order;
    }

    public function getTimes($date)
    {
        $startTime = settings('start_time');
        $endTime = settings('end_time');
        $now = Carbon::now();
        $start = Carbon::parse("$date $startTime");
        $end = Carbon::parse("$date $endTime");

        if ($end < $start) {
            $end->addDay();
        }

        $timeSlots = [];
        while ($start < $end) {

            if ($start >= $now)
            {
                $timeSlots[] = $start->format('H:i');
            }

            $start->addHour();
        }

        return $timeSlots;
    }

    public function create(array $request)
    {
        $order = $this->orderRepository->create($request);
        $this->addFeatures($request,$order->id);
        $this->addAttributes($request,$order->id);
    }

    public function addFeatures(array $request,int $order_id)
    {
        $type = ($request['status'] == OrderStatusEnum::PACKAGE->value ? 'package' : 'service') . '_ids';
        if(isset($request[$type]))
        {
            foreach ($request[$type] as $feature_id) {
                $content['order_id'] = $order_id;
                $content['featureable_id'] = $feature_id;
                $content['featureable_type'] = $request['status'];
                $this->orderRepository->addFeatures($content);
            }
        }
    }

    public function addAttributes(array $request,int $order_id)
    {
        if(isset($request['additions']))
        {
            foreach ($request['additions'] as $attribute)
            {
                if ( count($attribute) == 2 )
                {
                    $attribute['order_id'] = $order_id;
                    $this->orderRepository->addAttributes($attribute);
                }
            }
        }
    }

    public function getItemTrashed(int $id)
    {
        $order = $this->orderRepository->getItemTrashed($id);
        return $order;
    }

    public function destroy(int $id)
    {
        $order = $this->getItem($id);
        $response = $this->orderRepository->destroy($order);
        return $response;
    }
    public function destroyAll(array $ids): array
    {
        $response = $this->orderRepository->destroyAll($ids);
        return $response;
    }
    public function restore(int $id): array
    {
        $order = $this->getItem($id);
        $response = $this->orderRepository->restore($order);
        return $response;
    }
    public function restoreAll(array $ids): array
    {
        $response = $this->orderRepository->restoreAll($ids);
        return $response;
    }
}
