<script setup lang="ts">
import type { TableInfo, DbStats } from '@/types/database'

defineProps<{
  tables: TableInfo[]
  selectedTable: string | null
  stats: DbStats | null
  loading: boolean
  error: string | null
}>()

const emit = defineEmits<{
  selectTable: [tableName: string]
  refresh: []
}>()
</script>

<template>
  <aside class="sidebar">
    <div class="sidebar-header">
      <h2>Database</h2>
      <button class="btn-icon" @click="emit('refresh')" title="Refresh">
        <svg xmlns="http://www.w3.org/2000/svg" width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
          <path d="M21 12a9 9 0 1 1-9-9c2.52 0 4.93 1 6.74 2.74L21 8"/>
          <path d="M21 3v5h-5"/>
        </svg>
      </button>
    </div>

    <div class="table-list">
      <div v-if="loading && tables.length === 0" class="loading">Loading tables...</div>
      <div v-else-if="error" class="error">{{ error }}</div>
      <div
        v-else
        v-for="table in tables"
        :key="table.name"
        class="table-item"
        :class="{ active: selectedTable === table.name }"
        @click="emit('selectTable', table.name)"
      >
        <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
          <rect x="3" y="3" width="18" height="18" rx="2"/>
          <path d="M3 9h18"/>
          <path d="M3 15h18"/>
          <path d="M9 3v18"/>
        </svg>
        <span class="table-name">{{ table.name }}</span>
        <span class="table-count">{{ table.count }}</span>
      </div>
    </div>

    <div v-if="stats" class="sidebar-stats">
      <div class="stat-item">
        <span class="stat-label">Size</span>
        <span class="stat-value">{{ stats.size }}</span>
      </div>
      <div class="stat-item">
        <span class="stat-label">Tables</span>
        <span class="stat-value">{{ stats.tables }}</span>
      </div>
      <div class="stat-item">
        <span class="stat-label">Records</span>
        <span class="stat-value">{{ stats.totalRecords }}</span>
      </div>
    </div>
  </aside>
</template>

<style scoped>
.sidebar {
  width: 280px;
  background: var(--bg-secondary);
  border-right: 1px solid var(--border-color);
  display: flex;
  flex-direction: column;
}

.sidebar-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 1rem 1.25rem;
  border-bottom: 1px solid var(--border-color);
}

.sidebar-header h2 {
  margin: 0;
  font-size: 1rem;
  color: var(--text-primary);
}

.btn-icon {
  padding: 0.375rem;
  background: transparent;
  border: none;
  border-radius: 6px;
  color: var(--text-secondary);
  cursor: pointer;
  transition: all 0.15s;
}

.btn-icon:hover {
  background: var(--bg-hover);
  color: var(--accent);
}

.table-list {
  flex: 1;
  overflow-y: auto;
  padding: 0.75rem;
}

.table-item {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  padding: 0.625rem 0.75rem;
  border-radius: 6px;
  cursor: pointer;
  transition: all 0.15s;
  color: var(--text-secondary);
}

.table-item:hover {
  background: var(--bg-hover);
  color: var(--text-primary);
}

.table-item.active {
  background: var(--accent-muted);
  color: var(--accent);
}

.table-name {
  flex: 1;
  font-size: 0.875rem;
  font-weight: 500;
}

.table-count {
  font-size: 0.75rem;
  padding: 0.125rem 0.5rem;
  background: var(--bg-tertiary);
  border-radius: 9999px;
  color: var(--text-muted);
}

.table-item.active .table-count {
  background: var(--accent);
  color: white;
}

.sidebar-stats {
  padding: 1rem;
  border-top: 1px solid var(--border-color);
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}

.stat-item {
  display: flex;
  justify-content: space-between;
  font-size: 0.8rem;
}

.stat-label {
  color: var(--text-muted);
}

.stat-value {
  color: var(--text-primary);
  font-weight: 500;
}

.loading, .error {
  padding: 1rem;
  text-align: center;
  font-size: 0.875rem;
}

.loading {
  color: var(--text-muted);
}

.error {
  color: var(--error);
}

@media (max-width: 768px) {
  .sidebar {
    width: 100%;
    max-height: 200px;
    border-right: none;
    border-bottom: 1px solid var(--border-color);
  }

  .table-list {
    flex-direction: row;
    flex-wrap: wrap;
    overflow-x: auto;
    padding: 0.5rem;
  }

  .table-item {
    flex: 0 0 auto;
  }

  .sidebar-stats {
    display: none;
  }
}
</style>
