<?php

namespace App\Models;

use App\Core\Model;

class HalamanModel extends Model
{
    protected string $table = 'halaman_statis';

    public function getBySlug(string $slug): array|false
    {
        $stmt = $this->db->prepare(
            "SELECT * FROM {$this->table} WHERE slug = :slug AND is_active = 1 LIMIT 1"
        );
        $stmt->execute(['slug' => $slug]);

        return $stmt->fetch();
    }

    public function slugExists(string $slug, ?int $excludeId = null): bool
    {
        if ($excludeId) {
            $stmt = $this->db->prepare("SELECT id FROM {$this->table} WHERE slug = :slug AND id != :id LIMIT 1");
            $stmt->execute(['slug' => $slug, 'id' => $excludeId]);
        } else {
            $stmt = $this->db->prepare("SELECT id FROM {$this->table} WHERE slug = :slug LIMIT 1");
            $stmt->execute(['slug' => $slug]);
        }

        return (bool) $stmt->fetch();
    }
}
