<script setup lang="ts">
// Floating tweaks panel for the portal. Exposes role switching (the most
// important tweak — flips end-user/customer-admin/partner-admin views),
// plus theme/density/accent. Bottom-right corner, lives in default layout.

const { state, setTheme, setDensity, setAccent, setRole } = usePortalTweaks()
const partnerMode = usePartnerMode()
const router = useRouter()
const open = ref(false)

function changeRole(role: 'end-user' | 'customer-admin' | 'partner-admin') {
  setRole(role)
  // If switching out of partner-admin while in customer mode, exit it.
  if (role !== 'partner-admin') partnerMode.exit()
  // Route to a sensible default for the new role.
  if (role === 'partner-admin') router.push('/partner')
  else if (role === 'customer-admin') router.push('/admin')
  else router.push('/')
}

const ACCENTS = [
  { key: 'signal', label: 'Signal', hex: '#D4FF3A' },
  { key: 'cobalt', label: 'Cobalt', hex: '#3F6BFF' },
  { key: 'coral',  label: 'Coral',  hex: '#FF6B4A' },
  { key: 'moss',   label: 'Moss',   hex: '#5B8C5A' },
] as const
</script>

<template>
  <div class="tweaks-root">
    <button class="trigger" :class="{ on: open }" type="button" :title="open ? 'Close tweaks' : 'Open tweaks'" @click="open = !open">
      <UiIcon :name="open ? 'x' : 'shield'" :size="14" />
    </button>

    <Transition name="tweaks">
      <div v-if="open" class="panel" role="dialog" aria-label="Tweaks">
        <header>
          <Eyebrow>Tweaks · prototype</Eyebrow>
          <button class="x" type="button" aria-label="Close" @click="open = false">
            <UiIcon name="x" :size="11" />
          </button>
        </header>

        <section>
          <label class="row-label">Role</label>
          <div class="seg col">
            <button :class="{ on: state.role === 'end-user' }" @click="changeRole('end-user')">End user</button>
            <button :class="{ on: state.role === 'customer-admin' }" @click="changeRole('customer-admin')">Customer admin</button>
            <button :class="{ on: state.role === 'partner-admin' }" @click="changeRole('partner-admin')">Partner admin</button>
          </div>
        </section>

        <section>
          <label class="row-label">Theme</label>
          <div class="seg">
            <button :class="{ on: state.theme === 'light' }" @click="setTheme('light')">Light</button>
            <button :class="{ on: state.theme === 'dark' }" @click="setTheme('dark')">Dark</button>
          </div>
        </section>

        <section>
          <label class="row-label">Density</label>
          <div class="seg">
            <button :class="{ on: state.density === 'comfy' }" @click="setDensity('comfy')">Comfy</button>
            <button :class="{ on: state.density === 'compact' }" @click="setDensity('compact')">Compact</button>
          </div>
        </section>

        <section>
          <label class="row-label">Accent · whitelabel</label>
          <div class="swatches">
            <button
              v-for="a in ACCENTS"
              :key="a.key"
              :class="{ on: state.accent === a.key }"
              :title="a.label"
              :style="{ background: a.hex }"
              @click="setAccent(a.key)"
            />
          </div>
        </section>

        <footer>
          <Mono dim>// saved to localStorage</Mono>
        </footer>
      </div>
    </Transition>
  </div>
</template>

<style scoped>
.tweaks-root {
  position: fixed;
  right: 16px;
  bottom: 16px;
  z-index: 50;
}

.trigger {
  width: 34px;
  height: 34px;
  border-radius: 999px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text-dim);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.18);
}
.trigger:hover { color: var(--text); border-color: var(--border-hi); }
.trigger.on { background: var(--text); color: var(--bg); border-color: var(--text); }

.panel {
  position: absolute;
  right: 0;
  bottom: 44px;
  width: 280px;
  background: var(--elevated);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 12px;
  display: flex;
  flex-direction: column;
  gap: 12px;
  box-shadow: 0 16px 48px rgba(0, 0, 0, 0.32);
}

header { display: flex; justify-content: space-between; align-items: center; }
.x {
  width: 22px;
  height: 22px;
  border: 0;
  background: transparent;
  color: var(--text-mute);
  border-radius: 6px;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}
.x:hover { background: var(--surface); color: var(--text); }

section { display: flex; flex-direction: column; gap: 6px; }
.row-label {
  font-family: var(--font-mono);
  font-size: 10px;
  letter-spacing: 0.12em;
  text-transform: uppercase;
  color: var(--text-mute);
  font-weight: 500;
}

.seg {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 4px;
  padding: 3px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 7px;
}
.seg.col { grid-template-columns: 1fr; }
.seg button {
  appearance: none;
  border: 0;
  background: transparent;
  color: var(--text-dim);
  font-family: inherit;
  font-size: 12px;
  font-weight: 500;
  padding: 6px 8px;
  border-radius: 5px;
  cursor: pointer;
}
.seg button:hover { color: var(--text); }
.seg button.on { background: var(--text); color: var(--bg); }

.swatches { display: flex; gap: 6px; }
.swatches button {
  width: 28px;
  height: 28px;
  border-radius: 6px;
  border: 2px solid var(--border);
  cursor: pointer;
}
.swatches button.on { border-color: var(--text); transform: scale(1.05); }

footer { padding-top: 4px; border-top: 1px dashed var(--border); }

.tweaks-enter-active, .tweaks-leave-active { transition: opacity 0.12s, transform 0.12s; }
.tweaks-enter-from, .tweaks-leave-to { opacity: 0; transform: translateY(4px); }
</style>
