<script>
  import { ChevronLeft, ChevronRight, Calendar, Edit2, History, Plus } from 'lucide-svelte';
  import VisitCard from './VisitCard.svelte';
  import DataTable from '$lib/components/DataTable.svelte';
  import { formatPatientName } from '$lib/utils/patients.js';

  /**
   * @typedef {Object} Visit
   * @property {string} InternalPVID
   * @property {string} [PVID]
   * @property {string} [PatientID]
   * @property {string} [NameFirst]
   * @property {string} [NameLast]
   * @property {string} [EpisodeID]
   * @property {string} [LastVisitADT]
   * @property {string} [LocCode]
   * @property {string} [LocFull]
   * @property {string} [PVCreateDate]
   * @property {string} [ADTCode]
   * @property {string} [EndDate]
   * @property {string} [ArchivedDate]
   */

  /** @type {{
   *   visits: Visit[],
   *   loading: boolean,
   *   viewMode: 'table' | 'cards',
   *   currentPage: number,
   *   totalPages: number,
   *   totalItems: number,
   *   perPage: number,
   *   onPageChange: (page: number) => void,
   *   onEditVisit: (visit: Visit) => void,
   *   onDeleteVisit: (visit: Visit) => void,
   *   onViewHistory: (visit: Visit) => void,
   *   onDischarge: (visit: Visit) => void,
   *   onAddADT?: (visit: Visit) => void
   * }} */
  let {
    visits = [],
    loading = false,
    viewMode = 'table',
    currentPage = 1,
    totalPages = 1,
    totalItems = 0,
    perPage = 20,
    onPageChange,
    onEditVisit,
    onDeleteVisit,
    onViewHistory,
    onDischarge,
    onAddADT
  } = $props();

  const columns = [
    { key: 'PVID', label: 'PVID', class: 'font-medium w-24' },
    { key: 'PatientID', label: 'PatID', class: 'w-24' },
    { key: 'PatientName', label: 'Patient Name', class: 'min-w-36' },
    { key: 'EpisodeID', label: 'Episode ID', class: 'w-28' },
    { key: 'LastVisitADT', label: 'Last Visit ADT', class: 'w-32' },
    { key: 'LocCode', label: 'Location', class: 'w-24' },
    { key: 'Status', label: 'Status', class: 'w-20 text-center' },
    { key: 'actions', label: 'Actions', class: 'w-36 text-center' },
  ];

  function getStatusBadge(visit) {
    if (!visit.EndDate && !visit.ArchivedDate) {
      return '<span class="badge badge-sm badge-success">Active</span>';
    }
    return '<span class="badge badge-sm badge-ghost">Closed</span>';
  }

  function getPatientName(visit) {
    const first = visit.NameFirst || '';
    const last = visit.NameLast || '';
    return [first, last].filter(Boolean).join(' ') || visit.PatientName || '-';
  }
</script>

<div class="bg-base-100 rounded-lg shadow border border-base-200 flex flex-col h-full overflow-hidden">
  {#if !loading && visits.length === 0}
    <!-- Empty State -->
    <div class="flex-1 flex items-center justify-center bg-base-100">
      <div class="text-center text-gray-500 p-6">
        <Calendar class="w-12 h-12 mx-auto mb-2 opacity-50" />
        <p class="text-sm">No visits found</p>
        <p class="text-xs text-gray-400">Use search criteria to find visits</p>
      </div>
    </div>
  {:else}
    {#if viewMode === 'table'}
      <!-- Table View -->
      <div class="flex-1 overflow-auto">
        <DataTable
          {columns}
          data={visits}
          {loading}
          emptyMessage="No visits found"
          hover={true}
        >
          {#snippet cell({ column, row })}
            {#if column.key === 'Status'}
              {@html getStatusBadge(row)}
            {:else if column.key === 'PatientName'}
              <span class="truncate block">{getPatientName(row)}</span>
            {:else if column.key === 'actions'}
              <div class="flex justify-center gap-1">
                <button
                  class="btn btn-xs btn-ghost"
                  title="Edit"
                  onclick={() => onEditVisit(row)}
                >
                  <Edit2 class="w-3 h-3" />
                </button>
                <button
                  class="btn btn-xs btn-ghost"
                  title="History"
                  onclick={() => onViewHistory(row)}
                >
                  <History class="w-3 h-3" />
                </button>
                {#if onAddADT}
                  <button
                    class="btn btn-xs btn-ghost btn-primary"
                    title="Add ADT"
                    onclick={() => onAddADT(row)}
                  >
                    <Plus class="w-3 h-3" />
                  </button>
                {/if}
              </div>
            {:else}
              <span class="truncate block">{row[column.key] || '-'}</span>
            {/if}
          {/snippet}
        </DataTable>
      </div>
    {:else}
      <!-- Card View -->
      <div class="flex-1 overflow-auto p-3">
        {#if loading}
          <div class="flex items-center justify-center py-12">
            <span class="loading loading-spinner loading-lg text-primary"></span>
          </div>
        {:else}
          <div class="space-y-2">
            {#each visits as visit (visit.InternalPVID)}
              <VisitCard 
                {visit} 
                onEdit={() => onEditVisit(visit)}
                onDelete={() => onDeleteVisit(visit)}
                onViewHistory={() => onViewHistory(visit)}
                onDischarge={() => onDischarge(visit)}
              />
            {/each}
          </div>
        {/if}
      </div>
    {/if}

    <!-- Pagination -->
    {#if totalPages > 1}
      <div class="flex items-center justify-between px-3 py-2 border-t border-base-200 bg-base-100">
        <div class="text-xs text-gray-600">
          {(currentPage - 1) * perPage + 1} - {Math.min(currentPage * perPage, totalItems)} of {totalItems}
        </div>
        <div class="flex gap-1">
          <button 
            class="btn btn-xs btn-ghost" 
            onclick={() => onPageChange(currentPage - 1)} 
            disabled={currentPage === 1}
          >
            <ChevronLeft class="w-3 h-3" />
          </button>
          <span class="btn btn-xs btn-ghost no-animation text-xs">
            {currentPage} / {totalPages}
          </span>
          <button 
            class="btn btn-xs btn-ghost" 
            onclick={() => onPageChange(currentPage + 1)} 
            disabled={currentPage === totalPages}
          >
            <ChevronRight class="w-3 h-3" />
          </button>
        </div>
      </div>
    {/if}
  {/if}
</div>
