<?php

namespace Modules\Sector\Services;

use Illuminate\Http\Request;
use Modules\Sector\Interfaces\SectorRepositoryInterface;
use Modules\Sector\Models\Sector;
use Modules\MainService\Views\ViewFactory;

class SectorService
{
    public function __construct(
        private SectorRepositoryInterface $sectorRepository,
        private ViewFactory $viewFactory
    ){}

    public function renderView($request = [])
    {
        $request['sectors'] = $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->sectorRepository->get($request);

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

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

        if (@$request['employee_id'])
        {
            $builder->whereHas('users', function ($query) use ($request) {
                $query->where('user_id', '=', $request['employee_id']);
            });
        }

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

        if (@$request['date_to'])
        {
            $builder->whereDate('sectors.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)
    {
        $sector = $this->sectorRepository->checkItem($id);
        return $sector;
    }

    public function getItem($id)
    {
        $sector = $this->sectorRepository->getItem($id);

        return $sector;
    }

    public function getItemTrashed($id)
    {
        $sector = $this->sectorRepository->getItemTrashed($id);
        return $sector;
    }

    public function create(array $request): Sector
    {
        $sector = $this->sectorRepository->create($request);
        $this->sectorRepository->saveData($request,$sector->id);
        return $sector;
    }
    public function update(array $request,int $id): Sector
    {
        $sector = $this->checkItem($id);
        $sector = $this->sectorRepository->update($request,$sector);
        if(@$request['title_en'])
        {
            $this->sectorRepository->saveData($request,$sector->id);
        }
        return $sector;
    }

    public function updateLock($id)
    {
        $sector = $this->checkItem($id);
        $this->update(['lock'=>($sector->lock?0:1)],$id);
    }

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