<?php

namespace App\Repositories;

use App\Interfaces\NewsRepositoryInterface;
use App\Models\News;
use Illuminate\Pagination\LengthAwarePaginator;

class NewsRepository implements NewsRepositoryInterface
{
    public function __construct(private readonly News $model) {}

    public function paginate(int $perPage = 15, ?string $category = null): LengthAwarePaginator
    {
        $query = $this->model
            ->with('relatedTeam')
            ->where('is_published', true)
            ->orderByDesc('created_at');

        if ($category) {
            $query->where('category', $category);
        }

        return $query->paginate($perPage);
    }

    public function findById(int $id): ?News
    {
        return $this->model->with('relatedTeam')->find($id);
    }

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

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

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