<?php

namespace App\Models;

use App\Core\Model;
use PDO;

class BeritaModel extends Model
{
    protected string $table = 'berita';

    public function getTerbaru(int $limit = 5): array
    {
        $stmt = $this->db->prepare(
            "SELECT b.*, k.nama AS kategori
             FROM {$this->table} b
             LEFT JOIN kategori_berita k ON b.kategori_id = k.id
             WHERE b.status = 'terbit'
             ORDER BY b.tanggal DESC
             LIMIT :limit"
        );
        $stmt->bindValue(':limit', $limit, PDO::PARAM_INT);
        $stmt->execute();

        return $stmt->fetchAll();
    }

    /**
     * Semua berita (termasuk draft) untuk panel admin.
     */
    public function getAllAdmin(): array
    {
        $stmt = $this->db->query(
            "SELECT b.*, k.nama AS kategori
             FROM {$this->table} b
             LEFT JOIN kategori_berita k ON b.kategori_id = k.id
             ORDER BY b.tanggal DESC"
        );

        return $stmt->fetchAll();
    }

    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();
    }

    public function countTerbit(): int
    {
        $stmt = $this->db->query("SELECT COUNT(*) AS total FROM {$this->table} WHERE status = 'terbit'");
        return (int) $stmt->fetch()['total'];
    }

    /**
     * Berita terbit dengan pagination untuk halaman publik /berita
     */
    public function getPaginated(int $page = 1, int $perPage = 9): array
    {
        $offset = max(0, ($page - 1) * $perPage);

        $stmt = $this->db->prepare(
            "SELECT b.*, k.nama AS kategori
             FROM {$this->table} b
             LEFT JOIN kategori_berita k ON b.kategori_id = k.id
             WHERE b.status = 'terbit'
             ORDER BY b.tanggal DESC
             LIMIT :limit OFFSET :offset"
        );
        $stmt->bindValue(':limit', $perPage, \PDO::PARAM_INT);
        $stmt->bindValue(':offset', $offset, \PDO::PARAM_INT);
        $stmt->execute();

        return $stmt->fetchAll();
    }

    public function tambahDilihat(int $id): void
    {
        $stmt = $this->db->prepare("UPDATE {$this->table} SET dilihat = dilihat + 1 WHERE id = :id");
        $stmt->execute(['id' => $id]);
    }

    public function getBySlug(string $slug): array|false
    {
        $stmt = $this->db->prepare(
            "SELECT b.*, k.nama AS kategori
             FROM {$this->table} b
             LEFT JOIN kategori_berita k ON b.kategori_id = k.id
             WHERE b.slug = :slug AND b.status = 'terbit'
             LIMIT 1"
        );
        $stmt->execute(['slug' => $slug]);

        return $stmt->fetch();
    }
}
