{% extends "layouts/base.html" %}
{% block title %}Company Dashboard{% endblock %}
{% block extra_css %}
<style>
    .dashboard-hero {
        display: flex;
        flex-wrap: wrap;
        gap: 24px;
        align-items: center;
        background: linear-gradient(135deg, #2152ff, #31d2f2);
        color: #ffffff;
        padding: 32px;
        border-radius: 18px;
        box-shadow: 0 18px 40px -20px rgba(33, 82, 255, 0.45);
        margin-bottom: 32px;
        position: relative;
        overflow: hidden;
    }

    .dashboard-hero::after {
        content: "";
        position: absolute;
        inset: 0;
        background: radial-gradient(circle at top right, rgba(255, 255, 255, 0.35), transparent 55%);
        pointer-events: none;
    }

    .dashboard-hero .hero-text {
        flex: 1 1 320px;
        position: relative;
        z-index: 1;
    }

    .dashboard-hero h1 {
        font-size: 2.1rem;
        font-weight: 600;
        margin-bottom: 12px;
    }

    .dashboard-hero p {
        font-size: 1rem;
        opacity: 0.9;
        max-width: 520px;
        margin-bottom: 20px;
    }

    .hero-actions {
        display: flex;
        flex-wrap: wrap;
        gap: 12px;
    }

    .hero-actions .action-button {
        background: rgba(255, 255, 255, 0.18);
        color: #fff;
        border: none;
        backdrop-filter: blur(4px);
    }

    .hero-actions .action-button.secondary {
        background: rgba(16, 6, 57, 0.6);
    }

    .hero-illustration {
        flex: 0 1 260px;
        position: relative;
        z-index: 1;
        display: flex;
        align-items: center;
        justify-content: center;
    }

    .hero-illustration img {
        width: 240px;
        max-width: 100%;
        filter: drop-shadow(0 25px 40px rgba(7, 23, 91, 0.35));
    }

    .stats-grid {
        display: grid;
        grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
        gap: 20px;
        margin-bottom: 32px;
    }

    .stat-card {
        background: #ffffff;
        border-radius: 16px;
        padding: 20px;
        box-shadow: 0 18px 30px -24px rgba(15, 23, 42, 0.65);
        border: 1px solid rgba(226, 232, 240, 0.8);
        transition: transform 0.2s ease, box-shadow 0.2s ease;
        position: relative;
        overflow: hidden;
    }

    .stat-card::after {
        content: "";
        position: absolute;
        inset: 0;
        background: radial-gradient(circle at top left, rgba(33, 82, 255, 0.08), transparent 65%);
        pointer-events: none;
    }

    .stat-card:hover {
        transform: translateY(-4px);
        box-shadow: 0 22px 40px -20px rgba(15, 23, 42, 0.35);
    }

    .stat-card .stat-icon {
        width: 48px;
        height: 48px;
        border-radius: 14px;
        display: flex;
        align-items: center;
        justify-content: center;
        font-size: 1.4rem;
        color: #2152ff;
        background: rgba(33, 82, 255, 0.12);
        margin-bottom: 16px;
    }

    .stat-card h4 {
        font-size: 1.5rem;
        font-weight: 600;
        margin: 0;
        color: #0f172a;
    }

    .stat-card p {
        margin: 6px 0 0 0;
        color: #64748b;
        font-size: 0.9rem;
    }

    .info-grid {
        display: grid;
        grid-template-columns: minmax(0, 2fr) minmax(0, 1.3fr);
        gap: 24px;
        margin-bottom: 32px;
    }

    .section-card {
        background: #ffffff;
        border-radius: 16px;
        padding: 24px;
        border: 1px solid rgba(226, 232, 240, 0.8);
        box-shadow: 0 12px 40px -28px rgba(15, 23, 42, 0.45);
    }

    .section-card h2,
    .section-card h3 {
        margin: 0;
        font-size: 1.25rem;
        font-weight: 600;
        color: #0f172a;
    }

    .section-card .subtext {
        color: #64748b;
        font-size: 0.95rem;
        margin-top: 6px;
    }

    .profile-form {
        display: grid;
        gap: 20px;
        margin-top: 20px;
    }

    .card-heading {
        display: flex;
        flex-direction: column;
        gap: 6px;
        margin-bottom: 18px;
    }

    .form-columns {
        display: flex;
        flex-wrap: wrap;
        gap: 18px;
    }

    .form-columns .form-group {
        flex: 1 1 220px;
    }

    .profile-form .form-group label {
        font-weight: 600;
        color: #0f172a;
        margin-bottom: 8px;
        display: block;
    }

    .profile-form textarea,
    .profile-form input[type="email"],
    .profile-form input[type="text"],
    .profile-form input[type="url"] {
        width: 100%;
        border: 1px solid rgba(148, 163, 184, 0.45);
        border-radius: 10px;
        padding: 14px 16px;
        font-size: 0.95rem;
        color: #0f172a;
        background: #f8fafc;
        transition: border-color 0.2s ease, box-shadow 0.2s ease, background 0.2s ease;
    }

    .profile-form textarea:focus,
    .profile-form input:focus {
        outline: none;
        border-color: #2152ff;
        background: #ffffff;
        box-shadow: 0 0 0 4px rgba(33, 82, 255, 0.12);
    }

    .badge {
        display: inline-flex;
        align-items: center;
        gap: 6px;
        border-radius: 999px;
        padding: 6px 14px;
        font-size: 0.85rem;
        font-weight: 500;
        background: rgba(33, 82, 255, 0.12);
        color: #2152ff;
    }

    .quick-actions {
        display: grid;
        grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
        gap: 12px;
        margin-top: 20px;
    }

    .quick-actions button,
    .quick-actions a {
        display: flex;
        align-items: center;
        gap: 10px;
        justify-content: center;
        padding: 14px 16px;
        font-weight: 500;
        border-radius: 12px;
        border: 1px solid rgba(148, 163, 184, 0.35);
        background: #f8fafc;
        color: #0f172a;
        transition: transform 0.2s ease, box-shadow 0.2s ease, background 0.2s ease;
        text-decoration: none;
    }

    .quick-actions button:hover,
    .quick-actions a:hover {
        transform: translateY(-2px);
        background: #ffffff;
        box-shadow: 0 18px 30px -26px rgba(15, 23, 42, 0.35);
    }

    .widget-installation {
        background: linear-gradient(135deg, #f6f8ff, #ffffff);
        border: 1px solid rgba(226, 232, 240, 0.9);
        border-radius: 16px;
        padding: 24px;
        box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.65), 0 16px 40px -32px rgba(37, 99, 235, 0.35);
        margin-top: 24px;
    }

    .widget-installation h5 {
        margin: 0 0 12px 0;
        font-size: 1.05rem;
        font-weight: 600;
        color: #0f172a;
    }

    .code-container {
        background: #0f172a;
        color: #e2e8f0;
        border-radius: 14px;
        padding: 18px;
        position: relative;
        overflow: hidden;
        font-size: 0.85rem;
    }

    .code-container pre {
        margin: 0;
        white-space: pre-wrap;
        word-break: break-word;
    }

    .code-container code {
        font-family: "Fira Code", Consolas, monospace;
        color: inherit;
    }

    .copy-btn {
        position: absolute;
        top: 12px;
        right: 12px;
        padding: 8px 14px;
        border-radius: 999px;
        background: rgba(255, 255, 255, 0.12);
        color: #fff;
        border: 1px solid rgba(255, 255, 255, 0.3);
        cursor: pointer;
        font-size: 0.85rem;
        font-weight: 500;
        transition: background 0.2s ease, transform 0.2s ease;
    }

    .copy-btn:hover {
        background: rgba(255, 255, 255, 0.2);
        transform: translateY(-1px);
    }

    .copy-btn.copied {
        background: rgba(34, 197, 94, 0.25);
        border-color: rgba(34, 197, 94, 0.4);
    }

    .empty-state {
        background: rgba(148, 163, 184, 0.1);
        border-radius: 12px;
        padding: 18px;
        text-align: center;
        color: #64748b;
        font-size: 0.95rem;
        margin-top: 16px;
    }

    .integration-block {
        border-top: 1px solid rgba(226, 232, 240, 0.8);
        padding-top: 24px;
    }

    .assistant-overview ul li strong {
        font-size: 0.95rem;
        color: #0f172a;
    }

    @media (max-width: 992px) {
        .info-grid {
            grid-template-columns: 1fr;
        }

        .dashboard-hero {
            padding: 28px;
        }

        .dashboard-hero h1 {
            font-size: 1.75rem;
        }
    }

    @media (max-width: 768px) {
        .dashboard-hero {
            padding: 24px;
        }

        .hero-actions {
            width: 100%;
        }

        .stats-grid {
            grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
        }

        .form-columns {
            flex-direction: column;
        }
    }

    @media (max-width: 576px) {
        .dashboard-hero {
            gap: 18px;
        }

        .widget-installation {
            padding: 18px;
        }

        .section-card {
            padding: 20px;
        }
    }
</style>
{% endblock %}
{% block content %}
<div class="dashboard-hero">
    <div class="hero-text">
        <span class="badge"><i class="fa-light fa-sparkles"></i> Welcome back</span>
        <h1>Hey {{ company[1] or 'there' }}, let's build something remarkable today.</h1>
        <p>Keep your AI assistant aligned with your brand voice, launch new campaigns, and stay on top of performance from a single, elegant workspace.</p>
        <div class="hero-actions">
            <a href="/company/files" class="action-button primary">
                <i class="fa-light fa-upload"></i>
                <span>Upload Knowledge</span>
            </a>
            <a href="/company/call-campaign" class="action-button secondary">
                <i class="fa-light fa-envelope-open-text"></i>
                <span>Launch Campaign</span>
            </a>
        </div>
    </div>
    <div class="hero-illustration">
        <img src="{{ url_for('static', filename='img/Logo.png') }}" alt="RonnieGPT Assistant Illustration">
    </div>
</div>

<div class="stats-grid">
    <div class="stat-card">
        <div class="stat-icon">
            <i class="fa-light fa-message-dots"></i>
        </div>
        <h4>{{ total_chats|default(0) }}</h4>
        <p>Total Chats</p>
    </div>
    <div class="stat-card">
        <div class="stat-icon">
            <i class="fa-light fa-calendar-check"></i>
        </div>
        <h4>{{ total_meetings|default(0) }}</h4>
        <p>Scheduled Meetings</p>
    </div>
    <div class="stat-card">
        <div class="stat-icon">
            <i class="fa-light fa-users"></i>
        </div>
        <h4>{{ total_contacts|default(0) }}</h4>
        <p>Active Contacts</p>
    </div>
    <div class="stat-card">
        <div class="stat-icon">
            <i class="fa-light fa-chart-line"></i>
        </div>
        <h4>{{ total_files|default(0) }}</h4>
        <p>Total Files Uploaded</p>
    </div>
</div>

<div class="info-grid">
    <div class="section-card company-overview">
        <div class="card-heading">
            <h2>Company Information</h2>
            <p class="subtext">Update your brand voice, greeting, and support details. These settings are shared across every touchpoint.</p>
        </div>

        <form id="profileForm" class="profile-form" enctype="multipart/form-data">
            <div class="form-group">
                <label>Instructions for AI</label>
                <textarea name="instructions" rows="10" placeholder="Share tone of voice, escalation rules, and brand pillars...">{{ company[3] or '' }}</textarea>
            </div>

            <div class="form-columns">
                <div class="form-group">
                    <label>Company's Email Address</label>
                    <input type="email" name="email" id="company-email" value="{{ company[2] or '' }}" placeholder="hello@yourbrand.com">
                </div>
                <div class="form-group">
                    <label>Welcome Message</label>
                    <input type="text" name="message" id="company-message" value="{{ company[5] or '' }}" placeholder="Hi, I'm Julia. How can I help today?">
                </div>
            </div>

            <div class="table-actions">
                <button type="submit" class="action-button primary">
                    <i class="fa-light fa-floppy-disk"></i>
                    <span>Save Profile</span>
                </button>
            </div>
        </form>

        <div class="integration-block" style="margin-top: 28px;">
            <h3>Website Intelligence</h3>
            <p class="subtext">Pull fresh content from your live site so Your AI Companion always shares the latest offers and FAQs.</p>
            <div class="form-columns" style="align-items: flex-end;">
                <div class="form-group" style="flex: 1;">
                    <label>Website URL</label>
                    <input type="url" name="websiteUrl" id="websiteUrl-input" value="{{ company[4] or '' }}" placeholder="https://example.com">
                    <input type="hidden" value="{{ company[0] }}" id="companyId">
                </div>
                <button type="button" id="crawlUrlsBtn" class="action-button primary">
                    <i class="fa-light fa-spider-web"></i>
                    <span>Crawl URLs</span>
                </button>
            </div>
        </div>

        <div class="widget-installation">
            <h5>Embed the RonnieGPT Widget</h5>
            <p class="subtext" style="margin-bottom: 10px;">Drop this script into your site footer to invite visitors into a conversation with your agent.</p>
            <div class="code-container">
                <pre><code id="widgetCode">&lt;script src="{{ request.url_root.replace('http://', 'https://') }}static/js/chat-widget.js">&lt;/script>
&lt;script>
    document.addEventListener('DOMContentLoaded', function() {
        new RonnieGPTWidget('{{ company[0] }}');
    });
&lt;/script></code></pre>
                <button type="button" class="copy-btn" onclick="copyWidgetCode()">
                    <i class="fa-light fa-copy"></i> Copy Code
                </button>
            </div>
        </div>
    </div>

    <div class="section-card assistant-overview">
        <div class="badge" style="margin-bottom: 16px;">
            <i class="fa-light fa-robot"></i>
             AI Companion
        </div>
        <h3>Assistant Snapshot</h3>
        <p class="subtext">A quick view of agent's remit, tone, and the channels she's powering for your team.</p>

        <ul class="assistant-insights" style="list-style: none; padding: 0; margin: 24px 0 0 0; display: grid; gap: 16px;">
            <li style="display: flex; gap: 12px; align-items: flex-start;">
                <span class="insight-icon" style="width: 36px; height: 36px; border-radius: 12px; background: rgba(33, 82, 255, 0.12); color: #2152ff; display: inline-flex; align-items: center; justify-content: center;"><i class="fa-light fa-feather"></i></span>
                <div>
                    <strong>Brand Voice</strong>
                    <p class="subtext" style="margin-top: 4px;">{{ (company[3] or 'No instructions provided yet.')|truncate(110, True) }}</p>
                </div>
            </li>
            <li style="display: flex; gap: 12px; align-items: flex-start;">
                <span class="insight-icon" style="width: 36px; height: 36px; border-radius: 12px; background: rgba(16, 185, 129, 0.12); color: #0f766e; display: inline-flex; align-items: center; justify-content: center;"><i class="fa-light fa-waveform-lines"></i></span>
                <div>
                    <strong>Greeting</strong>
                    <p class="subtext" style="margin-top: 4px;">{{ company[5] or 'Add a warm welcome message to set the tone.' }}</p>
                </div>
            </li>
            <li style="display: flex; gap: 12px; align-items: flex-start;">
                <span class="insight-icon" style="width: 36px; height: 36px; border-radius: 12px; background: rgba(249, 115, 22, 0.12); color: #c2410c; display: inline-flex; align-items: center; justify-content: center;"><i class="fa-light fa-globe"></i></span>
                <div>
                    <strong>Primary Domain</strong>
                    <p class="subtext" style="margin-top: 4px;">{{ company[4] or 'Connect your website to enrich Julia’s answers.' }}</p>
                </div>
            </li>
        </ul>

        <div class="quick-actions">
            <a href="/company/chat-history" class="action-button">
                <i class="fa-light fa-message"></i>
                Review Chats
            </a>
            <a href="/company/meetings" class="action-button">
                <i class="fa-light fa-calendar-days"></i>
                Manage Meetings
            </a>
            <a href="/company/files" class="action-button">
                <i class="fa-light fa-folder-open"></i>
                Knowledge Base
            </a>
            <a href="/company/settings" class="action-button">
                <i class="fa-light fa-sliders"></i>
                Assistant Settings
            </a>
        </div>

        <div class="empty-state" style="margin-top: 28px;">
            Tip: Keep agent sharp by uploading new collateral after every campaign or product update.
        </div>
    </div>
</div>
{% endblock %}

{% block extra_js %}
<script>
    function copyWidgetCode() {
        const codeElement = document.getElementById('widgetCode');
        const textArea = document.createElement('textarea');
        textArea.value = codeElement.textContent.trim();
        document.body.appendChild(textArea);
        textArea.select();

        try {
            document.execCommand('copy');
            const copyBtn = document.querySelector('.copy-btn');
            copyBtn.innerHTML = '<i class="fas fa-check"></i> Copied!';
            copyBtn.classList.add('copied');

            // Reset button after 2 seconds
            setTimeout(() => {
                copyBtn.innerHTML = '<i class="fas fa-copy"></i> Copy Code';
                copyBtn.classList.remove('copied');
            }, 2000);
        } catch (err) {
            console.error('Failed to copy text: ', err);
        }

        document.body.removeChild(textArea);
    }
</script>
{% endblock %}