@php
  use Illuminate\Support\Facades\Route;
  use App\Models\Saltacoda;
  use App\Models\Attivita;

  /** @var \App\Models\Attivita|null $attivita */
  $accent = $attivita?->colore ?: '#f58220';
  $logoUrl = $attivita?->logoDisplayUrl();
  $hasCustomCover = $attivita?->hasCustomCover() ?? false;
  $coverUrl = $hasCustomCover ? $attivita->coverUrl() : null;
  $festLogoFallback = Attivita::defaultLogoUrl();
  $festLogoLarge = asset('assets/img/logo_fest_L.png');
  $backUrl = Route::has('welcome') ? route('welcome') : url('/');

  $saltacodaAttivo = $attivita
    && Saltacoda::query()
      ->where('attivita_id', $attivita->id)
      ->where('is_attivo', true)
      ->exists();
@endphp

@extends('layouts/guest')

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

@section('page-meta')
  <script>document.documentElement.classList.add('attivita-public-page');</script>
  <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;1,400&display=swap" rel="stylesheet" />
@endsection

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

  html.attivita-public-page .layout-wrapper,
  html.attivita-public-page .content-wrapper,
  html.attivita-public-page .layout-page,
  html.attivita-public-page .container-xxl {
    padding: 0 !important;
    margin: 0 !important;
    max-width: 100% !important;
  }

  html.attivita-public-page .d-flex.flex-column.min-vh-100 {
    min-height: 100vh !important;
    min-height: 100dvh !important;
  }

  html.attivita-public-page .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;
    background: transparent;
  }

  html.attivita-public-page .row.mt-3.mb-3,
  html.attivita-public-page footer {
    display: none !important;
  }

  .attivita-public {
    --card-accent: {{ $accent }};
    --text-main: #15265c;
    --text-muted: #5b6579;
    --purple: #602d91;
    --orange: #f58220;

    position: relative;
    min-height: 100vh;
    min-height: 100dvh;
    display: flex;
    flex-direction: column;
    font-family: "Plus Jakarta Sans", "Public Sans", system-ui, sans-serif;
  }

  .attivita-public__bg {
    position: fixed;
    inset: 0;
    z-index: 0;
    pointer-events: none;
    background: linear-gradient(145deg, #fff6ee 0%, #f3ebf8 38%, #e6edf8 100%);
  }

  .attivita-public__bg::before {
    content: "";
    position: absolute;
    inset: 0;
    background:
      radial-gradient(ellipse 70% 50% at 8% 18%, color-mix(in srgb, var(--card-accent) 35%, transparent) 0%, transparent 58%),
      radial-gradient(ellipse 65% 45% at 92% 82%, rgba(96, 45, 145, 0.22) 0%, transparent 55%);
  }

  .attivita-public__top {
    position: relative;
    z-index: 2;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 1rem;
    padding: calc(.85rem + env(safe-area-inset-top, 0px)) 1.15rem .85rem;
    background: rgba(255, 255, 255, .92);
    backdrop-filter: blur(8px);
    border-bottom: 1px solid rgba(21, 38, 92, .08);
    box-shadow: 0 1px 10px rgba(21, 38, 92, .05);
  }

  .attivita-public__top::after {
    content: "";
    position: absolute;
    left: 0;
    right: 0;
    bottom: 0;
    height: 3px;
    background: linear-gradient(90deg, var(--card-accent) 0%, var(--purple) 55%, #15265c 100%);
    opacity: .85;
  }

  .attivita-public__back {
    display: inline-flex;
    align-items: center;
    gap: .4rem;
    font-size: .88rem;
    font-weight: 600;
    color: #15265c;
    text-decoration: none;
    transition: color .15s ease;
  }

  .attivita-public__back:hover {
    color: var(--orange);
  }

  .attivita-public__brand img {
    display: block;
    height: 1.65rem;
    width: auto;
    opacity: .92;
  }

  .attivita-public__layout {
    position: relative;
    z-index: 1;
    flex: 1;
    min-height: 0;
    display: grid;
    grid-template-columns: minmax(300px, 50%) 1fr;
  }

  .attivita-public__media {
    position: relative;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 1.5rem 1rem;
    overflow: hidden;
  }

  .attivita-public__media-glow {
    position: absolute;
    width: min(70vw, 420px);
    height: min(70vw, 420px);
    border-radius: 50%;
    background: color-mix(in srgb, var(--card-accent) 22%, transparent);
    filter: blur(70px);
    opacity: .75;
    pointer-events: none;
  }

  .attivita-public__poster-wrap {
    position: relative;
    display: flex;
    align-items: center;
    justify-content: center;
    width: min(100%, 480px);
    aspect-ratio: 210 / 297;
    max-height: min(88vh, 680px);
    border-radius: .95rem;
    box-shadow:
      0 16px 44px rgba(21, 38, 92, .14),
      0 0 0 1px rgba(255, 255, 255, .65);
    overflow: hidden;
    background: #e8ecf4;
  }

  .attivita-public__poster-wrap::after {
    content: "";
    position: absolute;
    inset: 0;
    pointer-events: none;
    box-shadow: inset 0 0 0 1px rgba(21, 38, 92, .08);
  }

  .attivita-public__poster-wrap--placeholder {
    background: linear-gradient(
      165deg,
      color-mix(in srgb, var(--card-accent) 28%, #fff) 0%,
      color-mix(in srgb, var(--card-accent) 12%, #eef1f7) 38%,
      #e8ecf4 68%,
      color-mix(in srgb, var(--purple) 16%, #e8ecf4) 100%
    );
  }

  .attivita-public__poster {
    display: block;
    width: 100%;
    height: 100%;
    object-fit: cover;
    object-position: center top;
  }

  .attivita-public__poster-placeholder {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 100%;
    height: 100%;
    padding: 2.5rem 1.75rem;
  }

  .attivita-public__poster-placeholder img {
    width: min(72%, 8rem);
    height: auto;
    object-fit: contain;
    opacity: .95;
    filter: drop-shadow(0 8px 18px rgba(21, 38, 92, .16));
  }

  .attivita-public__main {
    display: flex;
    flex-direction: column;
    justify-content: center;
    padding: 1.9rem clamp(1.2rem, 4vw, 3rem);
    padding-bottom: calc(1.9rem + env(safe-area-inset-bottom, 0px));
  }

  .attivita-public__panel {
    max-width: 34rem;
    padding: 1.35rem 1.4rem 1.5rem;
    background: rgba(255, 255, 255, .94);
    border: 1px solid rgba(21, 38, 92, .08);
    border-radius: 1rem;
    border-top: 3px solid var(--card-accent);
    box-shadow: 0 10px 32px rgba(21, 38, 92, .08);
  }

  .attivita-public__head {
    display: flex;
    align-items: center;
    flex-wrap: wrap;
    gap: .65rem;
    margin-bottom: .85rem;
  }

  .attivita-public__logo {
    flex-shrink: 0;
    width: 2.85rem;
    height: 2.85rem;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: .6rem;
    background: #fff;
    border: 1px solid rgba(21, 38, 92, .08);
    box-shadow: 0 2px 8px rgba(21, 38, 92, .06);
    overflow: hidden;
  }

  .attivita-public__logo img {
    width: 88%;
    height: 88%;
    object-fit: contain;
  }

  .attivita-public__badge {
    display: inline-flex;
    align-items: center;
    gap: .3rem;
    width: fit-content;
    font-size: .68rem;
    font-weight: 700;
    letter-spacing: .06em;
    text-transform: uppercase;
    color: #0d7a4a;
    background: rgba(13, 122, 74, .1);
    padding: .22rem .55rem;
    border-radius: 999px;
    margin: 0;
  }

  .attivita-public__kicker {
    margin: 0 0 .35rem;
    font-size: .72rem;
    font-weight: 700;
    letter-spacing: .08em;
    text-transform: uppercase;
    color: color-mix(in srgb, var(--card-accent) 70%, var(--purple));
  }

  .attivita-public__title {
    margin: 0 0 .55rem;
    font-size: clamp(1.5rem, 3.8vw, 2.35rem);
    line-height: 1.12;
    letter-spacing: -.03em;
    font-weight: 800;
    color: var(--text-main);
  }

  .attivita-public__desc {
    margin: 0 0 1.25rem;
    color: var(--text-muted);
    line-height: 1.65;
    font-size: .95rem;
  }

  .attivita-public__actions {
    display: flex;
    flex-direction: column;
    gap: .65rem;
  }

  .attivita-public__actions-hint {
    margin: 0 0 .1rem;
    font-size: .72rem;
    font-weight: 700;
    letter-spacing: .06em;
    text-transform: uppercase;
    color: var(--text-muted);
  }

  .attivita-public__action {
    display: flex;
    align-items: center;
    gap: .85rem;
    padding: .9rem 1rem;
    border-radius: .75rem;
    text-decoration: none;
    color: #1a2744;
    background: #fff;
    border: 1px solid rgba(21, 38, 92, .08);
    box-shadow: 0 1px 4px rgba(21, 38, 92, .05);
    transition: transform .15s ease, box-shadow .15s ease, border-color .15s ease;
  }

  .attivita-public__action:hover {
    color: #15265c;
    transform: translateY(-1px);
    border-color: color-mix(in srgb, var(--card-accent) 28%, rgba(21, 38, 92, .16));
    box-shadow: 0 6px 16px rgba(21, 38, 92, .08);
  }

  .attivita-public__action--primary {
    background: linear-gradient(
      135deg,
      color-mix(in srgb, var(--card-accent) 92%, #fff) 0%,
      color-mix(in srgb, var(--card-accent) 78%, var(--purple)) 100%
    );
    border-color: transparent;
    color: #fff;
    box-shadow: 0 8px 22px color-mix(in srgb, var(--card-accent) 35%, transparent);
  }

  .attivita-public__action--primary:hover {
    color: #fff;
    transform: translateY(-2px);
    box-shadow: 0 12px 28px color-mix(in srgb, var(--card-accent) 42%, transparent);
  }

  .attivita-public__action--primary strong,
  .attivita-public__action--primary small {
    color: #fff;
  }

  .attivita-public__action--primary .attivita-public__action-icon {
    background: rgba(255, 255, 255, .22);
    color: #fff;
  }

  .attivita-public__action-icon {
    flex-shrink: 0;
    width: 2.25rem;
    height: 2.25rem;
    border-radius: .58rem;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    font-size: 1.12rem;
    color: var(--text-main);
    background: rgba(21, 38, 92, .08);
  }

  .attivita-public__action-icon--purple {
    color: var(--purple);
    background: rgba(96, 45, 145, .12);
  }

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

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

  .attivita-public__foot {
    position: relative;
    z-index: 1;
    padding: .85rem 1.15rem calc(.85rem + env(safe-area-inset-bottom, 0px));
    text-align: center;
    font-size: .76rem;
    color: #6b7280;
  }

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

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

    .attivita-public__media {
      padding: 1.15rem 1rem .35rem;
    }

    .attivita-public__poster-wrap {
      width: min(92vw, 400px);
      max-height: none;
    }

    .attivita-public__main {
      justify-content: flex-start;
      padding: 1.15rem 1rem 1.5rem;
    }

    .attivita-public__panel {
      max-width: none;
      padding: 1.15rem 1.1rem 1.25rem;
    }
  }
</style>
@endsection

@section('content')
<div class="attivita-public">
  <div class="attivita-public__bg" aria-hidden="true"></div>

  <header class="attivita-public__top">
    <a href="{{ $backUrl }}" class="attivita-public__back">
      <i class="bx bx-arrow-back"></i>
      Bacheca eventi
    </a>
    <a href="{{ $backUrl }}" class="attivita-public__brand" aria-label="{{ config('app.name') }}">
      <img src="{{ $festLogoLarge }}" alt="{{ config('app.name') }}" loading="lazy">
    </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 è disponibile.</p>
        <a href="{{ $backUrl }}" class="btn btn-primary">Torna alla bacheca</a>
      </div>
    </div>
  @else
    <div class="attivita-public__layout">
      <aside class="attivita-public__media" aria-label="Locandina evento">
        <div class="attivita-public__media-glow" aria-hidden="true"></div>
        <div class="attivita-public__poster-wrap {{ $hasCustomCover ? '' : 'attivita-public__poster-wrap--placeholder' }}">
          @if($hasCustomCover)
            <img
              src="{{ $coverUrl }}"
              alt=""
              class="attivita-public__poster"
              loading="lazy"
            >
          @else
            <span class="attivita-public__poster-placeholder" aria-hidden="true">
              <img src="{{ $festLogoFallback }}" alt="" loading="lazy">
            </span>
          @endif
        </div>
      </aside>

      <main class="attivita-public__main">
        <div class="attivita-public__panel">
          <div class="attivita-public__head">
            <span class="attivita-public__logo" aria-hidden="true">
              <img
                src="{{ $logoUrl }}"
                alt=""
                loading="lazy"
                data-fest-logo-fallback="{{ $festLogoFallback }}"
                onerror="if (this.dataset.festLogoFallback && this.src !== this.dataset.festLogoFallback) { this.src = this.dataset.festLogoFallback; }"
              >
            </span>
            @if($attivita->is_attiva)
              <span class="attivita-public__badge"><i class="bx bx-check-circle"></i> In programma</span>
            @endif
          </div>

          <p class="attivita-public__kicker">Evento in bacheca</p>
          <h1 class="attivita-public__title">{{ $attivita->nome }}</h1>

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

          <div class="attivita-public__actions">
            <p class="attivita-public__actions-hint">Cosa vuoi fare?</p>
            @if($saltacodaAttivo)
              <a
                href="{{ route('cliente.saltacoda.show', ['attivita_id' => $attivita->id]) }}"
                class="attivita-public__action attivita-public__action--primary"
              >
                <span class="attivita-public__action-icon">
                  <i class="bx bx-food-menu"></i>
                </span>
                <span>
                  <strong>Ordina (saltacoda)</strong>
                  <small>Menu e ordine dal telefono</small>
                </span>
              </a>
            @endif

            <a
              href="{{ route('cliente.prenotazione-tavolo.index', ['attivita_id' => $attivita->id]) }}"
              class="attivita-public__action"
            >
              <span class="attivita-public__action-icon attivita-public__action-icon--purple">
                <i class="bx bx-calendar-check"></i>
              </span>
              <span>
                <strong>Prenota tavolo</strong>
                <small>Invia la richiesta di prenotazione</small>
              </span>
            </a>
          </div>
        </div>
      </main>
    </div>

    <footer class="attivita-public__foot">
      {{ config('app.name') }} · la bacheca digitale per feste e sagre
    </footer>
  @endif
</div>
@endsection
