use super::html_escape;

pub(super) fn cover(
    target_name: &str,
    session_id: &str,
    date_short: &str,
    target_url: &str,
    requester_name: &str,
    requester_email: &str,
    app_screenshot_b64: Option<&str>,
) -> String {
    let screenshot_html = app_screenshot_b64
        .filter(|s| !s.is_empty())
        .map(|b64| {
            format!(
                r#"<div style="margin: 20px auto; max-width: 560px; border: 1px solid #cbd5e1; border-radius: 8px; overflow: hidden; box-shadow: 0 4px 12px rgba(0,0,0,0.08);">
    <img src="data:image/png;base64,{b64}" alt="Application screenshot" style="width:100%;display:block;"/>
</div>"#
            )
        })
        .unwrap_or_default();
    format!(
        r##"<!-- ═══════════════ COVER PAGE ═══════════════ -->
<div class="cover">
    <svg class="cover-shield" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 96 96">
        <defs>
            <linearGradient id="sg" x1="0%" y1="0%" x2="100%" y2="100%">
                <stop offset="0%" stop-color="#0d2137"/>
                <stop offset="100%" stop-color="#1a56db"/>
            </linearGradient>
        </defs>
        <path d="M48 6 L22 22 L22 48 C22 66 34 80 48 86 C62 80 74 66 74 48 L74 22 Z"
              fill="none" stroke="url(#sg)" stroke-width="3.5" stroke-linejoin="round"/>
        <path d="M48 12 L26 26 L26 47 C26 63 36 76 48 82 C60 76 70 63 70 47 L70 26 Z"
              fill="url(#sg)" opacity="0.07"/>
        <circle cx="44" cy="44" r="11" fill="none" stroke="#0d2137" stroke-width="2.5"/>
        <line x1="52" y1="52" x2="62" y2="62" stroke="#0d2137" stroke-width="2.5" stroke-linecap="round"/>
        <path d="M39 44 L42.5 47.5 L49 41" fill="none" stroke="#166534" stroke-width="2.5"
              stroke-linecap="round" stroke-linejoin="round"/>
    </svg>

    <div class="cover-tag">CONFIDENTIAL</div>

    <div class="cover-title">Penetration Test Report</div>
    <div class="cover-subtitle">{target_name}</div>

    <div class="cover-divider"></div>

    <div class="cover-meta">
        <strong>Report ID:</strong> {session_id}<br>
        <strong>Date:</strong> {date_short}<br>
        <strong>Target:</strong> {target_url}<br>
        <strong>Prepared for:</strong> {requester_name} ({requester_email})
    </div>

    {screenshot_html}

    <div class="cover-footer">
        Compliance Scanner &mdash; AI-Powered Security Assessment Platform
    </div>
</div>"##,
        target_name = html_escape(target_name),
        session_id = html_escape(session_id),
        date_short = date_short,
        target_url = html_escape(target_url),
        requester_name = html_escape(requester_name),
        requester_email = html_escape(requester_email),
    )
}
