<script setup lang="ts">
import { computed } from 'vue';
import { useI18n } from 'vue-i18n';
import { buildCustomerServiceUrl } from '../config/customerService';
import { useAuthStore } from '../stores/auth';
import { usePlayerProfile } from '../composables/usePlayerProfile';

const props = defineProps<{ modelValue: boolean }>();
const emit = defineEmits<{ 'update:modelValue': [boolean] }>();

const { t } = useI18n();
const auth = useAuthStore();
const { profileRaw, avatarUrl } = usePlayerProfile();

const visible = computed({
  get: () => props.modelValue,
  set: (v) => emit('update:modelValue', v),
});

const iframeSrc = computed(() => {
  const visitor = auth.user
    ? {
        name:
          profileRaw.value?.username ||
          profileRaw.value?.preferences?.phone ||
          auth.user.username ||
          '',
        avatar: avatarUrl.value
          ? new URL(avatarUrl.value, window.location.origin).href
          : '',
        id: String(profileRaw.value?.id ?? auth.user.id ?? ''),
      }
    : null;

  return buildCustomerServiceUrl(t('support.connecting'), visitor);
});

function close() {
  visible.value = false;
}
</script>

<template>
  <Teleport to="body">
    <Transition name="fade">
      <div v-if="visible" class="cs-overlay" @click.self="close">
        <div class="cs-modal" role="dialog" :aria-label="t('support.title')">
          <header class="cs-header">
            <h2 class="cs-title">{{ t('support.title') }}</h2>
            <button type="button" class="close-btn" :aria-label="t('support.close')" @click="close">
              ✕
            </button>
          </header>

          <div class="cs-body">
            <iframe
              v-if="visible"
              :key="iframeSrc"
              class="cs-frame"
              :src="iframeSrc"
              :title="t('support.title')"
              allow="microphone; camera; clipboard-read; clipboard-write"
            />
          </div>
        </div>
      </div>
    </Transition>
  </Teleport>
</template>

<style scoped>
.cs-overlay {
  position: fixed;
  inset: 0;
  z-index: 1000;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
  background: rgba(0, 0, 0, 0.5);
  backdrop-filter: blur(4px);
  -webkit-backdrop-filter: blur(4px);
}

.cs-modal {
  display: flex;
  flex-direction: column;
  width: min(100%, 420px);
  height: min(82vh, 680px);
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: 12px;
  overflow: hidden;
  box-shadow: 0 8px 30px rgba(0, 0, 0, 0.3);
}

.cs-header {
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 14px 16px;
  border-bottom: 1px solid var(--border);
  background: var(--bg-card);
}

.cs-title {
  margin: 0;
  font-size: 16px;
  font-weight: 800;
  color: var(--primary);
}

.close-btn {
  background: none;
  border: none;
  color: var(--text-muted);
  font-size: 18px;
  cursor: pointer;
  padding: 4px;
  line-height: 1;
}

.close-btn:hover {
  color: var(--text);
}

.cs-body {
  flex: 1;
  min-height: 0;
  background: var(--bg-body);
}

.cs-frame {
  display: block;
  width: 100%;
  height: 100%;
  border: 0;
  background: var(--bg-body);
}

.cs-empty {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 100%;
  margin: 0;
  padding: 24px;
  text-align: center;
  font-size: 14px;
  line-height: 1.6;
  color: var(--text-muted);
}

.fade-enter-active,
.fade-leave-active {
  transition: opacity 0.25s ease;
}

.fade-enter-from,
.fade-leave-to {
  opacity: 0;
}
</style>
