<?php
// Savit Research Prompt Details
// Ported from prompt-details.php

use CAE\Lib\Database;

$promptId = (int)($id ?? 0);
if (!$promptId) {
    echo "Invalid Prompt ID";
    exit();
}

$db = Database::getInstance();
$prompt = $db->fetch(
    "SELECT p.*, b.business_name FROM brand_prompts p 
     JOIN projects b ON p.brand_id = b.id 
     WHERE p.id = ?", 
    [$promptId]
);

if (!$prompt) {
    echo "Prompt not found";
    exit();
}

$pageTitle = 'Prompt Details — ' . $prompt['business_name'];
require __DIR__ . '/../layout/header.php';
?>

<?php
// Fetch the latest run
$latestRun = $db->fetch("SELECT * FROM prompt_runs WHERE prompt_id = ? ORDER BY created_at DESC LIMIT 1", [$promptId]);
$links = [];
if ($latestRun) {
    $links = $db->fetchAll("SELECT * FROM prompt_run_links WHERE run_id = ? ORDER BY rank ASC", [(int)$latestRun['id']]);
}
?>

<div class="mb-4">
    <a href="<?= BASE_URL ?>/projects/<?= $prompt['brand_id'] ?>/research" class="btn btn-link text-decoration-none p-0 mb-3 text-muted small">
        <i class="bi bi-arrow-left"></i> Back to Research
    </a>
    <div class="d-flex justify-content-between align-items-center">
        <h2 class="h3 fw-bold mb-1"><?= htmlspecialchars($prompt['prompt_text']) ?></h2>
        <span class="badge bg-primary rounded-pill px-3 py-2"><?= $prompt['visibility_score'] ?>% Visibility</span>
    </div>
    <p class="text-muted small">Project: <strong><?= htmlspecialchars($prompt['business_name']) ?></strong></p>
</div>

<div class="row g-4">
    <div class="col-lg-8">
        <!-- AI Response -->
        <div class="card shadow-sm border-0 rounded-4 mb-4">
            <div class="card-header bg-white border-bottom-0 pt-4 px-4">
                <h5 class="fw-bold mb-0"><i class="bi bi-robot text-primary me-2"></i> AI Response</h5>
            </div>
            <div class="card-body p-4">
                <?php if ($latestRun): ?>
                    <div class="ai-response-content bg-light p-4 rounded-3" style="line-height: 1.6;">
                        <?= nl2br(htmlspecialchars($latestRun['ai_response'])) ?>
                    </div>
                    <div class="mt-3 text-muted small d-flex justify-content-between">
                        <span>Model: <?= htmlspecialchars($latestRun['model']) ?></span>
                        <span>Date: <?= $latestRun['created_at'] ?></span>
                    </div>
                <?php else: ?>
                    <div class="text-center py-5">
                        <i class="bi bi-info-circle fs-1 text-muted opacity-25"></i>
                        <p class="mt-3 text-muted">No runs found for this prompt yet.</p>
                    </div>
                <?php endif; ?>
            </div>
        </div>

        <!-- Links/Citations -->
        <div class="card shadow-sm border-0 rounded-4">
            <div class="card-header bg-white border-bottom-0 pt-4 px-4">
                <h5 class="fw-bold mb-0"><i class="bi bi-link-45deg text-primary me-2"></i> Citations & Sources</h5>
            </div>
            <div class="card-body p-0">
                <div class="table-responsive">
                    <table class="table table-hover align-middle mb-0">
                        <thead class="bg-light">
                            <tr class="small text-muted text-uppercase">
                                <th class="ps-4">Rank</th>
                                <th>Title / URL</th>
                                <th class="pe-4 text-end">Domain</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php if (empty($links)): ?>
                                <tr><td colspan="3" class="text-center py-4 text-muted">No citations found</td></tr>
                            <?php else: ?>
                                <?php foreach ($links as $link): ?>
                                    <tr>
                                        <td class="ps-4 fw-bold text-muted"><?= $link['rank'] ?></td>
                                        <td>
                                            <div class="fw-medium text-dark"><?= htmlspecialchars($link['title']) ?></div>
                                            <a href="<?= htmlspecialchars($link['url']) ?>" target="_blank" class="small text-primary text-decoration-none text-truncate d-block" style="max-width: 400px;"><?= htmlspecialchars($link['url']) ?></a>
                                        </td>
                                        <td class="pe-4 text-end small text-muted"><?= htmlspecialchars($link['domain']) ?></td>
                                    </tr>
                                <?php endforeach; ?>
                            <?php endif; ?>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>

    <div class="col-lg-4">
        <!-- Stats Card -->
        <div class="card shadow-sm border-0 rounded-4 mb-4">
            <div class="card-body p-4">
                <h5 class="fw-bold mb-4">Run Analytics</h5>
                
                <div class="d-flex align-items-center mb-3">
                    <div class="bg-primary bg-opacity-10 text-primary p-3 rounded-3 me-3">
                        <i class="bi bi-chat-left-text fs-4"></i>
                    </div>
                    <div>
                        <div class="small text-muted">Brand Mentions</div>
                        <div class="h4 fw-bold mb-0"><?= $prompt['brand_mentions'] ?></div>
                    </div>
                </div>

                <div class="d-flex align-items-center mb-3">
                    <div class="bg-success bg-opacity-10 text-success p-3 rounded-3 me-3">
                        <i class="bi bi-link-45deg fs-4"></i>
                    </div>
                    <div>
                        <div class="small text-muted">Domain Citations</div>
                        <div class="h4 fw-bold mb-0"><?= $prompt['domain_citations'] ?></div>
                    </div>
                </div>

                <div class="d-flex align-items-center">
                    <div class="bg-info bg-opacity-10 text-info p-3 rounded-3 me-3">
                        <i class="bi bi-list-check fs-4"></i>
                    </div>
                    <div>
                        <div class="small text-muted">Total Citations</div>
                        <div class="h4 fw-bold mb-0"><?= $prompt['total_citations'] ?></div>
                    </div>
                </div>
            </div>
        </div>

        <?php if ($latestRun && $latestRun['snapshot_url']): ?>
        <div class="card shadow-sm border-0 rounded-4">
            <div class="card-header bg-white border-bottom-0 pt-4 px-4">
                <h5 class="fw-bold mb-0">Snapshot</h5>
            </div>
            <div class="card-body p-4 text-center">
                <img src="<?= htmlspecialchars($latestRun['snapshot_url']) ?>" class="img-fluid rounded border shadow-sm" alt="Run Snapshot">
                <a href="<?= htmlspecialchars($latestRun['snapshot_url']) ?>" target="_blank" class="btn btn-outline-primary btn-sm mt-3 w-100">View Full Screenshot</a>
            </div>
        </div>
        <?php endif; ?>
    </div>
</div>
<?php require __DIR__ . '/../layout/footer.php'; ?>
