<script setup lang="ts">
import type { ExtensionSortKey, ExtensionSummary } from "../../types/browser";

defineProps<{
  extensions: ExtensionSummary[];
  sortKey: ExtensionSortKey;
  extensionMonogram: (name: string) => string;
}>();

const emit = defineEmits<{
  "update:sortKey": [value: ExtensionSortKey];
  showProfiles: [extensionId: string];
}>();
</script>

<template>
  <section class="table-section">
    <div v-if="extensions.length" class="data-table">
      <div class="data-table-header extensions-grid">
        <div class="header-cell icon-cell">Icon</div>
        <button class="header-cell sortable" :class="{ active: sortKey === 'name' }" type="button" @click="emit('update:sortKey', 'name')">Name</button>
        <button class="header-cell sortable" :class="{ active: sortKey === 'id' }" type="button" @click="emit('update:sortKey', 'id')">Extension ID</button>
        <div class="header-cell">Version</div>
        <div class="header-cell actions-cell">Profiles</div>
      </div>
      <div class="data-table-body styled-scrollbar">
        <article v-for="extension in extensions" :key="extension.id" class="data-table-row extensions-grid">
          <div class="extension-icon table-icon" :class="{ filled: Boolean(extension.iconDataUrl) }">
            <img v-if="extension.iconDataUrl" :src="extension.iconDataUrl" :alt="`${extension.name} icon`" />
            <span v-else>{{ extensionMonogram(extension.name) }}</span>
          </div>
          <div class="row-cell primary-cell">
            <strong>{{ extension.name }}</strong>
          </div>
          <div class="row-cell muted-cell" :title="extension.id">{{ extension.id }}</div>
          <div class="row-cell">
            <span v-if="extension.version" class="badge neutral">v{{ extension.version }}</span>
            <span v-else class="muted-cell">-</span>
          </div>
          <div class="row-cell actions-cell">
            <button class="disclosure-button" type="button" @click="emit('showProfiles', extension.id)">
              <span>View</span>
              <span class="badge neutral">{{ extension.profileIds.length }}</span>
            </button>
          </div>
        </article>
      </div>
    </div>
    <div v-else class="empty-card">
      <p>No extensions were discovered for this browser.</p>
    </div>
  </section>
</template>

<style scoped>
.table-section {
  padding: 0;
  height: 100%;
  min-height: 0;
}

.data-table {
  display: flex;
  flex-direction: column;
  height: 100%;
  min-height: 0;
  border: 1px solid rgba(148, 163, 184, 0.18);
  border-radius: 22px;
  background: var(--panel);
  box-shadow: var(--shadow);
  overflow: hidden;
}

.data-table-body {
  min-height: 0;
  overflow: auto;
}

.extensions-grid {
  display: grid;
  grid-template-columns: 60px minmax(180px, 1.1fr) minmax(220px, 1fr) 108px 154px;
  gap: 12px;
  align-items: center;
}

.data-table-header {
  position: sticky;
  top: 0;
  z-index: 2;
  padding: 10px 14px;
  border-bottom: 1px solid rgba(148, 163, 184, 0.14);
  background: rgba(248, 250, 252, 0.94);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
}

.header-cell {
  color: var(--muted);
  font-size: 0.81rem;
  font-weight: 700;
  letter-spacing: 0.02em;
}

.header-cell.sortable {
  padding: 0;
  text-align: left;
  background: transparent;
  cursor: pointer;
}

.header-cell.sortable.active {
  color: var(--text);
}

.data-table-row {
  padding: 12px 14px;
  border-bottom: 1px solid rgba(148, 163, 184, 0.12);
}

.data-table-row:last-child {
  border-bottom: 0;
}

.data-table-row:hover {
  background: rgba(248, 250, 252, 0.65);
}

.extension-icon {
  display: grid;
  place-items: center;
  flex-shrink: 0;
  background: linear-gradient(135deg, #e2e8f0, #f8fafc);
  color: #475569;
  font-weight: 700;
  overflow: hidden;
}

.extension-icon.filled {
  background: transparent;
}

.table-icon {
  width: 36px;
  height: 36px;
  border-radius: 12px;
}

.extension-icon img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.row-cell {
  min-width: 0;
}

.primary-cell strong {
  display: block;
  font-size: 0.93rem;
  line-height: 1.3;
}

.muted-cell {
  color: var(--muted);
  font-size: 0.87rem;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.disclosure-button {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  width: fit-content;
  min-width: 120px;
  padding: 7px 10px;
  border-radius: 12px;
  background: rgba(241, 245, 249, 0.9);
  color: var(--badge-text);
  cursor: pointer;
}

.actions-cell {
  display: flex;
  justify-content: flex-end;
}

.icon-cell {
  padding-left: 4px;
}

@media (max-width: 900px) {
  .extensions-grid {
    grid-template-columns: 56px minmax(160px, 1fr) minmax(160px, 1fr) 96px 148px;
  }
}

@media (max-width: 720px) {
  .extensions-grid {
    grid-template-columns: 56px minmax(0, 1fr) 132px;
  }

  .extensions-grid > :nth-child(3),
  .extensions-grid > :nth-child(4) {
    display: none;
  }
}
</style>
