{% extends "base.html" %}

{% block title %}{{ person.full_name() }} - Norda Biznes Hub{% endblock %}

{% block extra_css %}
<style>
    .person-header {
        background: white;
        padding: var(--spacing-2xl);
        border-radius: var(--radius-xl);
        box-shadow: var(--shadow-lg);
        margin-bottom: var(--spacing-xl);
    }

    .person-avatar {
        width: 120px;
        height: 120px;
        background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        border-radius: 50%;
        display: flex;
        align-items: center;
        justify-content: center;
        font-size: 48px;
        color: white;
        font-weight: 600;
        margin-bottom: var(--spacing-lg);
    }

    .person-name {
        font-size: var(--font-size-3xl);
        color: var(--text-primary);
        margin-bottom: var(--spacing-sm);
    }

    .person-subtitle {
        font-size: var(--font-size-lg);
        color: var(--text-secondary);
        margin-bottom: var(--spacing-lg);
    }

    .person-section {
        background: white;
        padding: var(--spacing-xl);
        border-radius: var(--radius-xl);
        box-shadow: var(--shadow);
        margin-bottom: var(--spacing-xl);
    }

    .section-title {
        font-size: var(--font-size-xl);
        color: var(--text-primary);
        margin-bottom: var(--spacing-lg);
        padding-bottom: var(--spacing-md);
        border-bottom: 2px solid var(--border);
        display: flex;
        align-items: center;
        gap: var(--spacing-sm);
    }

    .section-title svg {
        color: var(--primary);
    }

    .company-role-card {
        display: flex;
        align-items: flex-start;
        padding: var(--spacing-lg);
        background: var(--background);
        border-radius: var(--radius-lg);
        margin-bottom: var(--spacing-md);
        border-left: 4px solid var(--primary);
        transition: transform 0.2s, box-shadow 0.2s;
    }

    .company-role-card:hover {
        transform: translateX(4px);
        box-shadow: var(--shadow);
    }

    .company-role-card a {
        text-decoration: none;
        color: inherit;
        flex: 1;
    }

    .company-name-link {
        font-size: var(--font-size-lg);
        font-weight: 600;
        color: var(--primary);
        margin-bottom: var(--spacing-xs);
    }

    .company-name-link:hover {
        text-decoration: underline;
    }

    .role-badge {
        display: inline-block;
        padding: 4px 12px;
        border-radius: 20px;
        font-size: var(--font-size-sm);
        font-weight: 500;
        color: white;
        margin-right: var(--spacing-sm);
        margin-top: var(--spacing-xs);
    }

    .role-badge.zarzad { background: #e74c3c; }
    .role-badge.wspolnik { background: #2ecc71; }
    .role-badge.prokurent { background: #f39c12; }
    .role-badge.wlasciciel_jdg { background: #9b59b6; }

    .portal-info {
        display: grid;
        grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
        gap: var(--spacing-md);
    }

    .portal-info-item {
        display: flex;
        align-items: center;
        gap: var(--spacing-sm);
        padding: var(--spacing-md);
        background: var(--background);
        border-radius: var(--radius);
    }

    .portal-info-item svg {
        color: var(--primary);
        flex-shrink: 0;
    }

    .portal-info-item a {
        color: var(--primary);
        text-decoration: none;
    }

    .portal-info-item a:hover {
        text-decoration: underline;
    }

    .data-source {
        font-size: var(--font-size-sm);
        color: var(--text-secondary);
        margin-top: var(--spacing-lg);
        padding-top: var(--spacing-md);
        border-top: 1px solid var(--border);
    }

    .data-source a {
        color: var(--primary);
    }

    .no-portal-data {
        padding: var(--spacing-lg);
        background: #fef3c7;
        border-radius: var(--radius);
        color: #92400e;
        font-size: var(--font-size-sm);
    }

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

    .back-link:hover {
        color: var(--primary);
    }
</style>
{% endblock %}

{% block content %}
<div class="main-container">
    <a href="#" onclick="history.back(); return false;" class="back-link">
        <svg width="16" height="16" fill="none" stroke="currentColor" viewBox="0 0 24 24">
            <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M10 19l-7-7m0 0l7-7m-7 7h18"/>
        </svg>
        Wstecz
    </a>

    <!-- Person Header -->
    <div class="person-header">
        <div class="person-avatar">
            {{ person.imiona[0] }}{{ person.nazwisko[0] }}
        </div>
        <h1 class="person-name">{{ person.full_name() }}</h1>
        {% set unique_company_ids = company_roles|map(attribute='company_id')|list|unique|list %}
        <p class="person-subtitle">
            Powiazany z {{ unique_company_ids|length }} firmami ({{ company_roles|length }} ról) w Norda Biznes
        </p>
    </div>

    <!-- Portal Data (if available) -->
    {% if portal_user %}
    <div class="person-section">
        <h2 class="section-title">
            <svg width="24" height="24" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M16 7a4 4 0 11-8 0 4 4 0 018 0zM12 14a7 7 0 00-7 7h14a7 7 0 00-7-7z"/>
            </svg>
            Dane kontaktowe (z konta na portalu)
        </h2>
        <div class="portal-info">
            {% if portal_user.email %}
            <div class="portal-info-item">
                <svg width="20" height="20" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                    <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M3 8l7.89 5.26a2 2 0 002.22 0L21 8M5 19h14a2 2 0 002-2V7a2 2 0 00-2-2H5a2 2 0 00-2 2v10a2 2 0 002 2z"/>
                </svg>
                <a href="mailto:{{ portal_user.email }}">{{ portal_user.email }}</a>
            </div>
            {% endif %}
            {% if portal_user.phone %}
            <div class="portal-info-item">
                <svg width="20" height="20" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                    <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M3 5a2 2 0 012-2h3.28a1 1 0 01.948.684l1.498 4.493a1 1 0 01-.502 1.21l-2.257 1.13a11.042 11.042 0 005.516 5.516l1.13-2.257a1 1 0 011.21-.502l4.493 1.498a1 1 0 01.684.949V19a2 2 0 01-2 2h-1C9.716 21 3 14.284 3 6V5z"/>
                </svg>
                <a href="tel:{{ portal_user.phone }}">{{ portal_user.phone }}</a>
            </div>
            {% endif %}
            {% if portal_user.company %}
            <div class="portal-info-item">
                <svg width="20" height="20" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                    <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M19 21V5a2 2 0 00-2-2H7a2 2 0 00-2 2v16m14 0h2m-2 0h-5m-9 0H3m2 0h5M9 7h1m-1 4h1m4-4h1m-1 4h1m-5 10v-5a1 1 0 011-1h2a1 1 0 011 1v5m-4 0h4"/>
                </svg>
                <span>{{ portal_user.company.name if portal_user.company else '-' }}</span>
            </div>
            {% endif %}
        </div>
    </div>
    {% else %}
    <div class="person-section">
        <h2 class="section-title">
            <svg width="24" height="24" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M16 7a4 4 0 11-8 0 4 4 0 018 0zM12 14a7 7 0 00-7 7h14a7 7 0 00-7-7z"/>
            </svg>
            Dane kontaktowe
        </h2>
        <div class="no-portal-data">
            Ta osoba nie ma jeszcze konta na portalu Norda Biznes Hub. Dane kontaktowe pojawia sie po rejestracji.
        </div>
    </div>
    {% endif %}

    <!-- Company Roles -->
    <div class="person-section">
        <h2 class="section-title">
            <svg width="24" height="24" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M19 21V5a2 2 0 00-2-2H7a2 2 0 00-2 2v16m14 0h2m-2 0h-5m-9 0H3m2 0h5M9 7h1m-1 4h1m4-4h1m-1 4h1m-5 10v-5a1 1 0 011-1h2a1 1 0 011 1v5m-4 0h4"/>
            </svg>
            Powiazania z firmami
        </h2>

        {% set unique_companies = {} %}
        {% for role in company_roles %}
            {% if role.company.id not in unique_companies %}
                {% set _ = unique_companies.update({role.company.id: {'company': role.company, 'roles': []}}) %}
            {% endif %}
            {% set _ = unique_companies[role.company.id]['roles'].append(role) %}
        {% endfor %}

        {% for company_id, data in unique_companies.items() %}
        <div class="company-role-card">
            <a href="{{ url_for('company_detail', company_id=data.company.id) }}">
                <div class="company-name-link">{{ data.company.name }}</div>
                <div>
                    {% for role in data.roles %}
                    <span class="role-badge {{ role.role_category }}">{{ role.role }}</span>
                    {% endfor %}
                </div>
                {% if data.company.address_city %}
                <div style="font-size: var(--font-size-sm); color: var(--text-secondary); margin-top: var(--spacing-xs);">
                    {{ data.company.address_city }}
                </div>
                {% endif %}
            </a>
        </div>
        {% endfor %}

        <div class="data-source">
            Dane urzedowe pochodza z:
            <a href="https://ekrs.ms.gov.pl" target="_blank">ekrs.ms.gov.pl</a> (KRS),
            <a href="https://dane.biznes.gov.pl" target="_blank">dane.biznes.gov.pl</a> (CEIDG)
        </div>
    </div>
</div>
{% endblock %}
