<script>
  import { createEventDispatcher } from 'svelte';

  export let tickets = [];
  export let selectedTicket = null;
  export let busy = false;

  const dispatch = createEventDispatcher();

  function badgeTone(priority) {
    if (priority === 'high') return 'bad';
    if (priority === 'medium') return 'warn';
    return 'good';
  }
</script>

<section class="panel-grid">
  <aside class="list">
    <header>
      <h2>Tickets</h2>
      <span>{tickets.length} active</span>
    </header>

    {#if tickets.length === 0}
      <p class="empty">No tickets assigned.</p>
    {:else}
      {#each tickets as ticket}
        <button class:selected={ticket.id === selectedTicket?.id} on:click={() => dispatch('select', ticket.id)}>
          <div class="topline">
            <strong>{ticket.id}</strong>
            <span class={`badge ${badgeTone(ticket.priority)}`}>{ticket.priority}</span>
          </div>
          <div>{ticket.subject}</div>
          <small>{ticket.status}</small>
        </button>
      {/each}
    {/if}
  </aside>

  <article class="detail">
    {#if selectedTicket}
      <div class="detail-header">
        <div>
          <p class="eyebrow">{selectedTicket.id}</p>
          <h3>{selectedTicket.subject}</h3>
        </div>

        <button
          class="primary"
          disabled={busy || selectedTicket.status === 'resolved'}
          on:click={() => dispatch('complete', selectedTicket.id)}
        >
          {busy ? 'Working…' : selectedTicket.status === 'resolved' ? 'Resolved' : 'Mark Complete'}
        </button>
      </div>

      <dl>
        <div>
          <dt>Status</dt>
          <dd>{selectedTicket.status}</dd>
        </div>
        <div>
          <dt>Priority</dt>
          <dd>{selectedTicket.current_priority ?? selectedTicket.priority}</dd>
        </div>
        <div>
          <dt>Linked Quest</dt>
          <dd>{selectedTicket.linked_quest}</dd>
        </div>
      </dl>

      {#if selectedTicket.body}
        <div class="body">{selectedTicket.body}</div>
      {:else}
        <p class="muted">Quest-linked ticket. Use the workstation and resolve the underlying system state.</p>
      {/if}
    {:else}
      <p class="empty">Select a ticket to inspect its details.</p>
    {/if}
  </article>
</section>

<style>
  .panel-grid {
    display: grid;
    grid-template-columns: minmax(260px, 320px) minmax(0, 1fr);
    gap: 1rem;
    min-height: 0;
  }

  .list,
  .detail {
    background: var(--panel);
    border: 1px solid var(--border);
    border-radius: 24px;
    padding: 1rem;
    box-shadow: var(--shadow);
  }

  .list {
    display: grid;
    align-content: start;
    gap: 0.75rem;
  }

  header,
  .detail-header {
    display: flex;
    justify-content: space-between;
    gap: 1rem;
    align-items: center;
  }

  h2,
  h3 {
    margin: 0;
  }

  .eyebrow,
  dt,
  small {
    font-size: 0.75rem;
    letter-spacing: 0.08em;
    text-transform: uppercase;
    color: var(--text-muted);
  }

  .list button {
    text-align: left;
    border: 1px solid var(--border);
    border-radius: 16px;
    padding: 0.9rem;
    background: rgba(255, 255, 255, 0.03);
    color: inherit;
  }

  .list button.selected {
    border-color: rgba(214, 104, 56, 0.45);
    background: rgba(214, 104, 56, 0.1);
  }

  .topline {
    display: flex;
    justify-content: space-between;
    gap: 0.5rem;
    align-items: center;
    margin-bottom: 0.35rem;
  }

  .badge {
    padding: 0.2rem 0.55rem;
    border-radius: 999px;
    font-size: 0.75rem;
    text-transform: uppercase;
  }

  .badge.good { background: rgba(99, 182, 122, 0.15); }
  .badge.warn { background: rgba(213, 166, 75, 0.18); }
  .badge.bad { background: rgba(200, 101, 101, 0.18); }

  .primary {
    border: 0;
    border-radius: 14px;
    padding: 0.8rem 1rem;
    background: var(--accent);
    color: #fff7f1;
  }

  .primary:disabled {
    opacity: 0.55;
    cursor: default;
  }

  dl {
    display: grid;
    grid-template-columns: repeat(3, minmax(0, 1fr));
    gap: 0.75rem;
  }

  dd {
    margin: 0.3rem 0 0;
  }

  .body,
  .empty,
  .muted {
    color: var(--text-muted);
    white-space: pre-wrap;
    line-height: 1.5;
  }

  @media (max-width: 960px) {
    .panel-grid {
      grid-template-columns: 1fr;
    }

    dl {
      grid-template-columns: 1fr;
    }
  }
</style>
