{% extends "base.html" %}

{% block title %}Wyniki wyszukiwania{% if query %} - {{ query }}{% endif %} - Norda Biznes Hub{% endblock %}

{% block extra_css %}
<style>
    .search-header {
        margin-bottom: var(--spacing-xl);
    }

    .search-header h1 {
        font-size: var(--font-size-3xl);
        color: var(--text-primary);
        margin-bottom: var(--spacing-md);
    }

    .search-meta {
        color: var(--text-secondary);
        font-size: var(--font-size-lg);
    }

    .search-query {
        color: var(--primary);
        font-weight: 600;
    }

    /* Reuse company grid styles from index.html */
    .companies-grid {
        display: grid;
        grid-template-columns: repeat(auto-fill, minmax(320px, 1fr));
        gap: var(--spacing-lg);
        margin-bottom: var(--spacing-2xl);
    }

    .company-card {
        background-color: var(--surface);
        border-radius: var(--radius-lg);
        padding: var(--spacing-lg);
        box-shadow: var(--shadow);
        transition: var(--transition);
        display: flex;
        flex-direction: column;
        height: 100%;
    }

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

    .company-logo {
        width: 100%;
        height: 120px;
        background: var(--background);
        border-radius: var(--radius);
        display: flex;
        align-items: center;
        justify-content: center;
        margin-bottom: var(--spacing-md);
        overflow: hidden;
    }

    .company-logo img {
        max-width: 80%;
        max-height: 80%;
        object-fit: contain;
    }

    .company-logo-placeholder {
        font-size: 32px;
        font-weight: 700;
        color: var(--primary);
        opacity: 0.3;
    }

    .company-header {
        margin-bottom: var(--spacing-md);
    }

    .company-category {
        display: inline-block;
        padding: var(--spacing-xs) var(--spacing-sm);
        background-color: var(--background);
        color: var(--text-secondary);
        font-size: var(--font-size-sm);
        border-radius: var(--radius);
        margin-bottom: var(--spacing-sm);
    }

    .company-name {
        font-size: var(--font-size-xl);
        font-weight: 600;
        color: var(--text-primary);
        margin-bottom: var(--spacing-sm);
        text-decoration: none;
        display: block;
    }

    .company-name:hover {
        color: var(--primary);
    }

    .company-description {
        color: var(--text-secondary);
        font-size: var(--font-size-sm);
        line-height: 1.6;
        margin-bottom: var(--spacing-md);
        flex: 1;
    }

    .company-contact {
        display: flex;
        flex-direction: column;
        gap: var(--spacing-xs);
        font-size: var(--font-size-sm);
        color: var(--text-secondary);
        padding-top: var(--spacing-md);
        border-top: 1px solid var(--border);
    }

    .company-contact-item {
        display: flex;
        align-items: center;
        gap: var(--spacing-sm);
    }

    .company-contact a {
        color: var(--primary);
        text-decoration: none;
    }

    .company-contact a:hover {
        text-decoration: underline;
    }

    .empty-state {
        text-align: center;
        padding: var(--spacing-2xl);
        background: white;
        border-radius: var(--radius-xl);
        box-shadow: var(--shadow);
    }

    .empty-state svg {
        opacity: 0.3;
        margin-bottom: var(--spacing-md);
    }

    .empty-state h2 {
        margin-bottom: var(--spacing-md);
    }

    /* People results */
    .people-section {
        margin-bottom: var(--spacing-xl);
    }

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

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

    .people-grid {
        display: grid;
        grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
        gap: var(--spacing-md);
    }

    .person-card {
        background: white;
        border-radius: var(--radius-lg);
        padding: var(--spacing-md);
        box-shadow: var(--shadow);
        display: flex;
        align-items: center;
        gap: var(--spacing-md);
        text-decoration: none;
        transition: var(--transition);
        border-left: 4px solid #667eea;
    }

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

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

    .person-info {
        flex: 1;
        min-width: 0;
    }

    .person-name-link {
        font-weight: 600;
        color: var(--text-primary);
        margin-bottom: 2px;
        display: flex;
        align-items: center;
        gap: var(--spacing-xs);
    }

    .person-meta {
        font-size: var(--font-size-sm);
        color: var(--text-secondary);
    }

    @media (max-width: 768px) {
        .companies-grid {
            grid-template-columns: 1fr;
        }
        .people-grid {
            grid-template-columns: 1fr;
        }
    }
</style>
{% endblock %}

{% block content %}
<div class="search-header">
    <h1>Wyniki wyszukiwania</h1>
    {% if query %}
    <p class="search-meta">
        Znaleziono <strong>{{ companies|length }}</strong> firm
        {%- if people %} i <strong>{{ people|length }}</strong> osób{% endif %}
        dla zapytania: <span class="search-query">"{{ query }}"</span>
    </p>
    {% else %}
    <p class="search-meta">
        Znaleziono <strong>{{ companies|length }}</strong> firm
    </p>
    {% endif %}
</div>

{% if people %}
<div class="people-section">
    <h2 class="section-title-small">
        <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="M17 20h5v-2a3 3 0 00-5.356-1.857M17 20H7m10 0v-2c0-.656-.126-1.283-.356-1.857M7 20H2v-2a3 3 0 015.356-1.857M7 20v-2c0-.656.126-1.283.356-1.857m0 0a5.002 5.002 0 019.288 0M15 7a3 3 0 11-6 0 3 3 0 016 0zm6 3a2 2 0 11-4 0 2 2 0 014 0zM7 10a2 2 0 11-4 0 2 2 0 014 0z"/>
        </svg>
        Osoby ({{ people|length }})
    </h2>
    <div class="people-grid">
        {% for person in people %}
        <a href="{{ url_for('person_detail', person_id=person.id) }}" class="person-card">
            <div class="person-avatar-small">
                {{ person.imiona[0] }}{{ person.nazwisko[0] }}
            </div>
            <div class="person-info">
                <div class="person-name-link">
                    {{ person.imiona }} {{ person.nazwisko }}
                    <svg width="14" height="14" fill="none" stroke="currentColor" viewBox="0 0 24 24" style="opacity: 0.4;">
                        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 5l7 7-7 7"/>
                    </svg>
                </div>
                <div class="person-meta">
                    Powiazany z {{ person.company_count }} firmami
                </div>
            </div>
        </a>
        {% endfor %}
    </div>
</div>
{% endif %}

{% if companies %}
<div class="companies-grid">
    {% for company in companies %}
    <div class="company-card">
        <a href="{{ url_for('company_detail', company_id=company.id) }}" class="company-logo">
            <img src="{{ url_for('static', filename='img/companies/' ~ company.slug ~ '.webp') }}"
                 alt="{{ company.name }}"
                 onerror="this.parentElement.innerHTML='<span class=\'company-logo-placeholder\'>{{ company.name[:2]|upper }}</span>'">
        </a>
        <div class="company-header">
            {% if company.category %}
            <span class="company-category">{{ company.category.name }}</span>
            {% endif %}
            <a href="{{ url_for('company_detail', company_id=company.id) }}" class="company-name">
                {{ company.name }}
            </a>
        </div>

        <div class="company-description">
            {{ company.description_short|truncate(150) if company.description_short else 'Brak opisu' }}
        </div>

        <div class="company-contact">
            {% if company.website %}
            <div class="company-contact-item">
                <svg width="16" height="16" fill="none" stroke="currentColor" stroke-width="2">
                    <circle cx="8" cy="8" r="7"/>
                    <path d="M1 8h14M8 1a11 11 0 0 1 0 14 11 11 0 0 1 0-14"/>
                </svg>
                <a href="{{ company.website }}" target="_blank" rel="noopener noreferrer">
                    {{ company.website|replace('https://', '')|replace('http://', '')|replace('www.', '')|truncate(30) }}
                </a>
            </div>
            {% endif %}

            {% if company.phone %}
            <div class="company-contact-item">
                <svg width="16" height="16" fill="none" stroke="currentColor" stroke-width="2">
                    <path d="M3 2a1 1 0 0 0-1 1v10a1 1 0 0 0 1 1h10a1 1 0 0 0 1-1V3a1 1 0 0 0-1-1H3z"/>
                </svg>
                <a href="tel:{{ company.phone }}">{{ company.phone }}</a>
            </div>
            {% endif %}

            {% if company.address_city %}
            <div class="company-contact-item">
                <svg width="16" height="16" fill="none" stroke="currentColor" stroke-width="2">
                    <path d="M8 2l6 6-6 6-6-6 6-6z"/>
                </svg>
                {{ company.address_city }}
            </div>
            {% endif %}
        </div>
    </div>
    {% endfor %}
</div>
{% endif %}

{% if not companies and not people %}
<div class="empty-state">
    <svg width="120" height="120" viewBox="0 0 120 120" fill="none">
        <circle cx="60" cy="60" r="50" stroke="currentColor" stroke-width="4"/>
        <circle cx="50" cy="50" r="15" stroke="currentColor" stroke-width="4"/>
        <path d="M62 62l20 20" stroke="currentColor" stroke-width="4" stroke-linecap="round"/>
        <path d="M40 50h20M50 40v20" stroke="currentColor" stroke-width="3" stroke-linecap="round"/>
    </svg>
    <h2>Nie znaleziono wyników</h2>
    <p style="color: var(--text-secondary); margin-bottom: var(--spacing-lg);">
        {% if query %}
        Spróbuj użyć innych słów kluczowych lub skorzystaj z AI chatu aby znaleźć odpowiednią firmę lub osobę.
        {% else %}
        Nie znaleziono firm ani osób spełniających kryteria wyszukiwania.
        {% endif %}
    </p>
    <div style="display: flex; gap: var(--spacing-md); justify-content: center;">
        <a href="{{ url_for('index') }}" class="btn btn-outline">Katalog firm</a>
        <a href="{{ url_for('chat') }}" class="btn btn-primary">Zapytaj AI</a>
    </div>
</div>
{% endif %}
{% endblock %}
