<template>
  <v-menu
    v-model="menu"
    :close-on-content-click="false"
    location="top end"
    origin="bottom right"
    transition="scale-transition"
    open-on-hover
  >
    <template v-slot:activator="{ props: menuProps }">
      <v-list-item
        v-if="!rail"
        v-bind="menuProps"
        :title="user?.name || user?.preferred_username || 'User'"
        :subtitle="user?.email || 'No email'"
        class="pa-3 ma-1 rounded-xl profile-activator"
        link
      >
        <template v-slot:prepend>
          <div class="profile-avatar-container">
            <v-avatar size="44" class="profile-avatar elevation-8">
              <v-img
                :src="getAvatarSrc(user?.picture)"
                :alt="`${user?.name || 'User'} Profile`"
              ></v-img>
            </v-avatar>
            <div class="status-dot"></div>
          </div>
        </template>
        <template v-slot:append>
          <v-icon size="20" class="chevron-icon">mdi-menu-down</v-icon>
        </template>
      </v-list-item>

      <v-btn
        v-else
        icon
        v-bind="menuProps"
        size="large"
        variant="flat"
        class="avatar-btn"
        :title="user?.name || 'Profile'"
      >
        <div class="profile-avatar-container">
          <v-avatar size="44" class="profile-avatar elevation-8">
            <v-img
              :src="getAvatarSrc(user?.picture)"
              :alt="`${user?.name || 'User'} Profile`"
            ></v-img>
          </v-avatar>
          <div class="status-dot"></div>
        </div>
      </v-btn>
    </template>

    <v-card class="profile-card rounded-xl elevation-12" width="340">
      <!-- Decorative Header -->
      <div class="card-header">
        <div class="header-pattern"></div>
        <div class="header-content pa-6 text-center">
          <div class="profile-avatar-container mb-4">
            <v-avatar size="90" class="profile-avatar elevation-8">
              <v-img
                :src="getAvatarSrc(user?.picture)"
                :alt="`${user?.name || 'User'} Profile`"
              ></v-img>
            </v-avatar>
            <div class="status-badge">
              <v-icon size="12" color="white">mdi-check</v-icon>
            </div>
          </div>
          <h3 class="text-h6 font-weight-bold text-white mb-1">
            {{ user?.name || user?.preferred_username || 'User' }}
          </h3>
          <p class="text-body-2 text-white opacity-90 mb-2">
            {{ user?.email || 'No email' }}
          </p>
          <v-chip
            size="small"
            class="glass-chip"
            prepend-icon="mdi-identifier"
          >
            {{ user?.id?.substring(0, 10) }}...
          </v-chip>
        </div>
      </div>

      <!-- Menu Items -->
      <v-card-text class="pa-4">
        <div class="menu-grid">
          <div 
            class="menu-tile rounded-lg pa-4 text-center cursor-pointer"
            @click="handleAction('profile')"
          >
            <div class="tile-icon-wrapper mb-2 mx-auto">
              <v-icon size="24" color="primary-600">mdi-account-circle</v-icon>
            </div>
            <div class="text-caption font-weight-medium">Profil</div>
          </div>
        </div>

        <v-divider class="my-4"></v-divider>

        <!-- Logout Button -->
        <v-btn
          block
          class="rounded-lg logout-btn"
          variant="flat"
          color="error"
          prepend-icon="mdi-logout"
          @click="signOut"
          :disabled="isLoggingOut"
          :loading="isLoggingOut"
        >
          {{ isLoggingOut ? 'Keluar...' : 'Keluar' }}
        </v-btn>
      </v-card-text>
    </v-card>
  </v-menu>
</template>

<script setup>
import { ref } from 'vue';
import { navigateTo } from '#app';
import { DEFAULT_AVATAR } from '@/constants/avatar';

const props = defineProps({
  user: {
    type: Object,
    required: true
  },
  rail: {
    type: Boolean,
    required: true
  }
})

const menu = ref(false);
const isLoggingOut = ref(false);
const emit = defineEmits(['logout']);

const getAvatarSrc = (userPicture) => {
  return userPicture || DEFAULT_AVATAR;
};

const signOut = async () => {
  if (isLoggingOut.value) return;
  isLoggingOut.value = true;
  menu.value = false;

  try {
    emit('logout'); 
  } finally {
    isLoggingOut.value = false;
  }
};

const handleAction = (action) => {
  switch(action) {
    case 'profile':
      navigateTo('/Profile/Profil')
      break;
  }

  menu.value = false;
};
</script>

<style scoped lang="scss">
@import '@/assets/scss/_colors.scss';

.profile-activator {
  background: linear-gradient(135deg, $primary-100 0%, $primary-50 100%);
  border: 1px solid $primary-200;
  transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
}

.profile-activator:hover {
  background: linear-gradient(135deg, $primary-200 0%, $primary-100 100%);
  border-color: $primary-300;
  transform: translateY(-2px);
}

.profile-avatar-container {
  position: relative;
  display: inline-block;
}

.profile-avatar {
  border: 3px solid white;
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.15);
}

.status-dot {
  position: absolute;
  bottom: 2px;
  right: 2px;
  width: 12px;
  height: 12px;
  background: linear-gradient(135deg, $success-600 0%, $success-400 100%);
  border: 2px solid white;
  border-radius: 50%;
  animation: pulse 2s infinite;
}

@keyframes pulse {
  0%, 100% {
    opacity: 1;
    transform: scale(1);
  }
  50% {
    opacity: 0.8;
    transform: scale(1.1);
  }
}

.chevron-icon {
  transition: transform 0.3s ease;
}

.profile-activator:hover .chevron-icon {
  transform: translateY(2px);
}

.avatar-btn {
  background: transparent !important;
}

.profile-card {
  overflow: hidden;
  border: 1px solid $primary-200;
}

.card-header {
  position: relative;
  background: linear-gradient(135deg, $primary-700 0%, $primary-500 100%);
  overflow: hidden;
}

.header-pattern {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-image: 
    radial-gradient(circle at 20% 50%, rgba(255, 255, 255, 0.1) 0%, transparent 50%),
    radial-gradient(circle at 80% 80%, rgba(255, 255, 255, 0.1) 0%, transparent 50%);
}

.header-content {
  position: relative;
  z-index: 1;
}


.status-badge {
  position: absolute;
  bottom: 0;
  right: 0;
  width: 28px;
  height: 28px;
  background: linear-gradient(135deg, $success-600 0%, $success-400 100%);
  border: 3px solid white;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.2);
}

.glass-chip {
  background: rgba(255, 255, 255, 0.2) !important;
  backdrop-filter: blur(10px);
  color: white !important;
  border: 1px solid rgba(255, 255, 255, 0.3);
}

.menu-grid {
  display: grid;
  grid-template-columns: 1fr;
  gap: 12px;
}

.menu-tile {
  background: linear-gradient(135deg, $primary-100 0%, $primary-50 100%);
  border: 1px solid $primary-200;
  transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
  cursor: pointer;
}

.menu-tile:hover {
  background: linear-gradient(135deg, $primary-200 0%, $primary-100 100%);
  border-color: $primary-300;
  transform: translateY(-4px);
  box-shadow: 0 8px 16px rgba(0, 0, 0, 0.1);
}

.tile-icon-wrapper {
  width: 48px;
  height: 48px;
  background: white;
  border-radius: 12px;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.08);
}

.quick-actions {
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.action-item {
  transition: all 0.2s ease;
  background: transparent;
}

.action-item:hover {
  background: linear-gradient(135deg, $primary-100 0%, $primary-50 100%);
  transform: translateX(4px);
}

.logout-btn {
  text-transform: none;
  font-weight: 600;
  letter-spacing: 0.5px;
  box-shadow: 0 4px 12px rgba(222, 71, 59, 0.3);
}

.logout-btn:hover {
  box-shadow: 0 6px 20px rgba(222, 71, 59, 0.4);
}

.cursor-pointer {
  cursor: pointer;
}

.opacity-90 {
  opacity: 0.9;
}
</style>