<script setup lang="ts">
import type { Theme } from '../../stores/theme'

const props = defineProps<{
  theme: Theme
  active: boolean
}>()

const emit = defineEmits<{
  select: [theme: Theme]
  delete: [id: string]
  clone: [id: string]
  setDefault: [id: string]
  edit: [theme: Theme]
}>()

function getAccentColor(): string {
  return props.theme.variables?.accent?.accent || '#6366f1'
}
</script>

<template>
  <div
    class="theme-item"
    :class="{ active, 'is-default': theme.is_default }"
    @click="emit('select', theme)"
  >
    <div class="theme-color" :style="{ background: getAccentColor() }"></div>

    <div class="theme-info">
      <span class="theme-name">
        {{ theme.name }}
        <span v-if="theme.is_default" class="default-badge">Default</span>
      </span>
      <span class="theme-meta">
        {{ theme.is_system ? 'System' : 'Custom' }}
      </span>
    </div>

    <div class="theme-actions" @click.stop>
      <button
        v-if="!theme.is_default"
        class="action-btn"
        @click="emit('setDefault', theme.id)"
        title="Set as default"
      >
        <svg xmlns="http://www.w3.org/2000/svg" width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
          <path d="M12 2l3.09 6.26L22 9.27l-5 4.87 1.18 6.88L12 17.77l-6.18 3.25L7 14.14 2 9.27l6.91-1.01L12 2z"/>
        </svg>
      </button>

      <button
        v-if="!theme.is_system"
        class="action-btn"
        @click="emit('edit', theme)"
        title="Edit theme"
      >
        <svg xmlns="http://www.w3.org/2000/svg" width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
          <path d="M11 4H4a2 2 0 0 0-2 2v14a2 2 0 0 0 2 2h14a2 2 0 0 0 2-2v-7"/>
          <path d="M18.5 2.5a2.121 2.121 0 0 1 3 3L12 15l-4 1 1-4 9.5-9.5z"/>
        </svg>
      </button>

      <button
        class="action-btn"
        @click="emit('clone', theme.id)"
        title="Clone theme"
      >
        <svg xmlns="http://www.w3.org/2000/svg" width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
          <rect x="9" y="9" width="13" height="13" rx="2" ry="2"/>
          <path d="M5 15H4a2 2 0 0 1-2-2V4a2 2 0 0 1 2-2h9a2 2 0 0 1 2 2v1"/>
        </svg>
      </button>

      <button
        v-if="!theme.is_system"
        class="action-btn delete"
        @click="emit('delete', theme.id)"
        title="Delete theme"
      >
        <svg xmlns="http://www.w3.org/2000/svg" width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
          <path d="M3 6h18"/><path d="M19 6v14c0 1-1 2-2 2H7c-1 0-2-1-2-2V6"/>
          <path d="M8 6V4c0-1 1-2 2-2h4c1 0 2 1 2 2v2"/>
        </svg>
      </button>
    </div>
  </div>
</template>

<style scoped>
.theme-item {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  padding: 0.625rem 0.75rem;
  background: var(--bg-primary);
  border: 1px solid var(--border-color);
  border-radius: 8px;
  cursor: pointer;
  transition: all 0.15s;
}

.theme-item:hover {
  border-color: var(--border-hover);
  background: var(--bg-hover);
}

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

.theme-color {
  width: 32px;
  height: 32px;
  border-radius: 6px;
  flex-shrink: 0;
}

.theme-info {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
  gap: 0.125rem;
}

.theme-name {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  font-size: 0.875rem;
  font-weight: 500;
  color: var(--text-primary);
}

.default-badge {
  font-size: 0.65rem;
  padding: 0.125rem 0.375rem;
  background: var(--accent);
  color: white;
  border-radius: 9999px;
  font-weight: 600;
}

.theme-meta {
  font-size: 0.7rem;
  color: var(--text-muted);
}

.theme-actions {
  display: flex;
  gap: 0.25rem;
  opacity: 0;
  transition: opacity 0.15s;
}

.theme-item:hover .theme-actions {
  opacity: 1;
}

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

.action-btn:hover {
  background: var(--bg-secondary);
  color: var(--text-primary);
}

.action-btn.delete:hover {
  background: var(--error-bg);
  color: var(--error);
}
</style>
