<?php

namespace Modules\DealStatus\Services;

use Illuminate\Http\Request;
use Modules\DealStatus\Interfaces\DealStatusRepositoryInterface;
use Modules\DealStatus\Models\DealStatus;
use Modules\MainService\Views\ViewFactory;

class DealStatusService
{
    public function __construct(
        private DealStatusRepositoryInterface $dealStatusRepository,
        private ViewFactory $viewFactory
    ){}

    public function renderView($request = [])
    {
        $request['deal_statuses'] = $this->get($request);
        return $this->render($request);
    }

    public function render($request = [])
    {
        return $this->viewFactory
            ->render($request['view'])
            ->renderView($request);
    }

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

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

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


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

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

    public function getItem($id)
    {
        $dealStatus = $this->dealStatusRepository->getItem($id);

        return $dealStatus;
    }

    public function getItemTrashed($id)
    {
        $dealStatus = $this->dealStatusRepository->getItemTrashed($id);
        return $dealStatus;
    }

    public function create(array $request): DealStatus
    {
        $dealStatus = $this->dealStatusRepository->create($request);
        $this->dealStatusRepository->saveData($request,$dealStatus->id);
        return $dealStatus;
    }
    public function update(array $request,int $id): DealStatus
    {
        $dealStatus = $this->checkItem($id);


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