<script setup lang="ts">
const { candidate, cancel, confirm } = useImpersonation()
const reason = ref('Customer reported issue — investigating')
const asUser = ref('first-user@example.com · Owner')

watch(candidate, (v) => {
  if (v) {
    reason.value = 'Customer reported issue — investigating'
  }
})

function onConfirm() {
  if (!reason.value.trim()) return
  confirm(reason.value.trim(), asUser.value)
}
</script>

<template>
  <Teleport to="body">
    <div v-if="candidate" class="backdrop" @click="cancel">
      <div class="modal" role="dialog" :aria-label="`Impersonate inside ${candidate.name}`" @click.stop>
        <header>
          <div>
            <Eyebrow>Operator action · logged</Eyebrow>
            <h2>Impersonate inside {{ candidate.name }}</h2>
          </div>
          <button class="x" type="button" aria-label="Close" @click="cancel">
            <UiIcon name="x" :size="12" />
          </button>
        </header>

        <div class="body">
          <div class="warn">
            <UiIcon name="shield" :size="15" />
            <p>
              You'll see <strong>{{ candidate.name }}</strong>'s workspace exactly as one of their users sees it.
              Any state-changing action will be flagged in their audit log as an operator action with your name
              attached. <Mono dim>// stub — no real session is created</Mono>
            </p>
          </div>

          <label class="field">
            <span>Impersonate as</span>
            <input v-model="asUser" type="text" />
          </label>

          <label class="field">
            <span>Reason (required)</span>
            <input v-model="reason" type="text" />
          </label>

          <Mono dim class="note">// logged as op.impersonate · session expires after 30 min idle</Mono>
        </div>

        <footer>
          <UiButton variant="ghost" @click="cancel">Cancel</UiButton>
          <UiButton variant="danger" :disabled="!reason.trim()" @click="onConfirm">
            <template #leading><UiIcon name="key" :size="13" /></template>
            Enter impersonation mode
          </UiButton>
        </footer>
      </div>
    </div>
  </Teleport>
</template>

<style scoped>
.backdrop {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.55);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
  z-index: 180;
}
.modal {
  width: 100%;
  max-width: 520px;
  background: var(--elevated);
  border: 1px solid var(--border);
  border-radius: 10px;
  box-shadow: 0 24px 80px rgba(0, 0, 0, 0.5);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}
header {
  padding: 16px 20px;
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  border-bottom: 1px solid var(--border);
}
h2 { margin: 4px 0 0 0; font-family: var(--font-display); font-weight: 600; font-size: 17px; }
.x {
  width: 26px; height: 26px;
  border: 0; border-radius: 6px; background: transparent;
  color: var(--text-mute); cursor: pointer;
  display: inline-flex; align-items: center; justify-content: center;
}
.x:hover { background: var(--surface); color: var(--text); }

.body {
  padding: 16px 20px;
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.warn {
  display: flex;
  align-items: flex-start;
  gap: 10px;
  padding: 12px 14px;
  background: rgba(226, 48, 48, 0.06);
  border: 1px solid rgba(226, 48, 48, 0.24);
  border-left: 3px solid var(--bad);
  border-radius: 6px;
  color: var(--bad);
}
.warn p { margin: 0; font-size: 12px; color: var(--text-dim); line-height: 1.55; }
.warn strong { color: var(--text); }

.field { display: flex; flex-direction: column; gap: 6px; }
.field span {
  font-family: var(--font-mono);
  font-size: 10px;
  letter-spacing: 0.12em;
  text-transform: uppercase;
  color: var(--text-mute);
  font-weight: 500;
}
.field input {
  height: 34px;
  padding: 0 12px;
  border: 1px solid var(--border);
  background: var(--input-bg);
  color: var(--text);
  border-radius: 6px;
  font-family: inherit;
  font-size: 13px;
  outline: 0;
}
.field input:focus { border-color: var(--border-hi); }

.note { padding-top: 2px; }

footer {
  padding: 12px 20px;
  display: flex;
  justify-content: flex-end;
  gap: 8px;
  border-top: 1px solid var(--border);
  background: var(--surface);
}
</style>
