<?php

namespace App\Repositories;

use App\Core\Database;
use PDO;

abstract class BaseRepository
{
    protected PDO $db;

    public function __construct()
    {
        $this->db = Database::connection();
    }

    protected function paginate(string $sql, array $params = []): array
    {
        $page = max(1, (int) ($_GET['page'] ?? 1));
        $perPage = min(100, max(5, (int) ($_GET['per_page'] ?? 20)));
        $offset = ($page - 1) * $perPage;
        $stmt = $this->db->prepare($sql . " LIMIT $perPage OFFSET $offset");
        $stmt->execute($params);

        return [
            'data' => $stmt->fetchAll(),
            'meta' => ['page' => $page, 'per_page' => $perPage],
        ];
    }
}
