<?php

declare(strict_types=1);

namespace Core;

abstract class Repository
{
    protected Database $database;
    protected string $table;
    protected string $primaryKey = 'id';

    public function __construct(Database $database)
    {
        $this->database = $database;
    }

    public function find(int|string $id): ?array
    {
        return $this->database->first(
            "SELECT * FROM {$this->table} WHERE {$this->primaryKey} = :id",
            ['id' => $id]
        );
    }

    public function all(): array
    {
        return $this->database->query("SELECT * FROM {$this->table}");
    }

    public function delete(int|string $id): bool
    {
        return $this->database->execute(
            "DELETE FROM {$this->table} WHERE {$this->primaryKey} = :id",
            ['id' => $id]
        );
    }
}
