<script setup lang="ts">
import { ref, computed, watch } from 'vue';
import { useBreakpoints } from '@vueuse/core';
import { useRouter } from 'vue-router';
import AppSidebar from './AppSidebar.vue';
import AppTopbar from './AppTopbar.vue';
import { BREAKPOINTS, LAYOUT } from '@/core/constants/app.constants';

const breakpoints = useBreakpoints(BREAKPOINTS);
const isMobile = breakpoints.smaller('tablet');
const isTablet = breakpoints.between('tablet', 'desktop');
const isDesktop = breakpoints.greaterOrEqual('desktop');

// 사이드바 상태
const sidebarVisible = ref(true);
const sidebarCollapsed = ref(false);

// 반응형 자동 조절
watch(isMobile, (mobile) => {
  if (mobile) {
    sidebarVisible.value = false;
    sidebarCollapsed.value = false;
  } else {
    sidebarVisible.value = true;
  }
}, { immediate: true });

watch(isTablet, (tablet) => {
  if (tablet) sidebarCollapsed.value = true;
}, { immediate: true });

watch(isDesktop, (desktop) => {
  if (desktop) {
    sidebarVisible.value = true;
    sidebarCollapsed.value = false;
  }
}, { immediate: true });

function toggleSidebar() {
  if (isMobile.value) {
    sidebarVisible.value = !sidebarVisible.value;
  } else {
    sidebarCollapsed.value = !sidebarCollapsed.value;
  }
}

// 모바일에서 라우트 변경 시 사이드바 닫기
const router = useRouter();
router.afterEach(() => {
  if (isMobile.value) sidebarVisible.value = false;
});

const contentMarginLeft = computed(() => {
  if (isMobile.value) return '0';
  return sidebarCollapsed.value
    ? `${LAYOUT.sidebarCollapsedWidth}px`
    : `${LAYOUT.sidebarWidth}px`;
});
</script>

<template>
  <div class="app-layout">
    <!-- Overlay (모바일 사이드바 열림 시) -->
    <div
      v-if="isMobile && sidebarVisible"
      class="app-layout__overlay"
      @click="sidebarVisible = false"
    />

    <!-- Sidebar -->
    <AppSidebar
      :visible="sidebarVisible"
      :collapsed="sidebarCollapsed"
      :mobile="isMobile"
    />

    <!-- Main -->
    <div
      class="app-layout__main"
      :style="{ marginLeft: contentMarginLeft }"
    >
      <AppTopbar @toggle-sidebar="toggleSidebar" />

      <main class="app-layout__content">
        <router-view v-slot="{ Component }">
          <transition name="fade" mode="out-in">
            <component :is="Component" />
          </transition>
        </router-view>
      </main>
    </div>
  </div>
</template>

<style lang="scss" scoped>
@use '@/assets/styles/variables' as *;

.app-layout {
  min-height: 100vh;
  background: $color-surface;

  &__overlay {
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.4);
    z-index: calc($z-sidebar - 1);
  }

  &__main {
    transition: margin-left 0.2s ease;
    min-height: 100vh;
    display: flex;
    flex-direction: column;
  }

  &__content {
    flex: 1;
    padding: $page-padding-y $page-padding-x;
    padding-top: calc($topbar-height + $page-padding-y);

    @media (max-width: $bp-mobile) {
      padding: $space-sm;
      padding-top: calc($topbar-height + $space-sm);
    }
  }
}

// Page transition
.fade-enter-active,
.fade-leave-active {
  transition: opacity 0.15s ease;
}
.fade-enter-from,
.fade-leave-to {
  opacity: 0;
}
</style>
