@php
  use Illuminate\Support\Facades\Route;
  use Carbon\Carbon;

  $accent = $attivita?->colore ?: '#f58220';
  $logoUrl = $attivita?->logoUrl();
  $coverUrl = $attivita?->coverUrl();
  $festLogoUrl = asset('assets/img/logo_fest_L.png');
  $eventi = $attivita
    ? $attivita->eventi()->orderByDesc('data_inizio')->get()
    : collect();
  $saltacodaAttivo = $attivita
    && \App\Models\Saltacoda::where('attivita_id', $attivita->id)->where('is_attivo', true)->exists();
  $backUrl = Route::has('welcome') ? route('welcome') : url('/');
@endphp

@extends('layouts/guest')

@section('title', $attivita ? $attivita->nome : 'Attività')

@section('page-meta')
  <link rel="preconnect" href="https://fonts.googleapis.com" />
  <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin />
  <link href="https://fonts.googleapis.com/css2?family=Plus+Jakarta+Sans:ital,wght@0,400;0,500;0,600;0,700;0,800&display=swap" rel="stylesheet" />
@endsection

@section('app-css-extra')
  @vite(['resources/css/guest-client.css'])
@endsection

@section('page-style')
<style>
  html:has(.attivita-public-shell),
  html:has(.attivita-public-shell) body {
    height: 100%;
    margin: 0;
  }

  .layout-wrapper:has(.attivita-public-shell) .content-wrapper,
  .layout-wrapper:has(.attivita-public-shell) .layout-page,
  .layout-wrapper:has(.attivita-public-shell) .container-xxl {
    padding: 0 !important;
    max-width: 100% !important;
  }

  .layout-wrapper:has(.attivita-public-shell) .misc-wrapper {
    min-height: 100vh;
    min-height: 100dvh;
    width: 100%;
    max-width: 100%;
    padding: 0;
    margin: 0;
    align-items: stretch;
    justify-content: stretch;
    text-align: start;
  }

  .layout-wrapper:has(.attivita-public-shell) .row.mt-3.mb-3 {
    display: none;
  }

  .layout-wrapper:has(.attivita-public-shell) footer {
    display: none;
  }

  .attivita-public-shell {
    --card-accent: {{ $accent }};
    display: flex;
    flex-direction: column;
    width: 100%;
    min-height: 100vh;
    min-height: 100dvh;
    font-family: "Plus Jakarta Sans", "Public Sans", system-ui, sans-serif;
    background: #f4f6fb;
  }

  .attivita-public-shell__top {
    flex-shrink: 0;
    padding: 0.85rem 1.25rem;
    background: #fff;
    border-bottom: 1px solid rgba(21, 38, 92, 0.08);
  }

  .attivita-public__back {
    display: inline-flex;
    align-items: center;
    gap: 0.35rem;
    font-size: 0.875rem;
    font-weight: 600;
    color: #602d91;
    text-decoration: none;
  }

  .attivita-public__back:hover {
    color: #4a2270;
  }

  .attivita-public__grid {
    flex: 1;
    display: grid;
    grid-template-columns: minmax(280px, 42%) 1fr;
    min-height: 0;
    width: 100%;
  }

  /* Colonna sinistra — logo */
  .attivita-public__col-logo {
    position: relative;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 2.5rem 2rem;
    background: linear-gradient(
      145deg,
      color-mix(in srgb, var(--card-accent) 18%, #fff) 0%,
      color-mix(in srgb, var(--card-accent) 8%, #f0f2f8) 100%
    );
    border-right: 1px solid rgba(21, 38, 92, 0.08);
    overflow: hidden;
  }

  .attivita-public__col-logo-bg {
    position: absolute;
    inset: 0;
    width: 100%;
    height: 100%;
    object-fit: cover;
    opacity: 0.22;
  }

  .attivita-public__col-logo-shade {
    position: absolute;
    inset: 0;
    background: linear-gradient(
      160deg,
      rgba(255, 255, 255, 0.75) 0%,
      rgba(255, 255, 255, 0.35) 100%
    );
  }

  .attivita-public__logo-panel {
    position: relative;
    z-index: 1;
    width: min(100%, 320px);
    padding: 1.5rem;
    display: flex;
    align-items: center;
    justify-content: center;
    background: #fff;
    border-radius: 1.15rem;
    box-shadow: 0 12px 40px rgba(21, 38, 92, 0.12);
    border: 1px solid rgba(255, 255, 255, 0.9);
  }

  .attivita-public__logo-panel img {
    display: block;
    width: 100%;
    height: auto;
    max-height: min(40vh, 220px);
    object-fit: contain;
  }

  .attivita-public__logo-panel--fest img {
    max-height: min(36vh, 120px);
  }

  /* Colonna destra — contenuto */
  .attivita-public__col-main {
    display: flex;
    flex-direction: column;
    min-height: 0;
    overflow-y: auto;
    padding: 2rem clamp(1.25rem, 4vw, 3rem);
    background: #fff;
  }

  .attivita-public__badge {
    display: inline-block;
    font-size: 0.65rem;
    font-weight: 700;
    letter-spacing: 0.06em;
    text-transform: uppercase;
    color: #0d7a4a;
    background: rgba(13, 122, 74, 0.12);
    padding: 0.2rem 0.5rem;
    border-radius: 2rem;
    margin-bottom: 0.65rem;
  }

  .attivita-public__title {
    margin: 0 0 0.5rem;
    font-size: clamp(1.5rem, 3.5vw, 2.25rem);
    font-weight: 800;
    color: #15265c;
    letter-spacing: -0.03em;
    line-height: 1.15;
  }

  .attivita-public__tipo {
    display: inline-block;
    font-size: 0.85rem;
    font-weight: 600;
    color: #602d91;
    margin-bottom: 1rem;
  }

  .attivita-public__desc {
    font-size: 1rem;
    line-height: 1.65;
    color: #4a5568;
    margin: 0 0 1rem;
    max-width: 40rem;
  }

  .attivita-public__info {
    font-size: 0.9rem;
    line-height: 1.55;
    color: #4a5568;
    margin: 0 0 1.25rem;
    max-width: 40rem;
    padding: 0.85rem 1rem;
    border-radius: 0.65rem;
    background: #f8f9fc;
    border: 1px solid rgba(21, 38, 92, 0.06);
  }

  .attivita-public__section-title {
    font-size: 0.75rem;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: 0.08em;
    color: #6b7280;
    margin: 0 0 0.75rem;
  }

  .attivita-public__actions {
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
    max-width: 28rem;
  }

  .attivita-public__action {
    display: flex;
    align-items: center;
    gap: 0.85rem;
    padding: 0.9rem 1rem;
    border-radius: 0.75rem;
    text-decoration: none;
    color: #1a2744;
    background: #f8f9fc;
    border: 1px solid rgba(21, 38, 92, 0.08);
    transition: background 0.15s ease, border-color 0.15s ease, transform 0.15s ease;
  }

  a.attivita-public__action:hover {
    background: #fff;
    border-color: rgba(21, 38, 92, 0.14);
    color: #15265c;
    transform: translateY(-1px);
  }

  .attivita-public__action.is-disabled {
    opacity: 0.5;
    pointer-events: none;
  }

  .attivita-public__action-icon {
    flex-shrink: 0;
    width: 2.35rem;
    height: 2.35rem;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: 0.55rem;
    font-size: 1.2rem;
    background: rgba(21, 38, 92, 0.08);
    color: #15265c;
  }

  .attivita-public__action-icon--orange {
    background: rgba(245, 130, 32, 0.15);
    color: #c45f00;
  }

  .attivita-public__action strong {
    display: block;
    font-size: 0.9rem;
    font-weight: 700;
    color: #15265c;
  }

  .attivita-public__action small {
    display: block;
    font-size: 0.78rem;
    color: #6b7280;
    margin-top: 0.1rem;
  }

  .attivita-public__eventi-block {
    margin-top: 2rem;
    max-width: 32rem;
  }

  .attivita-public__eventi {
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
    list-style: none;
    padding: 0;
    margin: 0;
  }

  .attivita-public__evento {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 0.75rem;
    padding: 0.85rem 1rem;
    border-radius: 0.75rem;
    background: #f8f9fc;
    border: 1px solid rgba(21, 38, 92, 0.08);
    text-decoration: none;
    color: inherit;
    transition: border-color 0.15s ease, box-shadow 0.15s ease;
  }

  .attivita-public__evento:hover {
    border-color: rgba(96, 45, 145, 0.25);
    box-shadow: 0 4px 14px rgba(21, 38, 92, 0.06);
    color: inherit;
  }

  .attivita-public__evento-name {
    font-weight: 700;
    font-size: 0.95rem;
    color: #15265c;
    margin: 0 0 0.15rem;
  }

  .attivita-public__evento-date {
    font-size: 0.8rem;
    color: #6b7280;
    margin: 0;
  }

  .attivita-public__empty {
    flex: 1;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 2rem;
    text-align: center;
  }

  @media (max-width: 767.98px) {
    .attivita-public__grid {
      grid-template-columns: 1fr;
      grid-template-rows: auto 1fr;
    }

    .attivita-public__col-logo {
      min-height: 12rem;
      padding: 1.75rem 1.25rem;
      border-right: none;
      border-bottom: 1px solid rgba(21, 38, 92, 0.08);
    }

    .attivita-public__logo-panel {
      width: min(100%, 240px);
      padding: 1rem;
    }

    .attivita-public__col-main {
      padding: 1.5rem 1.25rem 2rem;
    }
  }
</style>
@endsection

@section('content')
<div class="attivita-public-shell">
  <header class="attivita-public-shell__top">
    <a href="{{ $backUrl }}" class="attivita-public__back">
      <i class="bx bx-arrow-back"></i>
      Bacheca eventi
    </a>
  </header>

  @if(!$attivita)
    <div class="attivita-public__empty">
      <div>
        <i class="bx bx-error-circle bx-lg text-muted mb-2 d-block"></i>
        <h1 class="h5 fw-bold mb-2">Attività non trovata</h1>
        <p class="text-muted mb-3">La scheda richiesta non esiste o non è più disponibile.</p>
        <a href="{{ $backUrl }}" class="btn btn-primary">Torna alla bacheca</a>
      </div>
    </div>
  @else
    <div class="attivita-public__grid">
      <aside class="attivita-public__col-logo" aria-label="Logo">
        @if($coverUrl)
          <img src="{{ $coverUrl }}" alt="" class="attivita-public__col-logo-bg" loading="lazy">
        @endif
        <div class="attivita-public__col-logo-shade" aria-hidden="true"></div>
        <div class="attivita-public__logo-panel {{ $logoUrl ? '' : 'attivita-public__logo-panel--fest' }}">
          @if($logoUrl)
            <img
              src="{{ $logoUrl }}"
              alt="{{ $attivita->nome }}"
              loading="lazy"
              onerror="this.src='{{ $festLogoUrl }}'; this.alt='{{ config('app.name') }}';"
            >
          @else
            <img src="{{ $festLogoUrl }}" alt="{{ config('app.name') }}">
          @endif
        </div>
      </aside>

      <main class="attivita-public__col-main">
        @if($attivita->is_attiva)
          <span class="attivita-public__badge">In programma</span>
        @endif

        <h1 class="attivita-public__title">{{ $attivita->nome }}</h1>

        @if(filled($attivita->tipo))
          <span class="attivita-public__tipo">{{ $attivita->tipo }}</span>
        @endif

        @if(filled($attivita->descrizione))
          <p class="attivita-public__desc">{{ $attivita->descrizione }}</p>
        @endif

        @if(filled($attivita->info))
          <div class="attivita-public__info">{!! nl2br(e($attivita->info)) !!}</div>
        @endif

        <section aria-labelledby="servizi-title">
          <h2 id="servizi-title" class="attivita-public__section-title">Servizi</h2>
          <div class="attivita-public__actions">
            @if($saltacodaAttivo && Route::has('cliente.saltacoda.show'))
              <a
                href="{{ route('cliente.saltacoda.show', ['attivita_id' => $attivita->id]) }}"
                class="attivita-public__action"
              >
                <span class="attivita-public__action-icon attivita-public__action-icon--orange">
                  <i class="bx bx-food-menu"></i>
                </span>
                <span>
                  <strong>Ordina (saltacoda)</strong>
                  <small>Menu e ordine dal telefono</small>
                </span>
              </a>
            @endif

            <span class="attivita-public__action is-disabled" aria-disabled="true">
              <span class="attivita-public__action-icon">
                <i class="bx bx-calendar-check"></i>
              </span>
              <span>
                <strong>Prenotazioni</strong>
                <small>Prossimamente</small>
              </span>
            </span>
          </div>
        </section>

        @if($eventi->isNotEmpty())
          <section class="attivita-public__eventi-block" aria-labelledby="eventi-title">
            <h2 id="eventi-title" class="attivita-public__section-title">Eventi</h2>
            <ul class="attivita-public__eventi">
              @foreach($eventi as $evento)
                <li>
                  @if(Route::has('evento.show'))
                    <a href="{{ route('evento.show', ['evento_id' => $evento->id]) }}" class="attivita-public__evento">
                  @else
                    <div class="attivita-public__evento">
                  @endif
                    <div>
                      <p class="attivita-public__evento-name">{{ $evento->nome }}</p>
                      @if($evento->data_inizio)
                        <p class="attivita-public__evento-date">
                          <i class="bx bx-calendar me-1"></i>
                          {{ Carbon::parse($evento->data_inizio)->locale('it')->isoFormat('D MMMM YYYY') }}
                          @if($evento->data_fine && $evento->data_fine != $evento->data_inizio)
                            – {{ Carbon::parse($evento->data_fine)->locale('it')->isoFormat('D MMMM YYYY') }}
                          @endif
                        </p>
                      @endif
                    </div>
                    @if(Route::has('evento.show'))
                      <i class="bx bx-chevron-right" aria-hidden="true"></i>
                    @endif
                  @if(Route::has('evento.show'))
                    </a>
                  @else
                    </div>
                  @endif
                </li>
              @endforeach
            </ul>
          </section>
        @endif
      </main>
    </div>
  @endif
</div>
@endsection
