{% extends "base.html" %}

{% block title %}{{ benefit.name }} - Korzyści Członkowskie{% endblock %}

{% block extra_css %}
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/marked-highlight/styles/github.min.css">
<style>
    .benefit-detail {
        max-width: 900px;
        margin: 0 auto;
    }

    .benefit-detail-header {
        display: flex;
        align-items: flex-start;
        gap: var(--spacing-lg);
        margin-bottom: var(--spacing-xl);
        padding-bottom: var(--spacing-xl);
        border-bottom: 1px solid var(--border-color);
    }

    .benefit-detail-logo {
        width: 80px;
        height: 80px;
        border-radius: var(--radius-lg);
        object-fit: contain;
        background: #f8fafc;
        padding: 8px;
        border: 1px solid var(--border-color);
    }

    .benefit-detail-logo-placeholder {
        width: 80px;
        height: 80px;
        border-radius: var(--radius-lg);
        background: linear-gradient(135deg, var(--primary-light) 0%, var(--primary) 100%);
        display: flex;
        align-items: center;
        justify-content: center;
        color: white;
        font-weight: 700;
        font-size: var(--font-size-2xl);
    }

    .benefit-detail-info {
        flex: 1;
    }

    .benefit-detail-title {
        font-size: var(--font-size-2xl);
        font-weight: 700;
        color: var(--text-primary);
        margin-bottom: var(--spacing-xs);
    }

    .benefit-detail-category {
        color: var(--text-secondary);
        margin-bottom: var(--spacing-md);
    }

    .benefit-detail-pricing {
        display: flex;
        align-items: baseline;
        gap: var(--spacing-md);
    }

    .benefit-detail-member-price {
        font-size: var(--font-size-2xl);
        font-weight: 700;
        color: var(--success);
    }

    .benefit-detail-regular-price {
        font-size: var(--font-size-lg);
        color: var(--text-secondary);
        text-decoration: line-through;
    }

    .benefit-detail-body {
        background: white;
        border-radius: var(--radius-lg);
        border: 1px solid var(--border-color);
        box-shadow: var(--shadow);
        padding: var(--spacing-xl);
        margin-bottom: var(--spacing-xl);
    }

    /* ============================================
       MARKDOWN CONTENT STYLING
       ============================================ */
    .benefit-content {
        color: var(--text-primary);
        line-height: 1.7;
    }

    .benefit-content h2 {
        font-size: 1.5rem;
        font-weight: 700;
        color: var(--text-primary);
        margin: 2rem 0 1rem 0;
        padding-bottom: 0.5rem;
        border-bottom: 2px solid var(--primary);
        display: flex;
        align-items: center;
        gap: 0.5rem;
    }

    .benefit-content h2:first-child {
        margin-top: 0;
    }

    .benefit-content h3 {
        font-size: 1.2rem;
        font-weight: 600;
        color: var(--text-primary);
        margin: 1.5rem 0 0.75rem 0;
    }

    .benefit-content p {
        margin-bottom: 1rem;
        color: var(--text-secondary);
    }

    .benefit-content strong {
        color: var(--text-primary);
        font-weight: 600;
    }

    /* Beautiful tables */
    .benefit-content table {
        width: 100%;
        border-collapse: collapse;
        margin: 1rem 0 1.5rem 0;
        border-radius: var(--radius-md);
        overflow: hidden;
        box-shadow: 0 1px 3px rgba(0,0,0,0.1);
    }

    .benefit-content table thead {
        background: linear-gradient(135deg, var(--primary) 0%, var(--primary-dark) 100%);
        color: white;
    }

    .benefit-content table th {
        padding: 0.875rem 1rem;
        text-align: left;
        font-weight: 600;
        font-size: 0.9rem;
    }

    .benefit-content table td {
        padding: 0.875rem 1rem;
        border-bottom: 1px solid var(--border-color);
        background: white;
    }

    .benefit-content table tr:last-child td {
        border-bottom: none;
    }

    .benefit-content table tr:hover td {
        background: #f8fafc;
    }

    /* Lists */
    .benefit-content ul, .benefit-content ol {
        margin: 0.75rem 0 1.25rem 0;
        padding-left: 0;
        list-style: none;
    }

    .benefit-content ul li, .benefit-content ol li {
        position: relative;
        padding: 0.5rem 0 0.5rem 2rem;
        color: var(--text-secondary);
    }

    .benefit-content ul li::before {
        content: "✓";
        position: absolute;
        left: 0;
        color: var(--success);
        font-weight: bold;
        font-size: 1rem;
    }

    /* Code blocks (for flowcharts) - convert to visual */
    .benefit-content pre {
        display: none; /* Hide ASCII flowcharts */
    }

    /* Horizontal rule */
    .benefit-content hr {
        border: none;
        height: 1px;
        background: linear-gradient(to right, transparent, var(--border-color), transparent);
        margin: 2rem 0;
    }

    /* ============================================
       PLAN CARDS
       ============================================ */
    .plan-cards {
        display: grid;
        grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
        gap: 1rem;
        margin: 1.5rem 0;
    }

    .plan-card {
        background: white;
        border: 2px solid var(--border-color);
        border-radius: var(--radius-lg);
        padding: 1.25rem;
        text-align: center;
        transition: all 0.2s ease;
    }

    .plan-card:hover {
        border-color: var(--primary);
        transform: translateY(-2px);
        box-shadow: var(--shadow-lg);
    }

    .plan-card.featured {
        border-color: var(--primary);
        background: linear-gradient(135deg, #f0f9ff 0%, #e0f2fe 100%);
    }

    .plan-card.free {
        border-color: #22c55e;
        background: linear-gradient(135deg, #f0fdf4 0%, #dcfce7 100%);
    }

    .plan-card.student {
        border-color: #8b5cf6;
        background: linear-gradient(135deg, #faf5ff 0%, #f3e8ff 100%);
    }

    .plan-card-icon {
        font-size: 2rem;
        margin-bottom: 0.5rem;
    }

    .plan-card-title {
        font-weight: 700;
        font-size: 1.1rem;
        color: var(--text-primary);
        margin-bottom: 0.5rem;
    }

    .plan-card-price {
        font-size: 1.5rem;
        font-weight: 700;
        color: var(--primary);
        margin-bottom: 0.25rem;
    }

    .plan-card.free .plan-card-price {
        color: #22c55e;
    }

    .plan-card.student .plan-card-price {
        color: #8b5cf6;
    }

    .plan-card-subtitle {
        font-size: 0.85rem;
        color: var(--text-secondary);
    }

    /* ============================================
       PROCESS STEPS (Flowchart replacement)
       ============================================ */
    .process-section {
        margin: 2rem 0;
    }

    .process-title {
        font-size: 1.1rem;
        font-weight: 600;
        color: var(--text-primary);
        margin-bottom: 1rem;
        display: flex;
        align-items: center;
        gap: 0.5rem;
    }

    .process-steps {
        position: relative;
        padding-left: 2rem;
    }

    .process-steps::before {
        content: "";
        position: absolute;
        left: 0.75rem;
        top: 0;
        bottom: 0;
        width: 2px;
        background: linear-gradient(to bottom, var(--primary), var(--success));
        border-radius: 1px;
    }

    .process-step {
        position: relative;
        padding: 0.75rem 0 0.75rem 1.5rem;
        margin-bottom: 0.5rem;
    }

    .process-step::before {
        content: attr(data-step);
        position: absolute;
        left: -1.25rem;
        top: 0.75rem;
        width: 1.5rem;
        height: 1.5rem;
        background: var(--primary);
        color: white;
        border-radius: 50%;
        display: flex;
        align-items: center;
        justify-content: center;
        font-size: 0.75rem;
        font-weight: 700;
        z-index: 1;
    }

    .process-step:last-child::before {
        background: var(--success);
    }

    .process-step-content {
        background: #f8fafc;
        padding: 1rem;
        border-radius: var(--radius-md);
        border-left: 3px solid var(--primary);
    }

    .process-step:last-child .process-step-content {
        border-left-color: var(--success);
        background: #f0fdf4;
    }

    .process-step-title {
        font-weight: 600;
        color: var(--text-primary);
        margin-bottom: 0.25rem;
    }

    .process-step-desc {
        font-size: 0.9rem;
        color: var(--text-secondary);
    }

    /* ============================================
       FEATURE BADGES
       ============================================ */
    .feature-list {
        display: flex;
        flex-wrap: wrap;
        gap: 0.75rem;
        margin: 1rem 0;
    }

    .feature-badge {
        display: inline-flex;
        align-items: center;
        gap: 0.5rem;
        padding: 0.5rem 1rem;
        background: #f0f9ff;
        border: 1px solid #bae6fd;
        border-radius: 2rem;
        font-size: 0.9rem;
        color: #0369a1;
    }

    .feature-badge.success {
        background: #f0fdf4;
        border-color: #bbf7d0;
        color: #15803d;
    }

    /* ============================================
       DOCUMENT LIST
       ============================================ */
    .document-grid {
        display: grid;
        grid-template-columns: repeat(auto-fill, minmax(180px, 1fr));
        gap: 0.75rem;
        margin: 1rem 0;
    }

    .document-item {
        display: flex;
        align-items: center;
        gap: 0.5rem;
        padding: 0.75rem;
        background: #f8fafc;
        border-radius: var(--radius-md);
        font-size: 0.9rem;
        color: var(--text-secondary);
    }

    .document-item-icon {
        font-size: 1.25rem;
    }

    /* ============================================
       EXAMPLE CARDS
       ============================================ */
    .example-grid {
        display: grid;
        grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
        gap: 1rem;
        margin: 1.5rem 0;
    }

    .example-card {
        background: white;
        border: 1px solid var(--border-color);
        border-radius: var(--radius-lg);
        padding: 1.25rem;
        transition: all 0.2s ease;
    }

    .example-card:hover {
        box-shadow: var(--shadow-lg);
        transform: translateY(-2px);
    }

    .example-card-icon {
        font-size: 1.75rem;
        margin-bottom: 0.75rem;
    }

    .example-card-title {
        font-weight: 600;
        color: var(--text-primary);
        margin-bottom: 0.5rem;
    }

    .example-card-desc {
        font-size: 0.9rem;
        color: var(--text-secondary);
        line-height: 1.5;
    }

    /* ============================================
       TABS FOR OPTIONS
       ============================================ */
    .option-tabs {
        display: flex;
        gap: 0.5rem;
        margin-bottom: 1rem;
    }

    .option-tab {
        flex: 1;
        padding: 0.75rem 1rem;
        background: #f1f5f9;
        border: 2px solid transparent;
        border-radius: var(--radius-md);
        font-weight: 600;
        cursor: pointer;
        text-align: center;
        transition: all 0.2s ease;
    }

    .option-tab:hover {
        background: #e2e8f0;
    }

    .option-tab.active {
        background: white;
        border-color: var(--primary);
        color: var(--primary);
    }

    .option-content {
        display: none;
        padding: 1.5rem;
        background: #f8fafc;
        border-radius: var(--radius-lg);
    }

    .option-content.active {
        display: block;
    }

    /* ============================================
       CTA & PROMO
       ============================================ */
    .benefit-promo-code-box {
        background: linear-gradient(135deg, #f0f9ff 0%, #e0f2fe 100%);
        border: 2px dashed #0ea5e9;
        border-radius: var(--radius-lg);
        padding: var(--spacing-lg);
        text-align: center;
        margin-bottom: var(--spacing-xl);
    }

    .promo-code-label {
        font-size: var(--font-size-sm);
        color: var(--text-secondary);
        margin-bottom: var(--spacing-xs);
    }

    .promo-code-value {
        font-family: monospace;
        font-weight: 700;
        color: #0284c7;
        font-size: var(--font-size-2xl);
        letter-spacing: 2px;
        margin-bottom: var(--spacing-sm);
        background: white;
        padding: 0.5rem 1.5rem;
        border-radius: var(--radius-md);
        display: inline-block;
    }

    .promo-code-instructions {
        font-size: var(--font-size-sm);
        color: var(--text-secondary);
    }

    .benefit-detail-cta {
        display: flex;
        gap: var(--spacing-md);
    }

    .benefit-cta-primary {
        flex: 1;
        display: flex;
        align-items: center;
        justify-content: center;
        gap: var(--spacing-sm);
        padding: var(--spacing-md) var(--spacing-lg);
        background: linear-gradient(135deg, var(--primary) 0%, var(--primary-dark) 100%);
        color: white;
        border: none;
        border-radius: var(--radius-md);
        font-weight: 600;
        font-size: var(--font-size-lg);
        cursor: pointer;
        transition: all 0.2s ease;
        text-decoration: none;
        box-shadow: 0 4px 14px rgba(var(--primary-rgb), 0.3);
    }

    .benefit-cta-primary:hover {
        transform: translateY(-2px);
        box-shadow: 0 6px 20px rgba(var(--primary-rgb), 0.4);
        color: white;
    }

    .benefit-cta-primary svg {
        width: 20px;
        height: 20px;
    }

    .benefit-cta-secondary {
        display: flex;
        align-items: center;
        justify-content: center;
        gap: var(--spacing-sm);
        padding: var(--spacing-md) var(--spacing-lg);
        background: white;
        color: var(--text-primary);
        border: 1px solid var(--border-color);
        border-radius: var(--radius-md);
        font-weight: 500;
        cursor: pointer;
        transition: all 0.2s ease;
        text-decoration: none;
    }

    .benefit-cta-secondary:hover {
        background: #f8fafc;
    }

    .back-link {
        display: inline-flex;
        align-items: center;
        gap: var(--spacing-xs);
        color: var(--text-secondary);
        margin-bottom: var(--spacing-lg);
        text-decoration: none;
    }

    .back-link:hover {
        color: var(--primary);
    }

    .back-link svg {
        width: 16px;
        height: 16px;
    }

    .commission-info {
        background: #fef3c7;
        border: 1px solid #f59e0b;
        border-radius: var(--radius-md);
        padding: var(--spacing-md);
        margin-top: var(--spacing-xl);
        font-size: var(--font-size-sm);
        color: #92400e;
    }

    .commission-info strong {
        color: #78350f;
    }

    /* Mobile responsiveness */
    @media (max-width: 640px) {
        .plan-cards {
            grid-template-columns: 1fr;
        }

        .example-grid {
            grid-template-columns: 1fr;
        }

        .document-grid {
            grid-template-columns: 1fr 1fr;
        }

        .benefit-detail-cta {
            flex-direction: column;
        }

        .option-tabs {
            flex-direction: column;
        }
    }
</style>
{% endblock %}

{% block content %}
<div class="container">
    <div class="benefit-detail">
        <a href="{{ url_for('benefits.benefits_list') }}" class="back-link">
            <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
                <line x1="19" y1="12" x2="5" y2="12"></line>
                <polyline points="12 19 5 12 12 5"></polyline>
            </svg>
            Powrót do listy korzyści
        </a>

        <div class="benefit-detail-header">
            {% if benefit.logo_url %}
            <img src="{{ benefit.logo_url }}" alt="{{ benefit.name }}" class="benefit-detail-logo">
            {% else %}
            <div class="benefit-detail-logo-placeholder">{{ benefit.name[0] }}</div>
            {% endif %}

            <div class="benefit-detail-info">
                <h1 class="benefit-detail-title">{{ benefit.name }}</h1>
                <div class="benefit-detail-category">{{ benefit.category_label }}</div>

                {% if benefit.member_price or benefit.discount_description %}
                <div class="benefit-detail-pricing">
                    {% if benefit.member_price %}
                    <span class="benefit-detail-member-price">{{ benefit.member_price }}</span>
                    {% endif %}
                    {% if benefit.regular_price %}
                    <span class="benefit-detail-regular-price">{{ benefit.regular_price }}</span>
                    {% endif %}
                </div>
                {% endif %}
            </div>
        </div>

        <div class="benefit-detail-body">
            {% if benefit.description %}
            <div id="benefit-content" class="benefit-content">
                <!-- Content will be rendered by JavaScript -->
            </div>
            {% endif %}

            {% if benefit.promo_code %}
            <div class="benefit-promo-code-box">
                <div class="promo-code-label">Twój kod rabatowy:</div>
                <div class="promo-code-value">{{ benefit.promo_code }}</div>
                {% if benefit.promo_code_instructions %}
                <div class="promo-code-instructions">{{ benefit.promo_code_instructions }}</div>
                {% else %}
                <div class="promo-code-instructions">Użyj tego kodu podczas składania zamówienia.</div>
                {% endif %}
            </div>
            {% endif %}

            <div class="benefit-detail-cta">
                <a href="{{ url_for('benefits.benefit_redirect', slug=benefit.slug) }}" class="benefit-cta-primary" target="_blank" rel="noopener">
                    Wypróbuj za darmo
                    <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
                        <path d="M18 13v6a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2V8a2 2 0 0 1 2-2h6"></path>
                        <polyline points="15 3 21 3 21 9"></polyline>
                        <line x1="10" y1="14" x2="21" y2="3"></line>
                    </svg>
                </a>

                {% if benefit.product_url %}
                <a href="{{ benefit.product_url }}" class="benefit-cta-secondary" target="_blank" rel="noopener">
                    Zobacz stronę produktu
                </a>
                {% endif %}
            </div>

            <!-- Admin info - tylko dla właściciela -->
            {% if current_user.email == 'maciej.pienczyn@inpi.pl' %}
            <div class="commission-info">
                <strong>Dane prowizji (prywatne):</strong>
                {% if benefit.commission_rate %}Prowizja: {{ benefit.commission_rate }}{% endif %}
                {% if benefit.commission_duration %} przez {{ benefit.commission_duration }}{% endif %}
                {% if benefit.partner_platform %} | Platforma: {{ benefit.partner_platform }}{% endif %}
                {% if benefit.click_count %} | Kliknięć: {{ benefit.click_count }}{% endif %}
            </div>
            {% endif %}
        </div>
    </div>
</div>

<script src="https://cdn.jsdelivr.net/npm/marked/marked.min.js"></script>
<script>
document.addEventListener('DOMContentLoaded', function() {
    const rawContent = {{ benefit.description | tojson | safe }};
    const contentEl = document.getElementById('benefit-content');

    if (rawContent && contentEl) {
        // Configure marked
        marked.setOptions({
            breaks: true,
            gfm: true
        });

        // Render markdown
        let html = marked.parse(rawContent);

        // Post-process for better visual elements
        // Convert ASCII flowcharts to hidden (they're replaced by visual elements)
        html = html.replace(/<pre><code[^>]*>[\s\S]*?<\/code><\/pre>/g, '');

        contentEl.innerHTML = html;

        // Add visual enhancements after render
        enhanceContent();
    }
});

function enhanceContent() {
    // This function could be extended to add more dynamic enhancements
    // For example, converting certain patterns to interactive elements
}
</script>
{% endblock %}
