---
import SectionHeading from './SectionHeading.astro';
import type { FuelPrice } from '../lib/fallback-data';

type Props = {
  contactHref?: string;
  fuelPrices: FuelPrice[];
  moreInfoHref?: string;
  sectionId?: string;
  serviceNotes?: string[];
};

const { contactHref, fuelPrices, moreInfoHref, sectionId, serviceNotes = [] } = Astro.props as Props;

function formatFuelPrice(value: unknown): string {
  const numeric = Number(value);
  return Number.isFinite(numeric) ? numeric.toFixed(2) : '0.00';
}

function formatVatLabel(value: unknown): string {
  return value === true || value === 1 || value === '1' || value === 'true' ? 'Inc VAT' : 'Ex VAT';
}
---

<section id={sectionId}>
  <SectionHeading  title="Fuel at Swansea"  />
  {serviceNotes.length > 0 && (
    <div class="fuel-service-notes">
      {serviceNotes.map((note) => <p>{note}</p>)}
      {contactHref && (
        <a class="button primary fuel-contact-link" href={contactHref}>Contact Us</a>
      )}
    </div>
  )}
  <div class="cards-grid fuel-cards-grid">
    {fuelPrices.map((fuel) => (
      <article class="card fuel-card">
        <div class="fuel-row">
          <p class="fuel-type">{fuel.fuel_type}</p>
          <div class="fuel-price-group">
            <h3 class="fuel-price">
              {fuel.currency} {formatFuelPrice(fuel.price_per_litre)}
              <span>/ litre</span>
              <span class="vat-label">{formatVatLabel(fuel.inc_vat)}</span>
            </h3>
          </div>
        </div>
        {fuel.notes && <p>{fuel.notes}</p>}
      </article>
    ))}
  </div>
  {moreInfoHref && (
    <div class="fuel-more-info">
      <a class="button primary fuel-info-link" href={moreInfoHref}>
        More fuel information
        <span aria-hidden="true">-&gt;</span>
      </a>
    </div>
  )}
</section>

<style>
  .fuel-cards-grid {
    gap: 1.1rem;
  }

  .fuel-service-notes {
    margin: 0 0 1.9rem;
  }

  .fuel-service-notes p {
    margin: 0 0 0.65rem;
  }

  .fuel-service-notes p:last-child {
    margin-bottom: 0;
  }

  .fuel-contact-link {
    display: flex;
    width: fit-content;
    margin-top: 0.55rem;
    margin-inline: auto;
  }

  .fuel-more-info {
    margin-top: 1rem;
    display: flex;
    justify-content: flex-end;
  }

  .fuel-info-link {
    gap: 0.5rem;
    padding-inline: 1.15rem;
    box-shadow: 0 12px 24px rgba(11, 79, 122, 0.18);
    transition:
      background 0.18s ease,
      box-shadow 0.18s ease,
      transform 0.18s ease;
  }

  .fuel-info-link span {
    font-weight: 800;
    transition: transform 0.18s ease;
  }

  .fuel-info-link:hover,
  .fuel-info-link:focus-visible {
    color: white;
    background: var(--brand-2);
    box-shadow: 0 14px 28px rgba(11, 79, 122, 0.24);
    transform: translateY(-1px);
  }

  .fuel-info-link:hover span,
  .fuel-info-link:focus-visible span {
    transform: translateX(2px);
  }

  .fuel-card {
    border: 1px solid rgba(11, 79, 122, 0.18);
    background:
      linear-gradient(90deg, rgba(11, 79, 122, 0.11), transparent 38%),
      linear-gradient(180deg, rgba(255, 255, 255, 0.98), rgba(232, 243, 253, 0.94));
  }

  .fuel-row {
    display: grid;
    grid-template-columns: minmax(7rem, 0.75fr) minmax(0, 1.25fr);
    gap: 1rem;
    align-items: center;
  }

  .fuel-type {
    margin: 0;
    font-size: clamp(1.45rem, 2.5vw, 2rem);
    line-height: 1;
    text-transform: uppercase;
    font-weight: 800;
    color: var(--brand);
    overflow-wrap: anywhere;
  }

  .fuel-price-group {
    display: grid;
    justify-items: end;
    text-align: right;
  }

  .fuel-price {
    display: flex;
    flex-wrap: wrap;
    justify-content: flex-end;
    align-items: baseline;
    gap: 0.42rem;
    margin: 0;
    font-size: clamp(1.7rem, 3vw, 2.25rem);
    line-height: 1.1;
    color: var(--text);
  }

  .fuel-price span {
    font-size: 0.9rem;
    font-weight: 600;
    color: var(--muted);
  }

  .vat-label {
    display: inline-flex;
    align-items: center;
    padding: 0.28rem 0.7rem;
    border-radius: 999px;
    background: rgba(11, 79, 122, 0.1);
    color: var(--brand);
    font-size: 0.82rem;
    font-weight: 800;
  }

  @media (max-width: 560px) {
    .fuel-more-info {
      justify-content: stretch;
    }

    .fuel-info-link {
      width: 100%;
    }

    .fuel-row {
      grid-template-columns: 1fr;
      gap: 0.75rem;
    }

    .fuel-price-group {
      justify-items: start;
      text-align: left;
    }

    .fuel-price {
      justify-content: flex-start;
    }
  }
</style>
