{% extends "base.html" %}

{% block title %}Admin Banners{% endblock %}
{% block page_id %}banners{% endblock %}
{% block page_title %}Banners & Watermark{% endblock %}
{% block eyebrow %}Branding{% endblock %}

{% block head %}
<style>
    .preview-img {
        width: 100%;
        max-height: 200px;
        object-fit: contain;
        border-radius: 10px;
        background: #fff;
        display: block;
    }

    .upload-form input[type="url"],
    .upload-form input[type="text"],
    input, p, label {
        color: #111 !important;
    }

    .upload-form input[type="url"],
    .upload-form input[type="text"] {
        background: #ffffff !important;
        border: 1.5px solid #cbd5e1 !important;
        width: 100%;
        padding: 12px 16px;
        border-radius: 12px;
        font-size: 14px;
    }

    .upload-form input::placeholder {
        color: #94a3b8 !important;
    }

    .upload-form input:focus {
        outline: none;
        border-color: #2c7da0 !important;
        box-shadow: 0 0 0 3px rgba(44, 125, 160, 0.1);
    }

    .upload-form input[type="file"] {
        background: #ffffff !important;
        color: #0f172a !important;
        border: 1.5px solid #cbd5e1 !important;
        padding: 10px 14px;
        border-radius: 12px;
        cursor: pointer;
    }

    .upload-form input[type="file"]::-webkit-file-upload-button {
        background: #2c7da0 !important;
        color: #fff !important;
        border: none !important;
        border-radius: 8px;
        padding: 8px 16px;
        cursor: pointer;
        transition: all 0.2s;
    }

    .upload-form input[type="file"]::-webkit-file-upload-button:hover {
        background: #1f5068 !important;
    }

    .upload-form input[type="file"]::-moz-file-upload-button {
        background: #2c7da0 !important;
        color: #fff !important;
        border: none !important;
        border-radius: 8px;
        padding: 8px 16px;
        cursor: pointer;
        transition: all 0.2s;
    }

    .field-divider {
        display: flex;
        align-items: center;
        margin: 16px 0;
        position: relative;
    }
    .field-divider::before,
    .field-divider::after {
        content: '';
        flex: 1;
        height: 1px;
        background: rgba(0, 0, 0, 0.1);
    }
    .divider-text {
        padding: 0 12px;
        font-size: 12px;
        font-weight: 600;
        color: #475569 !important;
        text-transform: uppercase;
        letter-spacing: 1px;
    }

    .field-label {
        color: #475569 !important;
        font-weight: 600;
        display: block;
        margin-bottom: 6px;
    }

    .field-hint {
        color: #475569 !important;
        font-size: 13px;
        margin-top: 6px;
        margin-bottom: 0;
    }

    .asset-image {
        max-width: 100%;
        border-radius: 10px;
        background: #fff;
        object-fit: contain;
    }

    .asset-stage {
        min-height: 150px;
        display: flex;
        align-items: center;
        justify-content: center;
        background: #f8fafc;
        border-radius: 12px;
        padding: 20px;
        border: 1px solid #e2e8f0;
    }

    .asset-empty {
        color: #64748b !important;
        font-size: 14px;
        text-align: center;
    }

    .upload-layout {
        display: grid;
        grid-template-columns: 1fr 1fr;
        gap: 24px;
        padding: 24px;
    }

    @media (max-width: 1024px) {
        .upload-layout {
            grid-template-columns: 1fr;
        }
    }

    .asset-stage img {
        display: block;
        max-height: 250px;
        width: auto;
        margin: 0 auto;
    }

    .watermark-badge {
        display: inline-block;
        background: linear-gradient(135deg, #4dd0e1 0%, #29b6f6 100%);
        color: white;
        padding: 6px 12px;
        border-radius: 20px;
        font-size: 12px;
        font-weight: 600;
        margin-left: 12px;
    }

    .watermark-info {
        background: linear-gradient(135deg, rgba(77, 208, 225, 0.1) 0%, rgba(41, 182, 246, 0.1) 100%);
        border: 1px solid rgba(77, 208, 225, 0.3);
        padding: 16px;
        border-radius: 12px;
        margin-bottom: 16px;
    }

    .watermark-info p {
        color: #0369a1 !important;
        font-size: 13px;
        margin: 0;
    }

    .section-divider {
        margin: 32px 0;
        padding: 24px 0;
        border-top: 2px solid #e2e8f0;
        border-bottom: 2px solid #e2e8f0;
    }

    .section-title {
        color: #0f172a !important;
        font-size: 18px;
        font-weight: 700;
        margin-bottom: 8px;
    }

    .section-subtitle {
        color: #64748b !important;
        font-size: 14px;
    }
</style>
{% endblock %}

{% block content %}
    <section class="panel-stack">
        <!-- STANDARD ASSETS (Logo, Header, Footer, etc.) -->
        {% for asset_type, config in asset_rules.items() %}
            {% if asset_type != 'watermark' %}
                {% set current_url = assets.get(asset_type) %}
                <article class="glass-card panel-card upload-card">
                    <div class="panel-header">
                        <div>
                            <p class="eyebrow">{{ config.label }}</p>
                            <h3>{{ config.label }}</h3>
                        </div>
                        <span class="pill">{{ config.width }} x {{ config.height }} px</span>
                    </div>

                    <div class="upload-layout">
                        <div class="asset-stage">
                            {% if current_url %}
                                <img class="asset-image preview-img" src="{{ current_url }}" alt="{{ config.label }}">
                            {% else %}
                                <div class="asset-empty">No {{ config.label|lower }} uploaded yet</div>
                            {% endif %}
                        </div>

                        <div class="upload-form">
                            <form method="post" action="{{ url_for('admin.update_banner') }}" class="upload-form" enctype="multipart/form-data">
                                <input type="hidden" name="link_type" value="{{ asset_type }}">

                                <div class="field-group">
                                    <label class="field-label" for="url_{{ asset_type }}">Image URL</label>
                                    <input
                                        id="url_{{ asset_type }}"
                                        name="image_url"
                                        type="text"
                                        placeholder="https://example.com/image.jpg"
                                        value="{{ current_url if current_url else '' }}"
                                    >
                                </div>
                                <p class="field-hint">Enter a direct URL to an image (JPG, PNG). Recommended size: {{ config.width }} x {{ config.height }} px.</p>

                                <div class="field-divider">
                                    <span class="divider-text">OR</span>
                                </div>

                                <div class="field-group">
                                    <label class="field-label" for="file_{{ asset_type }}">Upload Image</label>
                                    <input
                                        id="file_{{ asset_type }}"
                                        name="image_file"
                                        type="file"
                                        accept="image/jpeg,image/jpg,image/png"
                                    >
                                </div>
                                <p class="field-hint">Upload from computer (JPG, PNG, max 5MB). File upload takes priority if both provided.</p>

                                <button class="primary-btn" type="submit">Update {{ config.label }}</button>
                            </form>
                        </div>
                    </div>
                </article>
            {% endif %}
        {% endfor %}

        <!-- WATERMARK SECTION (SPECIAL) -->
        {% set watermark_config = asset_rules.get('watermark') %}
        {% set watermark_url = assets.get('watermark') %}
        <article class="glass-card panel-card upload-card">
            <div class="panel-header">
                <div>
                    <p class="eyebrow">{{ watermark_config.label }}</p>
                    <h3>{{ watermark_config.label }} <span class="watermark-badge">✨ NEW</span></h3>
                </div>
                <span class="pill">{{ watermark_config.width }} x {{ watermark_config.height }} px</span>
            </div>

            <div class="watermark-info">
                <p>💡 <strong>Watermark images</strong> are used to protect your photos and add branding. Upload a transparent PNG or JPG (Recommended: 400 × 150 px)</p>
            </div>

            <div class="upload-layout">
                <div class="asset-stage">
                    {% if watermark_url %}
                        <img class="asset-image preview-img" src="{{ watermark_url }}" alt="Watermark Image" style="max-height: 150px;">
                    {% else %}
                        <div class="asset-empty">📸 No watermark uploaded yet<br><small>Upload to add watermark protection</small></div>
                    {% endif %}
                </div>

                <div class="upload-form">
                    <form method="post" action="{{ url_for('admin.update_banner') }}" class="upload-form" enctype="multipart/form-data">
                        <input type="hidden" name="link_type" value="watermark">

                        <div class="field-group">
                            <label class="field-label" for="url_watermark">Watermark URL</label>
                            <input
                                id="url_watermark"
                                name="image_url"
                                type="text"
                                placeholder="https://example.com/watermark.png"
                                value="{{ watermark_url if watermark_url else '' }}"
                            >
                        </div>
                        <p class="field-hint">Enter a direct URL to a watermark image (JPG, PNG, transparent PNG recommended). Size: {{ watermark_config.width }} x {{ watermark_config.height }} px.</p>

                        <div class="field-divider">
                            <span class="divider-text">OR</span>
                        </div>

                        <div class="field-group">
                            <label class="field-label" for="file_watermark">Upload Watermark</label>
                            <input
                                id="file_watermark"
                                name="image_file"
                                type="file"
                                accept="image/jpeg,image/jpg,image/png"
                            >
                        </div>
                        <p class="field-hint">Upload from computer (JPG, PNG with transparency, max 5MB). Uses AWS S3 for storage.</p>

                        <button class="primary-btn" type="submit">Update Watermark</button>
                    </form>
                </div>
            </div>

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


        </article>
    </section>
{% endblock %}