<script setup lang="ts">
const props = defineProps<{
  data: any[]
  visibleColumns: string[]
  selectedRows: Set<number>
  allSelected: boolean
  copiedCell: string | null
  cellIdPrefix?: string
}>()

const emit = defineEmits<{
  toggleRow: [idx: number]
  toggleAll: []
  copyCell: [value: any, cellId: string]
}>()

function formatValue(value: any): string {
  if (value === null) return 'NULL'
  if (typeof value === 'object') return JSON.stringify(value)
  if (typeof value === 'string' && value.length > 100) {
    return value.substring(0, 100) + '...'
  }
  return String(value)
}

function getCellId(idx: number, key: string): string {
  return `${props.cellIdPrefix || 'cell'}-${idx}-${key}`
}
</script>

<template>
  <div class="data-table-container">
    <div v-if="data.length > 0 && visibleColumns.length > 0" class="data-table-scroll">
      <table class="data-table">
        <thead>
          <tr>
            <th class="checkbox-col">
              <input
                type="checkbox"
                :checked="allSelected"
                @change="emit('toggleAll')"
                title="Select all"
              />
            </th>
            <th v-for="key in visibleColumns" :key="key">{{ key }}</th>
          </tr>
        </thead>
        <tbody>
          <tr
            v-for="(row, idx) in data"
            :key="idx"
            :class="{ 'row-selected': selectedRows.has(idx) }"
          >
            <td class="checkbox-col">
              <input
                type="checkbox"
                :checked="selectedRows.has(idx)"
                @change="emit('toggleRow', idx)"
              />
            </td>
            <td
              v-for="key in visibleColumns"
              :key="key"
              class="copyable"
              :class="{ copied: copiedCell === getCellId(idx, key) }"
              @click="emit('copyCell', row[key], getCellId(idx, key))"
              title="Click to copy"
            >
              <span class="cell-content">{{ formatValue(row[key]) }}</span>
              <span v-if="copiedCell === getCellId(idx, key)" class="copied-badge">Copied!</span>
            </td>
          </tr>
        </tbody>
      </table>
    </div>
    <div v-else-if="visibleColumns.length === 0" class="no-data">All columns hidden</div>
    <div v-else class="no-data">
      <slot name="empty">No records found</slot>
    </div>
  </div>
</template>

<style scoped>
.data-table-container {
  flex: 1;
  overflow: hidden;
  display: flex;
  flex-direction: column;
}

.data-table-scroll {
  flex: 1;
  overflow: auto;
  padding: 1rem;
}

.data-table {
  min-width: max-content;
  border-collapse: collapse;
  font-size: 0.85rem;
}

.data-table th,
.data-table td {
  padding: 0.625rem 0.75rem;
  text-align: left;
  border-bottom: 1px solid var(--border-color);
  white-space: nowrap;
  max-width: 300px;
  overflow: hidden;
  text-overflow: ellipsis;
}

.data-table th {
  background: var(--bg-secondary);
  color: var(--text-secondary);
  font-weight: 600;
  font-size: 0.75rem;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  position: sticky;
  top: 0;
  z-index: 2;
}

.data-table td {
  color: var(--text-primary);
}

.data-table tr:hover td {
  background: var(--bg-hover);
}

.checkbox-col {
  width: 40px;
  min-width: 40px;
  text-align: center;
  padding: 0.5rem !important;
  position: sticky;
  left: 0;
  z-index: 1;
  background: var(--bg-secondary);
}

.data-table td.checkbox-col {
  background: var(--bg-primary);
}

.data-table tr:hover td.checkbox-col {
  background: var(--bg-hover);
}

.data-table tr.row-selected td.checkbox-col {
  background: var(--accent-muted);
}

.checkbox-col input[type="checkbox"] {
  width: 16px;
  height: 16px;
  cursor: pointer;
  accent-color: var(--accent);
}

.data-table tr.row-selected td {
  background: var(--accent-muted);
}

.data-table tr.row-selected:hover td {
  background: var(--accent-muted);
  filter: brightness(0.95);
}

.data-table td.copyable {
  cursor: pointer;
  position: relative;
  transition: background 0.15s;
}

.data-table td.copyable:hover {
  background: var(--accent-muted);
}

.data-table td.copyable:active {
  background: var(--accent);
  color: white;
}

.data-table td.copied {
  background: var(--success-bg, #d4edda);
}

.cell-content {
  display: block;
}

.copied-badge {
  position: absolute;
  top: 50%;
  right: 0.5rem;
  transform: translateY(-50%);
  padding: 0.125rem 0.375rem;
  background: var(--accent);
  color: white;
  font-size: 0.65rem;
  font-weight: 600;
  border-radius: 4px;
  animation: fadeIn 0.15s ease;
}

@keyframes fadeIn {
  from { opacity: 0; transform: translateY(-50%) scale(0.9); }
  to { opacity: 1; transform: translateY(-50%) scale(1); }
}

.no-data {
  text-align: center;
  padding: 2rem;
  color: var(--text-muted);
}
</style>
