<script lang="ts">
  import { Trash2 } from 'lucide-svelte';

  import type { ThroughputEntry } from '$lib/types';

  let {
    pendingDelete,
    deletingId,
    formatDate,
    onCancel,
    onConfirm,
    dialogRef = $bindable<HTMLElement | null>(null)
  }: {
    pendingDelete: ThroughputEntry;
    deletingId: number | null;
    formatDate: (value: string) => string;
    onCancel: () => void;
    onConfirm: () => void;
    dialogRef?: HTMLElement | null;
  } = $props();
</script>

<div class="modal-backdrop" role="presentation" onclick={onCancel}>
  <div
    class="modal-card"
    role="dialog"
    aria-modal="true"
    aria-labelledby="delete-title"
    tabindex="-1"
    bind:this={dialogRef}
    onclick={(event) => event.stopPropagation()}
    onkeydown={(event) => { if (event.key === 'Escape') onCancel(); }}
  >
    <div class="modal-icon"><Trash2 size={22} strokeWidth={2.2} /></div>
    <h2 id="delete-title" class="modal-title">Delete this run?</h2>
    <p class="modal-text">
      The <strong>{pendingDelete.product_name_snapshot}</strong> run from
      {formatDate(pendingDelete.production_date)} will be permanently removed.
      This cannot be undone.
    </p>
    <div class="modal-actions">
      <button type="button" class="modal-cancel" onclick={onCancel}>Cancel</button>
      <button
        type="button"
        class="modal-confirm"
        disabled={deletingId === pendingDelete.id}
        onclick={onConfirm}
      >
        {deletingId === pendingDelete.id ? 'Deleting…' : 'Delete run'}
      </button>
    </div>
  </div>
</div>

<style>
  .modal-backdrop {
    position: fixed;
    inset: 0;
    z-index: 80;
    display: grid;
    place-items: center;
    padding: 1.5rem;
    background: color-mix(in srgb, var(--color-text-primary) 32%, transparent);
    backdrop-filter: blur(6px);
  }

  .modal-card {
    width: min(28rem, 100%);
    display: grid;
    gap: 0.7rem;
    padding: 1.6rem;
    border: 1px solid var(--color-border);
    border-radius: 1rem;
    background: var(--color-bg-surface);
    box-shadow: 0 24px 60px -20px rgba(0, 0, 0, 0.45);
    animation: modal-pop 160ms cubic-bezier(0.22, 1, 0.36, 1);
  }

  .modal-card:focus {
    outline: none;
  }

  .modal-icon {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 2.8rem;
    height: 2.8rem;
    border-radius: 0.8rem;
    background: #fdecee;
    color: #b3261e;
  }

  .modal-title,
  .modal-text {
    margin: 0;
  }

  .modal-title {
    font-size: 1.25rem;
    font-weight: 700;
    letter-spacing: -0.02em;
    color: var(--color-text-primary);
  }

  .modal-text {
    font-size: 0.98rem;
    line-height: 1.5;
    color: var(--color-text-secondary);
  }

  .modal-actions {
    display: flex;
    justify-content: flex-end;
    gap: 0.6rem;
    margin-top: 0.55rem;
  }

  .modal-cancel,
  .modal-confirm {
    min-height: 44px;
    padding: 0.6rem 1.15rem;
    border-radius: 0.7rem;
    font-size: 0.98rem;
    font-weight: 650;
    cursor: pointer;
    transition: background-color 150ms ease, border-color 150ms ease;
  }

  .modal-cancel {
    background: var(--color-bg-surface);
    border: 1px solid var(--color-border);
    color: var(--color-text-secondary);
  }

  .modal-cancel:hover {
    color: var(--color-text-primary);
    border-color: var(--color-text-muted);
  }

  .modal-confirm {
    background: #b3261e;
    border: 1px solid #b3261e;
    color: #fff;
  }

  .modal-confirm:hover:not(:disabled) {
    background: #95201a;
  }

  .modal-confirm:disabled {
    opacity: 0.65;
    cursor: progress;
  }

  .modal-cancel:focus-visible,
  .modal-confirm:focus-visible {
    outline: 3px solid var(--color-brand);
    outline-offset: 2px;
  }

  @keyframes modal-pop {
    from { opacity: 0; transform: translateY(6px) scale(0.985); }
    to { opacity: 1; transform: none; }
  }

  @media (prefers-reduced-motion: reduce) {
    .modal-card {
      animation: none;
    }
  }
</style>
