<?php
declare(strict_types=1);

namespace Core;

abstract class Model
{
    protected string $table = '';
    protected string $primaryKey = 'id';

    protected function db(): \PDO
    {
        return DB::conn('main');
    }

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

    public function all(string $orderBy = ''): array
    {
        $sql = "SELECT * FROM `{$this->table}`" . ($orderBy ? " ORDER BY {$orderBy}" : '');
        return DB::all($sql);
    }

    public function insert(array $data): string
    {
        return DB::insert($this->table, $data);
    }
}
