<?php

namespace Modules\Team\Services;

use Modules\Team\Interfaces\TeamRepositoryInterface;
use Modules\Team\Models\Team;

class TeamService
{
    public function __construct(
        private TeamRepositoryInterface $teamRepository
    ) {
    }

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

        if (@$request['name']) {
            $builder->where('td.name', 'LIKE', '%' . $request['name'] . '%');
        }
        if (@$request['job']) {
            $builder->where('td.job', 'LIKE', '%' . $request['job'] . '%');
        }
        if (@$request['date_from']) {
            $builder->whereDate('teams.created_at', '>=', $request['date_from']);
        }
        if (@$request['date_to']) {
            $builder->whereDate('teams.created_at', '<=', $request['date_to']);
        }
        if (isset($request['status']) && @$request['status'] == 0) {
            $builder->onlyTrashed();
        }
        if (@$request['orderBy']) {
            $builder->orderBy($request['orderBy']['key'], $request['orderBy']['value']);
        } else {
            $builder->orderBy('id', 'DESC');
        }

        return @$request['paginate'] ? $builder->paginate(50) : $builder->cursor();
    }

    public function checkItem(int $id)
    {
        return $this->teamRepository->checkItem($id);
    }

    public function getItem(int $id)
    {
        $team = $this->checkItem($id);
        $descriptions = $this->teamRepository->getItem($id);
        foreach ($descriptions as $row) {
            $team[$row->local] = $row;
        }
        return $team;
    }

    public function getItemTrashed(int $id)
    {
        return $this->teamRepository->getItemTrashed($id);
    }

    public function create(array $request): Team
    {
        $team = $this->teamRepository->create($request);
        $this->teamRepository->saveData($request, $team->id);
        return $team;
    }

    public function update(array $request, int $id): Team
    {
        $team = $this->checkItem($id);
        $team = $this->teamRepository->update($request, $team);
        $this->teamRepository->saveData($request, $team->id);
        return $team;
    }

    public function destroy(int $id): void
    {
        $team = $this->checkItem($id);
        if ($team) {
            $this->teamRepository->destroy($team);
        }
    }

    public function restore(int $id): void
    {
        $team = $this->getItemTrashed($id);
        if ($team) {
            $this->teamRepository->restore($team);
        }
    }

    public function forceDelete(int $id): void
    {
        $team = $this->getItemTrashed($id);
        if ($team) {
            $this->teamRepository->forceDelete($team);
        }
    }

    public function forceDeleteAll(array $ids): void
    {
        $this->teamRepository->forceDeleteAll($ids);
    }

    public function destroyAll(array $request): void
    {
        if ($request['status'] == 1) {
            $this->teamRepository->destroyAll($request['ids']);
        } else {
            $this->teamRepository->restoreAll($request['ids']);
        }
    }
}

