<script setup>
/*
 * MelissaPrecificacao — Pagina nativa Melissa pra "Precificacao".
 *
 * Substitui o embed cfg-precificacao que vivia dentro do MelissaConfiguracoes.
 * Layout 2-col:
 *   - COL 1 (sidebar) — Botao "Configuracoes" (alterna pra MelissaConfigList)
 *     + Card "Como funciona" (FAQ explicando tipos de servico e preco)
 *   - COL 2 (main) — Reusa <ConfiguracoesPrecificacaoPage /> direto pra
 *     evitar duplicar a logica de tabela de servicos (~400 linhas).
 *
 * Quando aprovado o pattern visual, replica nos outros 16 cfg-* embeds.
 * Pra essa primeira iteracao, o main reusa a page existente — alternativa
 * cleaner seria mover a logica pra ca, mas fica pra refator dedicado.
 */
import { ref, onMounted, onBeforeUnmount } from 'vue';
import MelissaConfigList from './MelissaConfigList.vue';
import ConfiguracoesPrecificacaoPage from '@/layout/configuracoes/ConfiguracoesPrecificacaoPage.vue';

const emit = defineEmits(['close']);

// ── Breakpoints + drawer ───────────────────────────────────
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; }

// Toggle entre cards (default) e lista de configs (alterna inline na sidebar)
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="mpc-drawer-fade">
        <div
            v-show="isMobile && drawerOpen"
            class="mpc-mobile-drawer"
            :class="{ 'is-open': drawerOpen }"
        >
            <div id="mpc-mobile-drawer-target" class="mpc-mobile-drawer__scroll" />
        </div>
    </Transition>
    <Transition name="mpc-drawer-fade">
        <div
            v-show="isMobile && drawerOpen"
            class="mpc-mobile-drawer__backdrop"
            @click="fecharDrawer"
        />
    </Transition>

    <section class="mpc-page">
        <header class="mpc-page__head">
            <button
                class="mpc-menu-btn mpc-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="mpc-page__title">
                <i class="pi pi-tag mpc-page__title-icon" aria-hidden="true" />
                <span>Precificação</span>
            </div>
            <div class="mpc-page__actions">
                <button
                    class="mpc-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 class="mpc-subheader" role="note">
            <i class="pi pi-info-circle mpc-subheader__icon" aria-hidden="true" />
            <span class="mpc-subheader__text">
                Defina o valor padrão da sessão e os preços por tipo de compromisso.
                Esses valores aparecem ao agendar e cobrar pacientes.
            </span>
        </div>

        <div class="mpc-body">
            <Teleport to="#mpc-mobile-drawer-target" :disabled="!isMobile">
                <aside class="mpc-side">
                    <button
                        class="mpc-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 mpc-cfg-btn__chev" aria-hidden="true" />
                    </button>
                    <div v-if="cfgOpen" class="mpc-side__scroll mpc-side__scroll--cfg">
                        <MelissaConfigList @select="fecharCfg" />
                    </div>
                    <div v-else class="mpc-side__scroll">
                        <!-- Card: Como funciona -->
                        <div class="mpc-w mpc-w--side">
                            <div class="mpc-w__head">
                                <div class="mpc-w__icon"><i class="pi pi-question-circle" aria-hidden="true" /></div>
                                <div class="mpc-w__title">
                                    <div class="mpc-w__title-text">Como funciona</div>
                                    <div class="mpc-w__sub">Tipos de serviço e preços</div>
                                </div>
                            </div>
                            <div class="mpc-w__body">
                                <ul class="mpc-faq">
                                    <li class="mpc-faq__item">
                                        <i class="pi pi-tag" aria-hidden="true" />
                                        <span><strong>Serviço:</strong> nome (ex: Consulta inicial), preço e duração.</span>
                                    </li>
                                    <li class="mpc-faq__item">
                                        <i class="pi pi-clock" aria-hidden="true" />
                                        <span><strong>Duração dinâmica:</strong> habilitada nas configs da agenda; cada serviço pode ter duração própria.</span>
                                    </li>
                                    <li class="mpc-faq__item">
                                        <i class="pi pi-eye-slash" aria-hidden="true" />
                                        <span><strong>Inativar:</strong> mantém o histórico mas remove dos formulários novos.</span>
                                    </li>
                                </ul>
                            </div>
                        </div>
                    </div>
                </aside>
            </Teleport>

            <div class="mpc-main">
                <ConfiguracoesPrecificacaoPage />
            </div>
        </div>
    </section>
</template>

<style scoped>
/* ═══════ Page chrome ═══════ */
.mpc-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: mpc-page-enter 240ms cubic-bezier(0.2, 0.7, 0.3, 1);
}
@keyframes mpc-page-enter {
    from { opacity: 0; transform: scale(0.985); }
    to   { opacity: 1; transform: scale(1); }
}

.mpc-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;
}
.mpc-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;
}
.mpc-page__title-icon { color: var(--p-primary-color); font-size: 1.05rem; }
.mpc-page__actions { display: flex; align-items: center; gap: 6px; }

.mpc-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;
}
.mpc-close:hover { background: var(--m-bg-soft-hover); color: var(--m-text); }
.mpc-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;
}
.mpc-menu-btn:hover { background: var(--m-bg-soft-hover); }

.mpc-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;
}
.mpc-subheader__icon {
    color: var(--p-primary-color);
    font-size: 0.85rem;
    margin-top: 2px;
    flex-shrink: 0;
}

/* ═══════ Body 2-col ═══════ */
.mpc-body {
    flex: 1;
    min-height: 0;
    display: flex;
    overflow: hidden;
}
.mpc-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;
}
.mpc-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;
}
.mpc-side__scroll::-webkit-scrollbar { width: 5px; }
.mpc-side__scroll::-webkit-scrollbar-thumb {
    background: var(--m-border-strong);
    border-radius: 3px;
}

/* Botao "Configuracoes" no topo da sidebar — alterna entre cards e lista. */
.mpc-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;
}
.mpc-cfg-btn:hover {
    background: var(--m-bg-soft-hover);
    border-color: var(--m-border-strong);
}
.mpc-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);
}
.mpc-cfg-btn > i:first-child {
    color: var(--p-primary-color);
    font-size: 0.92rem;
}
.mpc-cfg-btn > span { flex: 1; }
.mpc-cfg-btn__chev {
    color: var(--m-text-muted);
    font-size: 0.7rem;
}
.mpc-side__scroll--cfg {
    padding: 8px;
    gap: 0;
}

.mpc-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;
}
.mpc-main::-webkit-scrollbar { width: 5px; }
.mpc-main::-webkit-scrollbar-thumb {
    background: var(--m-border-strong);
    border-radius: 3px;
}

/* Desktop: fake dialog (largura adaptativa) — espelha pattern de
   Seguranca/Pagamento/etc. Sidebar cards com altura por conteudo. */
@media (min-width: 1024px) {
    .mpc-page {
        right: max(6px, min(50%, calc(100% - 1006px)));
    }
    .mpc-side > .mpc-side__scroll > .mpc-w--side {
        flex-shrink: 0;
        height: auto;
        display: flex;
        flex-direction: column;
    }
}

/* ═══════ Card-base sidebar ═══════ */
.mpc-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;
}
.mpc-w--side {
    background: var(--m-bg-medium);
    box-shadow: 0 2px 10px rgba(0, 0, 0, 0.16);
}
.mpc-w__head {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 12px 14px;
    border-bottom: 1px solid var(--m-border);
}
.mpc-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);
}
.mpc-w__icon > i { font-size: 0.95rem; }
.mpc-w__title { flex: 1; min-width: 0; }
.mpc-w__title-text {
    font-size: 0.92rem;
    font-weight: 700;
    color: var(--m-text);
    line-height: 1.2;
}
.mpc-w__sub {
    font-size: 0.74rem;
    color: var(--m-text-muted);
    margin-top: 2px;
    line-height: 1.3;
}
.mpc-w__body {
    padding: 14px;
    display: flex;
    flex-direction: column;
    gap: 14px;
}

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

/* ═══════ Mobile drawer ═══════ */
.mpc-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;
}
.mpc-mobile-drawer.is-open { transform: translateX(0); }
.mpc-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;
}
.mpc-mobile-drawer__scroll::-webkit-scrollbar { width: 5px; }
.mpc-mobile-drawer__scroll::-webkit-scrollbar-thumb {
    background: var(--m-border-strong);
    border-radius: 3px;
}
.mpc-mobile-drawer__scroll .mpc-side {
    width: 100%;
    border-right: none;
}
.mpc-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;
}
.mpc-drawer-fade-enter-active,
.mpc-drawer-fade-leave-active { transition: opacity 200ms ease; }
.mpc-drawer-fade-enter-from,
.mpc-drawer-fade-leave-to { opacity: 0; }

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