<template>
  <div class="main-layout">
    <header class="app-header">
      <div class="toolbar-title">mitlist</div>
      <div class="user-menu" v-if="authStore.isAuthenticated">
        <button @click="toggleUserMenu" class="user-menu-button">
          <!-- Placeholder for user icon -->
          <svg xmlns="http://www.w3.org/2000/svg" height="24px" viewBox="0 0 24 24" width="24px" fill="#ff7b54">
            <path d="M0 0h24v24H0z" fill="none" />
            <path
              d="M12 12c2.21 0 4-1.79 4-4s-1.79-4-4-4-4 1.79-4 4 1.79 4 4 4zm0 2c-2.67 0-8 1.34-8 4v2h16v-2c0-2.66-5.33-4-8-4z" />
          </svg>
        </button>
        <div v-if="userMenuOpen" class="dropdown-menu" ref="userMenuDropdown">
          <a href="#" @click.prevent="handleLogout">Logout</a>
        </div>
      </div>
    </header>

    <main class="page-container">
      <keep-alive>
        <router-view v-slot="{ Component, route }">
          <component :is="Component" v-if="route.meta.keepAlive" />
          <component :is="Component" v-else :key="route.fullPath" />
        </router-view>
      </keep-alive>
    </main>

    <OfflineIndicator />

    <footer class="app-footer">
      <nav class="tabs">
        <router-link to="/lists" class="tab-item" active-class="active">
          <span class="material-icons">list</span>
          <span class="tab-text">Lists</span>
        </router-link>
        <router-link to="/groups" class="tab-item" active-class="active">
          <span class="material-icons">group</span>
          <span class="tab-text">Groups</span>
        </router-link>
        <router-link to="/chores" class="tab-item" active-class="active">
          <span class="material-icons">person_pin_circle</span>
          <span class="tab-text">Chores</span>
        </router-link>
        <!-- <router-link to="/account" class="tab-item" active-class="active">
          <span class="material-icons">person</span>
          <span class="tab-text">Account</span>
        </router-link> -->
      </nav>
    </footer>
  </div>
</template>

<script setup lang="ts">
import { ref, defineComponent, onMounted } from 'vue';
import { useRouter } from 'vue-router';
import { useAuthStore } from '@/stores/auth';
import OfflineIndicator from '@/components/OfflineIndicator.vue';
import { onClickOutside } from '@vueuse/core';
import { useNotificationStore } from '@/stores/notifications';

defineComponent({
  name: 'MainLayout'
});

const router = useRouter();
const authStore = useAuthStore();
const notificationStore = useNotificationStore();

// Add initialization logic
const initializeApp = async () => {
  if (authStore.isAuthenticated) {
    try {
      await authStore.fetchCurrentUser();
    } catch (error) {
      console.error('Failed to initialize app:', error);
      // Don't automatically logout - let the API interceptor handle token refresh
      // The response interceptor will handle 401s and refresh tokens automatically
      console.log('Token may need refresh, will be handled by API interceptor');
    }
  }
};

// Call initialization when component is mounted
onMounted(() => {
  initializeApp();
});

const userMenuOpen = ref(false);
const userMenuDropdown = ref<HTMLElement | null>(null);

const toggleUserMenu = () => {
  userMenuOpen.value = !userMenuOpen.value;
};

onClickOutside(userMenuDropdown, () => {
  userMenuOpen.value = false;
}, { ignore: ['.user-menu-button'] });


const handleLogout = async () => {
  try {
    authStore.logout(); // Pinia action
    notificationStore.addNotification({
      type: 'success',
      message: 'Logged out successfully',
    });
    await router.push('/auth/login'); // Adjusted path
  } catch (error: unknown) {
    notificationStore.addNotification({
      type: 'error',
      message: error instanceof Error ? error.message : 'Logout failed',
    });
  }
  userMenuOpen.value = false;
};
</script>

<style lang="scss" scoped>
.main-layout {
  display: flex;
  flex-direction: column;
  min-height: 100vh;
}

.app-header {
  background-color: #fff8f0;
  padding: 0 1rem;
  height: var(--header-height);
  display: flex;
  align-items: center;
  justify-content: space-between;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
  position: sticky;
  top: 0;
  z-index: 100;
}

.toolbar-title {
  font-size: 1.5rem;
  font-weight: 500;
  letter-spacing: 0.1ch;
  color: var(--primary);
}

.user-menu {
  position: relative;
}

.user-menu-button {
  background: none;
  border: none;
  color: var(--primary);
  cursor: pointer;
  padding: 0.5rem;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;

  &:hover {
    background-color: rgba(255, 255, 255, 0.1);
  }
}

.dropdown-menu {
  position: absolute;
  right: 0;
  top: calc(100% + 5px);
  color: var(--primary);
  background-color: #f3f3f3;
  border: 1px solid #ddd;
  border-radius: 4px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.15);
  min-width: 150px;
  z-index: 101;

  a {
    display: block;
    padding: 0.5rem 1rem;
    color: var(--text-color);
    text-decoration: none;

    &:hover {
      background-color: #f5f5f5;
    }
  }
}


.page-container {
  flex-grow: 1;
  padding-bottom: calc(var(--footer-height) + 1rem); // Space for fixed footer
}

.app-footer {
  background-color: white;
  border-top: 1px solid #e0e0e0;
  height: var(--footer-height);
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  z-index: 100;
}

.tabs {
  display: flex;
  height: 100%;
}

.tab-item {
  flex-grow: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  color: var(--text-color);
  text-decoration: none;
  font-size: 0.8rem;
  padding: 0.5rem 0;
  border-bottom: 2px solid transparent;
  gap: 4px;

  .material-icons {
    font-size: 24px;
  }

  .tab-text {
    display: none;
  }

  @media (min-width: 768px) {
    flex-direction: row;
    gap: 8px;

    .tab-text {
      display: inline;
    }
  }

  &.active {
    color: var(--primary-color);
    border-bottom-color: var(--primary-color);
  }

  &:hover {
    background-color: #f0f0f0;
  }
}
</style>
