<template>
  <main class="settings">
    <h1 class="settings__title">Settings</h1>

    <section class="settings__section">
      <h2 class="settings__section-title">Theme</h2>
      <div class="theme-grid" role="radiogroup" aria-label="Choose theme">
        <button
          v-for="t in THEMES"
          :key="t.id"
          type="button"
          role="radio"
          :aria-checked="currentTheme === t.id"
          :aria-label="t.label"
          :class="['theme-swatch', { 'theme-swatch--active': currentTheme === t.id }]"
          :style="{ '--swatch-bg': t.bg, '--swatch-primary': t.primary, '--swatch-text': t.text }"
          @click="select(t.id)"
        >
          <span class="theme-swatch__preview" aria-hidden="true">
            <span class="theme-swatch__bar" />
            <span class="theme-swatch__dot" />
          </span>
          <span class="theme-swatch__label">{{ t.label }}</span>
          <span v-if="currentTheme === t.id" class="theme-swatch__check" aria-hidden="true">✓</span>
        </button>
      </div>
    </section>

    <section class="settings__section">
      <h2 class="settings__section-title">Account</h2>
      <div class="settings__row">
        <span class="settings__row-label">Signed in as</span>
        <span class="settings__row-value">{{ auth.user?.email }}</span>
      </div>
      <a href="/logout" class="settings__logout">Sign out</a>
    </section>
  </main>
</template>

<script setup lang="ts">
import { computed } from 'vue'
import { useAuthStore } from '@/stores/auth'
import { useTheme, THEMES } from '@/composables/useTheme'

const auth = useAuthStore()
const { saveTheme } = useTheme()

const currentTheme = computed(() => auth.user?.theme ?? 'warm-craft')

function select(themeId: string) {
  saveTheme(themeId)
}
</script>

<style scoped lang="scss">
.settings {
  padding: var(--space-4) var(--space-4) calc(64px + var(--space-6));
  max-width: 480px;
  margin: 0 auto;

  &__title {
    font-size: var(--text-xl);
    font-weight: 700;
    margin-bottom: var(--space-6);
  }

  &__section {
    margin-bottom: var(--space-6);
  }

  &__section-title {
    font-size: var(--text-sm);
    font-weight: 700;
    color: var(--color-text-muted);
    text-transform: uppercase;
    letter-spacing: 0.06em;
    margin-bottom: var(--space-3);
  }

  &__row {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: var(--space-3) 0;
    border-bottom: 1px solid var(--color-border);
    font-size: var(--text-base);
  }

  &__row-label { color: var(--color-text-muted); }
  &__row-value { font-weight: 600; }

  &__logout {
    display: flex;
    align-items: center;
    min-height: var(--touch-min);
    padding: var(--space-3) 0;
    color: var(--color-destructive);
    font-weight: 600;
    text-decoration: none;
    font-size: var(--text-base);
  }
}

.theme-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: var(--space-3);
}

.theme-swatch {
  position: relative;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: var(--space-2);
  padding: var(--space-3);
  background: var(--swatch-bg);
  border: 2px solid transparent;
  border-radius: var(--radius-md);
  cursor: pointer;
  transition: border-color var(--duration-fast);
  min-height: var(--touch-min);

  &--active {
    border-color: var(--swatch-primary);
  }

  &__preview {
    width: 100%;
    height: 36px;
    border-radius: var(--radius-sm);
    background: var(--swatch-bg);
    display: flex;
    flex-direction: column;
    justify-content: center;
    gap: 4px;
    padding: 0 6px;
    border: 1px solid color-mix(in srgb, var(--swatch-text) 15%, transparent);
  }

  &__bar {
    display: block;
    height: 6px;
    border-radius: 3px;
    background: var(--swatch-primary);
    width: 60%;
  }

  &__dot {
    display: block;
    height: 4px;
    border-radius: 2px;
    background: var(--swatch-text);
    width: 80%;
    opacity: 0.4;
  }

  &__label {
    font-size: var(--text-xs);
    font-weight: 600;
    color: var(--color-text);
    text-align: center;
    line-height: 1.2;
  }

  &__check {
    position: absolute;
    top: var(--space-1);
    right: var(--space-2);
    font-size: var(--text-sm);
    color: var(--swatch-primary);
    font-weight: 700;
  }
}
</style>
