<script lang="ts">
  import ScoreBar from '$lib/components/ScoreBar.svelte';
  import SongPlayer from '$lib/components/SongPlayer.svelte';

  const beatsaverMapBase = 'https://beatsaver.com/maps/';

  export let hash: string;
  export let coverURL: string | undefined = undefined;
  export let songName: string | undefined = undefined;
  export let mapper: string | undefined = undefined;
  export let beatsaverKey: string | undefined = undefined;
  export let score: number | null | undefined = undefined;

  $: mapTitle = songName ?? hash;
  $: mapLink = beatsaverKey ? `${beatsaverMapBase}${beatsaverKey}` : null;
</script>

<article class="playlist-map-card card-surface">
  <div class="cover">
    {#if coverURL}
      <img src={coverURL} alt={`Cover art for ${mapTitle}`} loading="lazy" />
    {:else}
      <div class="placeholder" aria-hidden="true">☁️</div>
    {/if}
  </div>

  <div class="content">
    <div class="title" title={mapTitle}>
      {#if mapLink}
        <a href={mapLink} target="_blank" rel="noreferrer noopener">{mapTitle}</a>
      {:else}
        <span>{mapTitle}</span>
      {/if}
    </div>

    {#if mapper}
      <div class="mapper">{mapper}</div>
    {/if}

    <div class="metrics">
      <ScoreBar value={score ?? null} size="sm" decimals={1} showLabel={true} emptyLabel="No score" />
    </div>

    <div class="player">
      <SongPlayer {hash} preferBeatLeader={true} />
    </div>
  </div>
</article>

<style>
  .playlist-map-card {
    display: grid;
    grid-template-columns: 104px 1fr;
    gap: 1rem;
    padding: 0.85rem 1rem;
    border-radius: 0.75rem;
    background: rgba(15, 23, 42, 0.55);
    border: 1px solid rgba(148, 163, 184, 0.12);
    box-shadow: 0 18px 36px -18px rgba(15, 23, 42, 0.8);
  }

  .cover {
    position: relative;
    width: 100%;
    padding-top: 100%;
    border-radius: 0.65rem;
    overflow: hidden;
    background: rgba(15, 23, 42, 0.65);
  }

  .cover img,
  .cover .placeholder {
    position: absolute;
    inset: 0;
    width: 100%;
    height: 100%;
    object-fit: cover;
    display: block;
  }

  .cover .placeholder {
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 1.5rem;
    color: rgba(226, 232, 240, 0.8);
  }

  .content {
    display: flex;
    flex-direction: column;
    gap: 0.65rem;
    min-width: 0;
  }

  .title {
    font-weight: 600;
    font-size: 1rem;
    color: #f8fafc;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }

  .title a {
    color: inherit;
    text-decoration: none;
  }

  .title a:hover {
    text-decoration: underline;
    color: color-mix(in srgb, var(--color-neon) 80%, white 20%);
  }

  .mapper {
    font-size: 0.8rem;
    color: rgba(148, 163, 184, 0.85);
  }

  .metrics {
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
  }

  .metrics :global(.score-meter) {
    width: 100%;
    max-width: none;
    margin: 0;
  }

  .player {
    margin-top: auto;
    padding-top: 0.5rem;
  }

  .player :global(.song-player) {
    width: 100%;
  }

  @media (max-width: 640px) {
    .playlist-map-card {
      grid-template-columns: minmax(0, 1fr);
      padding: 0.75rem;
      gap: 0.75rem;
    }

    .cover {
      padding-top: 60%;
      border-radius: 0.5rem;
    }

    .player {
      margin-top: 0.25rem;
    }
  }
</style>

