<?php
/// file: app/Models/JournalPost.php

declare(strict_types=1);

namespace App\Models;

use App\Core\Model;
use PDO;

final class JournalPost extends Model
{
    protected static function table(): string
    {
        return 'journal_posts';
    }

    /** @return array<string, mixed>|null */
    public static function findBySlug(string $slug): ?array
    {
        return parent::findBy('slug', $slug);
    }

    /** @return list<array<string, mixed>> */
    public static function published(int $limit = 50): array
    {
        $st = self::pdo()->prepare(
            'SELECT * FROM journal_posts WHERE status = ? ORDER BY published_at DESC, id DESC LIMIT ' . (int) $limit
        );
        $st->execute(['published']);
        return $st->fetchAll(PDO::FETCH_ASSOC) ?: [];
    }

    /** @return list<array<string, mixed>> */
    public static function related(string $category, int $excludeId, int $limit = 3): array
    {
        $st = self::pdo()->prepare(
            'SELECT * FROM journal_posts WHERE status = ? AND id != ? AND category = ? ORDER BY published_at DESC LIMIT ' . (int) $limit
        );
        $st->execute(['published', $excludeId, $category]);
        return $st->fetchAll(PDO::FETCH_ASSOC) ?: [];
    }
}
