<template>
  <div class="default-header-page">
    <q-breadcrumbs
      v-if="displayBreadcrumbs.length"
      class="q-mb-xs text-secondary"
      :class="$q.screen.lt.sm ? '' : 'q-pl-lg'"
    >
      <template #separator>
        <q-icon
          class="breadcrumb-separator"
          name="mdi-chevron-right"
          size="18px"
        />
      </template>

      <q-breadcrumbs-el
        v-for="crumb in displayBreadcrumbs"
        :key="crumb.name || crumb.label"
        :icon="crumb.icon"
        :label="crumb.title"
        :to="crumb.name ? { name: crumb.name, params: crumb.params } : null"
      />
    </q-breadcrumbs>

    <div class="flex items-center justify-between">
      <div class="flex items-center" :class="$q.screen.lt.sm ? '' : 'q-pt-md'">
        <span
          v-if="displayTitle"
          class="text-h6 text-primary text-weight-regular"
        >
          {{ displayTitle }}
        </span>

        <div v-else style="width: 280px">
          <q-skeleton type="text" height="40px" />
        </div>

        <q-icon
          v-if="showFilterIcon"
          class="q-ml-sm cursor-pointer"
          name="mdi-filter-outline"
          size="sm"
          :class="filterOpen ? 'bg-primary' : ''"
          :color="filterOpen ? 'background' : 'primary'"
          :style="
            filterOpen
              ? 'border-radius: 8px; padding: 2px'
              : 'border-radius: 8px; padding: 2px; border: 1.5px solid var(--q-primary)'
          "
          @click="$emit('show-filter')"
        />
      </div>

      <div
        class="flex items-center q-pr-sm"
        style="gap: 8px"
        :class="$q.screen.lt.sm ? '' : 'q-pt-md'"
      >
        <slot name="after" />

        <div class="flex items-center no-wrap" style="gap: 12px">
          <!-- Seletor de unidade: aparece apenas quando o usuário tem mais de uma -->
          <template v-if="userUnits.length > 1">
            <DefaultSelect
              :model-value="store.selectedUnit"
              :option-label="getUnitLabel"
              :options="userUnits"
              label="Unidade"
              option-value="id"
              outlined
              style="width: 280px; flex-shrink: 0"
              @update:model-value="store.setSelectedUnit"
            />

            <q-spinner
              v-if="isUnitLoading"
              color="primary"
              size="18px"
              style="flex-shrink: 0"
            />
          </template>

          <template v-if="$q.screen.gt.sm && lastLoginFormatted">
            <div
              class="column"
              style="line-height: 1.2; white-space: nowrap; flex-shrink: 0"
            >
              <span class="text-caption text-grey-6 text-primary text-center">
                Ultimo acesso
              </span>

              <span class="text-caption text-primary text-center">
                {{ lastLoginFormatted }}
              </span>
            </div>
          </template>

          <div
            class="flex items-center no-wrap"
            style="gap: 2px; flex-shrink: 0"
          >
            <!-- Notificações -->
            <q-btn flat round dense icon="mdi-bell-badge" color="secondary">
              <q-badge
                v-if="unreadCount > 0"
                color="negative"
                floating
                rounded
                :label="unreadCount"
              />

              <q-menu
                anchor="bottom right"
                class="header-menu"
                self="top right"
                :offset="[0, 8]"
                @before-show="loadNotifications"
              >
                <div class="notifications-panel">
                  <div class="row items-center justify-between q-px-md q-py-sm">
                    <span
                      class="text-subtitle2 text-primary text-weight-medium"
                    >
                      Notificações
                    </span>

                    <q-btn
                      color="secondary"
                      dense
                      flat
                      label="Marcar tudo como lido"
                      no-caps
                      size="sm"
                      :disable="unreadCount === 0"
                      @click="markAllAsRead"
                    />
                  </div>

                  <q-separator />

                  <q-scroll-area
                    v-if="notifications.length"
                    style="height: 360px"
                    :thumb-style="{ width: '5px', borderRadius: '4px' }"
                  >
                    <q-list separator>
                      <q-item
                        v-for="n in notifications"
                        :key="n.id"
                        v-close-popup
                        :class="!n.is_read ? 'bg-grey-2' : ''"
                        clickable
                        @click="openNotification(n)"
                      >
                        <q-item-section avatar top>
                          <q-icon
                            :color="n.is_read ? 'grey-5' : 'secondary'"
                            :name="iconFor(n.notification_type)"
                            size="24px"
                          />
                        </q-item-section>

                        <q-item-section>
                          <q-item-label
                            class="text-primary"
                            :class="!n.is_read ? 'text-weight-medium' : ''"
                          >
                            {{ n.title }}
                          </q-item-label>

                          <q-item-label caption lines="2">
                            {{ n.message }}
                          </q-item-label>

                          <q-item-label caption class="text-grey-6 q-mt-xs">
                            {{ formatNotificationDate(n.created_at) }}
                          </q-item-label>
                        </q-item-section>

                        <q-item-section v-if="!n.is_read" side top>
                          <q-badge
                            color="negative"
                            rounded
                            style="padding: 4px"
                          />
                        </q-item-section>
                      </q-item>
                    </q-list>
                  </q-scroll-area>

                  <div
                    v-else
                    class="column items-center justify-center q-pa-lg text-grey-6"
                  >
                    <q-icon name="mdi-bell-off-outline" size="32px" />

                    <span class="text-caption q-mt-sm">Sem notificações</span>
                  </div>
                </div>
              </q-menu>
            </q-btn>

            <!-- Perfil do usuário -->
            <q-btn flat round dense icon="mdi-account" color="secondary">
              <q-menu
                anchor="bottom right"
                class="header-menu"
                self="top right"
                :offset="[0, 8]"
              >
                <div class="profile-panel q-pa-md">
                  <div class="row items-center no-wrap q-gutter-x-md">
                    <q-img
                      class="avatar-circle"
                      :src="user?.avatar_url || 'icons/user-icon.jpg'"
                    />

                    <div class="column" style="min-width: 0">
                      <span
                        class="text-body2 text-primary text-weight-medium ellipsis"
                      >
                        {{ user?.name || "Usuário" }}
                      </span>

                      <span class="text-caption text-grey-6 ellipsis">
                        {{ user?.email }}
                      </span>
                    </div>
                  </div>

                  <q-separator class="q-my-md" />

                  <q-btn
                    v-close-popup
                    align="left"
                    class="full-width"
                    color="primary"
                    flat
                    icon="mdi-account-edit-outline"
                    label="Editar perfil"
                    no-caps
                    @click="goToEditProfile"
                  />
                </div>
              </q-menu>
            </q-btn>

            <!-- Configurações -->
            <q-btn color="secondary" dense flat icon="mdi-cog-outline" round>
              <q-menu
                anchor="bottom right"
                class="header-menu"
                self="top right"
                :offset="[0, 8]"
                @before-show="loadUnitDetails"
              >
                <div class="settings-panel q-pa-md">
                  <div class="column q-gutter-y-sm">
                    <div>
                      <span class="text-caption text-grey-6">
                        Nome da Unidade
                      </span>

                      <div
                        class="text-body2 text-primary text-weight-medium ellipsis"
                      >
                        {{ unitInfo.name || "Não informado" }}
                      </div>
                    </div>

                    <div>
                      <span class="text-caption text-grey-6">
                        Nome fantasia
                      </span>

                      <div
                        class="text-body2 text-primary text-weight-medium ellipsis"
                      >
                        {{ unitInfo.fantasy_name || "Não informado" }}
                      </div>
                    </div>

                    <div>
                      <span class="text-caption text-grey-6">Razão social</span>

                      <div
                        class="text-body2 text-primary text-weight-medium ellipsis"
                      >
                        {{ unitInfo.social_reason || "Não informado" }}
                      </div>
                    </div>

                    <div>
                      <span class="text-caption text-grey-6">
                        Franqueado Operador
                      </span>

                      <div
                        class="text-body2 text-primary text-weight-medium ellipsis"
                      >
                        {{ unitInfo.name_responsible || "Não informado" }}
                      </div>
                    </div>
                  </div>

                  <q-separator class="q-my-md" />

                  <q-btn
                    v-close-popup
                    align="left"
                    class="full-width"
                    color="primary"
                    flat
                    icon="mdi-store-edit-outline"
                    label="Editar unidade"
                    no-caps
                    @click="goToEditUnit"
                  />

                  <q-btn
                    v-close-popup
                    align="left"
                    class="full-width q-mt-xs"
                    color="negative"
                    flat
                    icon="mdi-logout"
                    label="Sair"
                    no-caps
                    @click="logoutFn"
                  />
                </div>
              </q-menu>
            </q-btn>
          </div>
        </div>
      </div>
    </div>

    <q-separator class="q-my-sm" />
  </div>
</template>

<script setup>
import { computed, onBeforeUnmount, onMounted, ref, watch } from "vue";
import { formatUnitName } from "src/helpers/utils";

import {
  getMyNotifications,
  getUnreadCount,
  markNotificationRead,
  markAllNotificationsRead,
} from "src/api/notification";

import { getUnitMe } from "src/api/unit";
import { joinRoom, leaveRoom, socket } from "src/boot/socket.io";
import { useAuth } from "src/composables/useAuth";
import { useI18n } from "vue-i18n";
import { useRoute, useRouter } from "vue-router";
import { userStore } from "src/stores/user";


import DefaultSelect from "src/components/defaults/DefaultSelect.vue";

const { breadcrumbs, filterOpen, showFilterIcon, title } = defineProps({
  breadcrumbs: {
    type: Array,
    default: null,
  },
  filterOpen: {
    type: Boolean,
    default: false,
  },
  showFilterIcon: {
    type: Boolean,
    default: false,
  },
  title: {
    type: [String, Object],
    default: null,
  },
});

defineEmits(["show-filter"]);

const route = useRoute();
const router = useRouter();
const { t } = useI18n();
const store = userStore();
const { logout } = useAuth();

const isUnitLoading = ref(false);

let loadingTimer = null;

const unitDetails = ref(null);
const unitInfo = computed(() => unitDetails.value || store.selectedUnit || {});

const user = computed(() => store.user);
const userUnits = computed(() => store.user?.units ?? []);

const displayBreadcrumbs = computed(() => {
  const items = breadcrumbs?.length ? breadcrumbs : route.meta?.breadcrumbs;

  return (items || []).map((b) => ({
    ...b,
    title: b.translate ? t(b.title) : b.title,
  }));
});

const displayTitle = computed(() => {
  if (title) {
    if (typeof title === "string") return title;

    if (title.translate) return t(title.value);

    return title.value;
  } else if (route.meta?.title) {
    const metaTitle = route.meta.title;

    if (typeof metaTitle === "string") return metaTitle;

    if (metaTitle.translate) return t(metaTitle.value);

    return metaTitle.value;
  }

  return null;
});

const lastLoginFormatted = computed(() => {
  const raw = store.user?.last_login_at;

  if (!raw) return null;

  const d = new Date(raw.replace(" ", "T"));

  return new Intl.DateTimeFormat("pt-BR", {
    day: "2-digit",
    month: "2-digit",
    year: "numeric",
    hour: "2-digit",
    minute: "2-digit",
    timeZone: "America/Fortaleza",
  }).format(d);
});

const userRoom = computed(() =>
  store.user?.id ? `user.${store.user.id}` : null,
);

const getUnitLabel = (unit) => formatUnitName(unit);

const goToEditProfile = () => {
  if (!user.value?.id) return;

  router.push({ name: "UserEditPage", params: { id: user.value.id } });
};

const goToEditUnit = () => {
  router.push({ name: "UnitDataPage" });
};

const loadUnitDetails = async () => {
  try {
    unitDetails.value = await getUnitMe();
  } catch (e) {
    unitDetails.value = store.selectedUnit;

    console.error("Falha ao carregar dados da unidade:", e);
  }
};

const logoutFn = async () => {
  await logout();

  router.push({ name: "LoginPage" });
};

// ------------------- Notificações (sino) -------------------

const notifications = ref([]);

const unreadCount = ref(0);

const NOTIFICATION_ICONS = {
  support_ticket_created: "mdi-lifebuoy",
  support_ticket_reply: "mdi-message-reply-text-outline",
  kanban_created: "mdi-clipboard-plus-outline",
  kanban_reply: "mdi-comment-text-outline",
  kanban_status_changed: "mdi-swap-horizontal",
  tbr_month_closed: "mdi-cash-multiple",
  account_payable_created: "mdi-file-document-plus-outline",
  payment_credited: "mdi-cash-check",
};

const formatNotificationDate = (raw) => {
  if (!raw) return "";

  const d = new Date(String(raw).replace(" ", "T"));

  return new Intl.DateTimeFormat("pt-BR", {
    day: "2-digit",
    month: "2-digit",
    year: "numeric",
    hour: "2-digit",
    minute: "2-digit",
  }).format(d);
};

const iconFor = (type) => NOTIFICATION_ICONS[type] || "mdi-bell-outline";

const loadNotifications = async () => {
  try {
    notifications.value = await getMyNotifications();
  } catch (e) {
    console.error("Falha ao carregar notificações:", e);
  }
};

const loadUnreadCount = async () => {
  try {
    unreadCount.value = await getUnreadCount();
  } catch (e) {
    console.error("Falha ao carregar contador de notificações:", e);
  }
};

const markAllAsRead = async () => {
  try {
    await markAllNotificationsRead();

    notifications.value.forEach((n) => (n.is_read = true));

    unreadCount.value = 0;
  } catch (e) {
    console.error("Falha ao marcar todas como lidas:", e);
  }
};

const openNotification = async (n) => {
  if (!n.is_read) {
    try {
      await markNotificationRead(n.id);

      n.is_read = true;

      unreadCount.value = Math.max(0, unreadCount.value - 1);
    } catch (e) {
      console.error("Falha ao marcar como lida:", e);
    }
  }
  if (n.url) router.push(n.url);
};

const onRealtimeNotification = () => {
  loadUnreadCount();

  loadNotifications();
};

onBeforeUnmount(() => {
  if (userRoom.value) {
    socket.off("notification", onRealtimeNotification);

    leaveRoom(userRoom.value);
  }
});

onMounted(() => {
  loadUnreadCount();

  if (userRoom.value) {
    joinRoom(userRoom.value);

    socket.on("notification", onRealtimeNotification);
  }
});

watch(
  () => store.selectedUnit?.id,
  (newId, oldId) => {
    if (newId && oldId !== undefined && newId !== oldId) {
      isUnitLoading.value = true;

      clearTimeout(loadingTimer);

      loadingTimer = setTimeout(() => {
        isUnitLoading.value = false;
      }, 1500);
    }
  },
);
</script>

<style scoped>
.default-header-page {
  padding-left: 4px;
}

.default-header-page :deep(.q-breadcrumbs) {
  display: inline-flex;
  align-items: center;
  max-width: 100%;
}

.default-header-page :deep(.q-breadcrumbs__el),
.default-header-page :deep(.q-breadcrumbs__separator) {
  flex: 0 0 auto;
  width: auto;
  display: inline-flex;
  align-items: center;
  height: 24px;
  line-height: 24px;
}

.default-header-page :deep(.q-breadcrumbs__el) {
  max-width: max-content;
}

.default-header-page :deep(.q-breadcrumbs__el-icon) {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 24px;
  height: 24px;
  margin-top: 0;
  line-height: 1;
  vertical-align: middle;
}

.breadcrumb-separator {
  align-self: center;
  line-height: 1;
}

.header-menu {
  border-radius: 10px;
}

.notifications-panel {
  width: 340px;
  max-width: 90vw;
}

.avatar-circle {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  flex-shrink: 0;
}

.profile-panel,
.settings-panel {
  width: 280px;
  max-width: 90vw;
}
</style>
