<script setup>
/*
 * MelissaConfigPage — Chrome compartilhado pras paginas nativas de
 * configuracao Melissa (cfg-wa, cfg-lembretes, etc).
 *
 * Padroniza header, drawer mobile, sidebar (Configuracoes + FAQ) e main.
 * Cada pagina concreta passa props (titulo/icone/subtitulo/faq) e o
 * conteudo via slot default.
 */
import { ref, onMounted, onBeforeUnmount } from 'vue';
import MelissaConfigList from './MelissaConfigList.vue';

defineProps({
    title:    { type: String, required: true },
    icon:     { type: String, required: true },
    subtitle: { type: String, default: '' },
    faqTitle: { type: String, default: 'Como funciona' },
    faqSub:   { type: String, default: '' },
    faq:      { type: Array,  default: () => [] }
});
const emit = defineEmits(['close']);

const drawerOpen = ref(false);
const isMobile = ref(false);
let _mqMobile = null;
function _onMqMobileChange(e) {
    isMobile.value = e.matches;
    if (!e.matches) drawerOpen.value = false;
}
function toggleDrawer() { drawerOpen.value = !drawerOpen.value; }
function fecharDrawer() { drawerOpen.value = false; }

const cfgOpen = ref(false);
function toggleCfg() { cfgOpen.value = !cfgOpen.value; }
function fecharCfg() { cfgOpen.value = false; }

onMounted(() => {
    if (typeof window !== 'undefined' && window.matchMedia) {
        _mqMobile = window.matchMedia('(max-width: 1023px)');
        isMobile.value = _mqMobile.matches;
        try { _mqMobile.addEventListener('change', _onMqMobileChange); }
        catch { _mqMobile.addListener(_onMqMobileChange); }
    }
});
onBeforeUnmount(() => {
    if (_mqMobile) {
        try { _mqMobile.removeEventListener('change', _onMqMobileChange); }
        catch { _mqMobile.removeListener(_onMqMobileChange); }
    }
});
</script>

<template>
    <Transition name="mcp-drawer-fade">
        <div
            v-show="isMobile && drawerOpen"
            class="mcp-mobile-drawer"
            :class="{ 'is-open': drawerOpen }"
        >
            <div id="mcp-mobile-drawer-target" class="mcp-mobile-drawer__scroll" />
        </div>
    </Transition>
    <Transition name="mcp-drawer-fade">
        <div
            v-show="isMobile && drawerOpen"
            class="mcp-mobile-drawer__backdrop"
            @click="fecharDrawer"
        />
    </Transition>

    <section class="mcp-page">
        <header class="mcp-page__head">
            <button
                class="mcp-menu-btn mcp-menu-btn--mobile-only"
                v-tooltip.bottom="'Configurações'"
                aria-label="Abrir menu de configurações"
                @click="toggleDrawer"
            >
                <i class="pi pi-bars" aria-hidden="true" />
                <span>Menu</span>
            </button>
            <div class="mcp-page__title">
                <i :class="icon" class="mcp-page__title-icon" aria-hidden="true" />
                <span>{{ title }}</span>
            </div>
            <div class="mcp-page__actions">
                <!-- Target dos <Teleport to="#cfg-page-actions"> dos ConfiguracoesXxxPage embutidos -->
                <div id="cfg-page-actions" class="mcp-page__actions-slot"></div>
                <button
                    class="mcp-close"
                    v-tooltip.bottom="'Voltar (Esc)'"
                    aria-label="Voltar (Esc)"
                    @click="emit('close')"
                >
                    <i class="pi pi-times" aria-hidden="true" />
                </button>
            </div>
        </header>

        <div v-if="subtitle" class="mcp-subheader" role="note">
            <i class="pi pi-info-circle mcp-subheader__icon" aria-hidden="true" />
            <span class="mcp-subheader__text">{{ subtitle }}</span>
        </div>

        <div class="mcp-body">
            <Teleport to="#mcp-mobile-drawer-target" :disabled="!isMobile">
                <aside class="mcp-side">
                    <button
                        class="mcp-cfg-btn"
                        :class="{ 'is-open': cfgOpen }"
                        @click="toggleCfg"
                    >
                        <i :class="cfgOpen ? 'pi pi-arrow-left' : 'pi pi-cog'" aria-hidden="true" />
                        <span>{{ cfgOpen ? 'Voltar' : 'Configurações' }}</span>
                        <i v-if="!cfgOpen" class="pi pi-chevron-down mcp-cfg-btn__chev" aria-hidden="true" />
                    </button>
                    <div v-if="cfgOpen" class="mcp-side__scroll mcp-side__scroll--cfg">
                        <MelissaConfigList @select="fecharCfg" />
                    </div>
                    <div v-else class="mcp-side__scroll">
                        <div v-if="faq.length" class="mcp-w mcp-w--side">
                            <div class="mcp-w__head">
                                <div class="mcp-w__icon"><i class="pi pi-question-circle" aria-hidden="true" /></div>
                                <div class="mcp-w__title">
                                    <div class="mcp-w__title-text">{{ faqTitle }}</div>
                                    <div v-if="faqSub" class="mcp-w__sub">{{ faqSub }}</div>
                                </div>
                            </div>
                            <div class="mcp-w__body">
                                <ul class="mcp-faq">
                                    <li v-for="(item, i) in faq" :key="i" class="mcp-faq__item">
                                        <i :class="item.icon" aria-hidden="true" />
                                        <span><strong>{{ item.label }}:</strong> {{ item.text }}</span>
                                    </li>
                                </ul>
                            </div>
                        </div>
                        <slot name="side" />
                        <!-- Target dos <Teleport to="#cfg-page-side"> das pages embarcadas
                             (ex: guia de formatacao do WhatsappPage na aba templates) -->
                        <div id="cfg-page-side" class="mcp-side-target"></div>
                    </div>
                </aside>
            </Teleport>

            <div class="mcp-main">
                <Suspense>
                    <template #default>
                        <slot />
                    </template>
                    <template #fallback>
                        <div class="mcp-loading">
                            <i class="pi pi-spin pi-spinner" aria-hidden="true" />
                            <span>Carregando…</span>
                        </div>
                    </template>
                </Suspense>
            </div>
        </div>
    </section>
</template>

<style scoped>
.mcp-page {
    position: absolute;
    inset: 6px 6px calc(var(--m-dock-h, 76px) + 6px) var(--m-config-aside-left, 6px);
    z-index: 40;
    display: flex; flex-direction: column;
    background: var(--m-bg-medium);
    backdrop-filter: blur(32px) saturate(160%);
    -webkit-backdrop-filter: blur(32px) saturate(160%);
    border: 1px solid var(--m-border);
    border-radius: 18px;
    box-shadow: 0 16px 48px rgba(0, 0, 0, 0.4);
    overflow: hidden;
    font-family: 'Segoe UI', system-ui, -apple-system, sans-serif;
    color: var(--m-text);
    animation: mcp-page-enter 240ms cubic-bezier(0.2, 0.7, 0.3, 1);
}
@keyframes mcp-page-enter {
    from { opacity: 0; transform: scale(0.985); }
    to   { opacity: 1; transform: scale(1); }
}

.mcp-page__head {
    display: flex; align-items: center; justify-content: space-between;
    padding: 14px 18px;
    border-bottom: 1px solid var(--m-border);
    flex-shrink: 0; gap: 10px;
}
.mcp-page__title {
    flex: 1; min-width: 0;
    display: flex; align-items: center; gap: 10px;
    font-size: 1rem; font-weight: 700; color: var(--m-text);
    flex-wrap: wrap;
}
.mcp-page__title-icon { color: var(--p-primary-color); font-size: 1.05rem; }
.mcp-page__actions { display: flex; align-items: center; gap: 6px; }
.mcp-page__actions-slot { display: flex; align-items: center; gap: 6px; }
.mcp-page__actions-slot:empty { display: none; }
.mcp-side-target { display: flex; flex-direction: column; gap: 12px; }
.mcp-side-target:empty { display: none; }

.mcp-close {
    width: 32px; height: 32px;
    display: grid; place-items: center;
    background: transparent;
    border: 1px solid var(--m-border);
    color: var(--m-text-muted);
    border-radius: 8px;
    cursor: pointer;
    transition: background-color 120ms ease, color 120ms ease;
}
.mcp-close:hover { background: var(--m-bg-soft-hover); color: var(--m-text); }
.mcp-menu-btn {
    display: none;
    align-items: center; gap: 6px;
    padding: 7px 12px;
    border-radius: 8px;
    border: 1px solid var(--m-border);
    background: var(--m-bg-soft);
    color: var(--m-text);
    cursor: pointer;
    font-size: 0.82rem; font-weight: 600;
    flex-shrink: 0;
}
.mcp-menu-btn:hover { background: var(--m-bg-soft-hover); }

.mcp-subheader {
    display: flex; align-items: flex-start; gap: 8px;
    padding: 10px 18px;
    background: var(--m-bg-soft);
    border-bottom: 1px solid var(--m-border);
    color: var(--m-text-muted);
    font-size: 0.78rem;
    flex-shrink: 0;
}
.mcp-subheader__icon { color: var(--p-primary-color); font-size: 0.85rem; margin-top: 2px; flex-shrink: 0; }

.mcp-body { flex: 1; min-height: 0; display: flex; overflow: hidden; }
.mcp-side {
    width: 320px; flex-shrink: 0;
    display: flex; flex-direction: column;
    background: var(--m-bg-soft);
    border-right: 1px solid var(--m-border);
    overflow: hidden;
}
.mcp-side__scroll {
    flex: 1; min-height: 0;
    overflow-y: auto; overflow-x: hidden;
    padding: 12px;
    display: flex; flex-direction: column; gap: 12px;
    scrollbar-width: thin;
    scrollbar-color: var(--m-border-strong) transparent;
}
.mcp-side__scroll::-webkit-scrollbar { width: 5px; }
.mcp-side__scroll::-webkit-scrollbar-thumb { background: var(--m-border-strong); border-radius: 3px; }

.mcp-cfg-btn {
    display: flex; align-items: center; gap: 8px;
    width: calc(100% - 24px);
    margin: 12px 12px 0;
    padding: 10px 12px;
    background: var(--m-bg-medium);
    border: 1px solid var(--m-border);
    border-radius: 9px;
    color: var(--m-text);
    cursor: pointer;
    font-family: inherit;
    font-size: 0.84rem; font-weight: 600;
    text-align: left;
    transition: background-color 120ms ease, border-color 120ms ease, color 120ms ease;
    flex-shrink: 0;
}
.mcp-cfg-btn:hover { background: var(--m-bg-soft-hover); border-color: var(--m-border-strong); }
.mcp-cfg-btn.is-open {
    background: color-mix(in srgb, var(--p-primary-color) 14%, transparent);
    border-color: color-mix(in srgb, var(--p-primary-color) 38%, transparent);
    color: var(--p-primary-color);
}
.mcp-cfg-btn > i:first-child { color: var(--p-primary-color); font-size: 0.92rem; }
.mcp-cfg-btn > span { flex: 1; }
.mcp-cfg-btn__chev { color: var(--m-text-muted); font-size: 0.7rem; }
.mcp-side__scroll--cfg { padding: 8px; gap: 0; }

.mcp-main {
    flex: 1; min-width: 0; min-height: 0;
    overflow-y: auto; overflow-x: hidden;
    padding: 16px;
    display: flex; flex-direction: column; gap: 12px;
    scrollbar-width: thin;
    scrollbar-color: var(--m-border-strong) transparent;
}
.mcp-main::-webkit-scrollbar { width: 5px; }
.mcp-main::-webkit-scrollbar-thumb { background: var(--m-border-strong); border-radius: 3px; }
.mcp-loading {
    display: flex; align-items: center; justify-content: center; gap: 10px;
    padding: 32px;
    color: var(--m-text-muted);
    font-size: 0.85rem;
}
.mcp-loading > i { color: var(--p-primary-color); font-size: 1.2rem; }

@media (min-width: 1024px) {
    .mcp-page { right: max(6px, min(50%, calc(100% - 1006px))); }
    .mcp-side > .mcp-side__scroll > .mcp-w--side {
        flex-shrink: 0;
        height: auto;
        display: flex;
        flex-direction: column;
    }
}

.mcp-w {
    background: var(--m-bg-soft);
    border: 1px solid var(--m-border);
    border-radius: 12px;
    overflow: hidden;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.08);
    display: flex; flex-direction: column;
}
.mcp-w--side { background: var(--m-bg-medium); box-shadow: 0 2px 10px rgba(0, 0, 0, 0.16); }
.mcp-w__head {
    display: flex; align-items: center; gap: 10px;
    padding: 12px 14px;
    border-bottom: 1px solid var(--m-border);
}
.mcp-w__icon {
    width: 36px; height: 36px;
    display: grid; place-items: center;
    border-radius: 9px;
    flex-shrink: 0;
    background: color-mix(in srgb, var(--p-primary-color) 15%, transparent);
    color: var(--p-primary-color);
}
.mcp-w__icon > i { font-size: 0.95rem; }
.mcp-w__title { flex: 1; min-width: 0; }
.mcp-w__title-text { font-size: 0.92rem; font-weight: 700; color: var(--m-text); line-height: 1.2; }
.mcp-w__sub { font-size: 0.74rem; color: var(--m-text-muted); margin-top: 2px; line-height: 1.3; }
.mcp-w__body { padding: 14px; display: flex; flex-direction: column; gap: 14px; }

.mcp-faq {
    display: flex; flex-direction: column; gap: 10px;
    list-style: none; margin: 0; padding: 0;
}
.mcp-faq__item {
    display: flex; gap: 10px;
    font-size: 0.78rem;
    color: var(--m-text-muted);
    line-height: 1.4;
}
.mcp-faq__item > i { color: var(--p-primary-color); font-size: 0.78rem; margin-top: 3px; flex-shrink: 0; }
.mcp-faq__item strong { color: var(--m-text); font-weight: 600; }

.mcp-mobile-drawer {
    position: fixed;
    top: 0; left: 0;
    height: 100vh; height: 100dvh;
    width: min(360px, 88vw);
    z-index: 80;
    background: var(--m-bg-medium);
    backdrop-filter: blur(28px) saturate(160%);
    -webkit-backdrop-filter: blur(28px) saturate(160%);
    border-right: 1px solid var(--m-border);
    transform: translateX(-100%);
    transition: transform 250ms cubic-bezier(0.4, 0, 0.2, 1);
    color: var(--m-text);
    font-family: 'Segoe UI', system-ui, -apple-system, sans-serif;
    display: flex; flex-direction: column;
}
.mcp-mobile-drawer.is-open { transform: translateX(0); }
.mcp-mobile-drawer__scroll {
    flex: 1; min-height: 0;
    overflow-y: auto; overflow-x: hidden;
    padding: 12px;
    display: flex; flex-direction: column; gap: 12px;
    scrollbar-width: thin;
    scrollbar-color: var(--m-border-strong) transparent;
}
.mcp-mobile-drawer__scroll::-webkit-scrollbar { width: 5px; }
.mcp-mobile-drawer__scroll::-webkit-scrollbar-thumb { background: var(--m-border-strong); border-radius: 3px; }
.mcp-mobile-drawer__scroll .mcp-side { width: 100%; border-right: none; }
.mcp-mobile-drawer__backdrop {
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.45);
    backdrop-filter: blur(4px);
    -webkit-backdrop-filter: blur(4px);
    z-index: 79;
}
.mcp-drawer-fade-enter-active,
.mcp-drawer-fade-leave-active { transition: opacity 200ms ease; }
.mcp-drawer-fade-enter-from,
.mcp-drawer-fade-leave-to { opacity: 0; }

@media (max-width: 1023px) {
    .mcp-body { flex-direction: column; padding: 0; }
    .mcp-body > .mcp-side { display: none; }
    .mcp-main { width: 100%; padding: 8px; }
    .mcp-page__title > span:first-of-type { display: none; }
    .mcp-page__title-icon { display: none; }
    .mcp-menu-btn--mobile-only { display: inline-flex; }
}
</style>
