<?php

namespace Models;

class CprMonitoringModel
{
    private \PDO $pdo;

    public function __construct()
    {
        if (!isset($GLOBALS['pdo']) || !$GLOBALS['pdo'] instanceof \PDO) {
            throw new \RuntimeException('Global PDO connection not initialized');
        }

        $this->pdo = $GLOBALS['pdo'];
    }

    public function getById(int $id): ?array
    {
        $stmt = $this->pdo->prepare(
            'SELECT
                cpr_monitoring_id AS id,
                cpr_id,
                cpr_monitoring_preview AS preview,
                cpr_monitoring_description AS description,
                cpr_monitoring_link AS link
             FROM "cpr_monitoring"
             WHERE cpr_monitoring_id = :id
             LIMIT 1'
        );
        $stmt->execute(['id' => $id]);
        $row = $stmt->fetch(\PDO::FETCH_ASSOC);

        return $row ?: null;
    }

    public function listByCprId(int $cprId): array
    {
        $stmt = $this->pdo->prepare(
            'SELECT
                cpr_monitoring_id AS id,
                cpr_id,
                cpr_monitoring_preview AS preview,
                cpr_monitoring_description AS description,
                cpr_monitoring_link AS link
             FROM "cpr_monitoring"
             WHERE cpr_id = :cpr_id
             ORDER BY cpr_monitoring_id DESC'
        );
        $stmt->execute(['cpr_id' => $cprId]);

        return $stmt->fetchAll(\PDO::FETCH_ASSOC);
    }

    public function create(int $cprId, bool $preview, string $description, string $link): array
    {
        $stmt = $this->pdo->prepare(
            'INSERT INTO "cpr_monitoring" (
                cpr_id,
                cpr_monitoring_preview,
                cpr_monitoring_description,
                cpr_monitoring_link
             )
             VALUES (:cpr_id, :preview, :description, :link)
             RETURNING cpr_monitoring_id'
        );

        $stmt->execute([
            'cpr_id' => $cprId,
            'preview' => $preview,
            'description' => $description,
            'link' => $link,
        ]);

        $id = (int)$stmt->fetchColumn();

        return [
            'id' => $id,
            'cpr_id' => $cprId,
            'preview' => $preview,
            'description' => $description,
            'link' => $link,
        ];
    }

    public function update(int $id, ?bool $preview = null, ?string $description = null, ?string $link = null): ?array
    {
        $fields = [];
        $params = ['id' => $id];

        if ($preview !== null) {
            $fields[] = 'cpr_monitoring_preview = :preview';
            $params['preview'] = $preview;
        }
        if ($description !== null) {
            $fields[] = 'cpr_monitoring_description = :description';
            $params['description'] = $description;
        }
        if ($link !== null) {
            $fields[] = 'cpr_monitoring_link = :link';
            $params['link'] = $link;
        }

        if (!$fields) {
            return null;
        }

        $sql = 'UPDATE "cpr_monitoring" SET ' . implode(', ', $fields) . ' WHERE cpr_monitoring_id = :id';
        $stmt = $this->pdo->prepare($sql);
        $ok = $stmt->execute($params);
        if (!$ok) {
            return null;
        }

        return $this->getById($id);
    }

    public function delete(int $id): bool
    {
        $stmt = $this->pdo->prepare('DELETE FROM "cpr_monitoring" WHERE cpr_monitoring_id = :id');
        $stmt->execute(['id' => $id]);

        return $stmt->rowCount() > 0;
    }
}
