<?php

namespace Modules\Type\Services;

use Illuminate\Http\Request;
use Modules\Type\Enums\TypeBoolEnum;
use Modules\Type\Interfaces\TypeRepositoryInterface;
use Modules\Type\Models\Type;

class TypeService
{
    private TypeRepositoryInterface $typeRepository ;

    public function __construct(TypeRepositoryInterface $typeRepository)
    {
        $this->typeRepository = $typeRepository;
    }

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

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

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

    public function getItem($id)
    {
        $type = $this->typeRepository->getItem($id);

        return $type;
    }

    public function getItemTrashed($id)
    {
        $type = $this->typeRepository->getItemTrashed($id);
        return $type;
    }

    public function create(array $request): Type
    {
        // if($request['default_type'] == TypeBoolEnum::TRUE->value)
        // {
        //     $this->unDefaultTypes();
        // }
        $type = $this->typeRepository->create($request);
        $this->typeRepository->saveData($request,$type->id);
        return $type;
    }
    public function update(array $request,int $id): Type
    {
        // if($request['default_type'] == TypeBoolEnum::TRUE->value)
        // {
        //     $this->unDefaultTypes();
        // }

        $type = $this->checkItem($id);


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

    public function unDefaultTypes()
    {
        $city = $this->defaultType();
        if ($city)
        {
            $this->typeRepository->unDefaultTypes($city);
        }
    }

    public function defaultType()
    {
        return $this->typeRepository->defaultType();
    }
}
