<?php

namespace App\Repositories;

use App\Interfaces\TeamRepositoryInterface;
use App\Models\Team;
use Illuminate\Database\Eloquent\Collection;

class TeamRepository implements TeamRepositoryInterface
{
    public function __construct(private readonly Team $model) {}

    public function all(): Collection
    {
        return $this->model->orderBy('group_name')->orderBy('name')->get();
    }

    public function findById(int $id): ?Team
    {
        return $this->model->find($id);
    }

    public function findByCode(string $code): ?Team
    {
        return $this->model->where('code', strtoupper($code))->first();
    }

    public function create(array $data): Team
    {
        return $this->model->create($data);
    }

    public function update(int $id, array $data): bool
    {
        return $this->model->where('id', $id)->update($data) > 0;
    }

    public function getCommunitySupport(): Collection
    {
        return $this->model
            ->withCount('supporters')
            ->orderByDesc('supporters_count')
            ->get();
    }
}
