<template>
  <div class="loyalty-suite-sidebar">
    <div class="loyalty-suite-sidebar__header">
      <div class="loyalty-suite-sidebar__header-wrapper">
        <q-btn icon="arrow_back" color="primary" class="loyalty-suite-sidebar__back" @click="goBack" />
        <span class="loyalty-suite-sidebar__title">Loyalty Suite</span>
      </div>
    </div>
    <q-scroll-area class="loyalty-suite-sidebar__scroll">
      <q-list class="loyalty-suite-sidebar__list">

        <!-- Overview -->
        <router-link class="loyalty-suite-sidebar__item" :to="'/'" exact>
          <q-item-section avatar>
            <q-icon name="dashboard" />
          </q-item-section>
          <q-item-section class="loyalty-suite-sidebar__section">{{ $t('overview') }}</q-item-section>
        </router-link>

        <!-- Wallet -->
        <router-link :to="'/wallets/list'" class="loyalty-suite-sidebar__item" exact>
          <q-item-section avatar>
            <q-icon name="account_balance_wallet" />
          </q-item-section>
          <q-item-section class="loyalty-suite-sidebar__section">{{ $t('walletsMenu') }}</q-item-section>
        </router-link>

        <!-- Ranks -->
        <router-link :to="'/ranks/list'" class="loyalty-suite-sidebar__item" exact>
          <q-item-section avatar>
            <q-icon name="military_tech" />
          </q-item-section>
          <q-item-section class="loyalty-suite-sidebar__section">{{ $t('ranksMenu') }}</q-item-section>
        </router-link>

        <!-- Achievements -->
        <router-link :to="'/achievements/list'" class="loyalty-suite-sidebar__item" exact>
          <q-item-section avatar>
            <q-icon name="emoji_events" />
          </q-item-section>
          <q-item-section class="loyalty-suite-sidebar__section">{{ $t('achievementsMenu') }}</q-item-section>
        </router-link>

        <!-- Challenges -->
        <router-link :to="'/challenges/list'" class="loyalty-suite-sidebar__item" exact>
          <q-item-section avatar>
            <q-icon name="campaign" />
          </q-item-section>
          <q-item-section class="loyalty-suite-sidebar__section">{{ $t('challengesMenu') }}</q-item-section>
        </router-link>

        <!-- User Rewards -->
        <router-link :to="'/rewards/users'" class="loyalty-suite-sidebar__item" exact>
          <q-item-section avatar>
            <q-icon name="card_membership" />
          </q-item-section>
          <q-item-section class="loyalty-suite-sidebar__section">{{ $t('userRewards') }}</q-item-section>
        </router-link>

        <!-- User Submissions -->
        <router-link v-if="isPro" :to="'/submissions/list'" class="loyalty-suite-sidebar__item" exact>
          <q-item-section avatar>
            <q-icon name="assignment" />
          </q-item-section>
          <q-item-section class="loyalty-suite-sidebar__section">{{ $t('userSubmissions') }}</q-item-section>
          <span v-if="hasSubmission" class="loyalty-suite-sidebar__badge">{{ submissionCount }}</span>
        </router-link>

        <!-- Analytics -->
        <router-link :to="'/analytics'" class="loyalty-suite-sidebar__item" exact>
          <q-item-section avatar>
            <q-icon name="assessment" />
          </q-item-section>
          <q-item-section class="loyalty-suite-sidebar__section">{{ $t('analyticsEdit') }}</q-item-section>
        </router-link>

        <!-- Settings -->
        <router-link :to="'/settings'" class="loyalty-suite-sidebar__item" exact>
          <q-item-section avatar>
            <q-icon name="settings" />
          </q-item-section>
          <q-item-section class="loyalty-suite-sidebar__section">{{ $t('settingsMenu') }}</q-item-section>
        </router-link>
      </q-list>
    </q-scroll-area>
  </div>
</template>

<script lang="ts">
import { defineComponent, ref, onBeforeMount, watch  } from 'vue';
import { useRoute } from 'vue-router';

export default defineComponent({
  setup() {
    const apiRoutes = window.loyalty_suite_admin_data.apiRoutes;
    const nonce = window.loyalty_suite_admin_data.nonce;
    const isPro = window.loyalty_suite_admin_data.isPro;
    const route = useRoute();
    const hasSubmission = ref(false);
    const submissionCount = ref(0);

    const fetchHasSubmission = async () => {
      try {
        const response = await fetch(apiRoutes.admin.has_submission, {
          headers: {
            'Content-Type': 'application/json',
            'X-WP-Nonce': nonce
          }
        });

        const data = await response.json();
        if (data?.success === true) {
          hasSubmission.value = true;
          submissionCount.value = data.count || 0;
          window.loyalty_suite_admin_data.adminNotifications = data.notifications || {};
        }
      } catch (error) {
        console.error('Failed to fetch submission status:', error);
      }
    };

    const updateCountAfterClear = async () => {
      const id = route.params.id;
      try {
        const response = await fetch(apiRoutes.admin.notification_clear, {
          method: 'POST',
          headers: {
            'Content-Type': 'application/json',
            'X-WP-Nonce': nonce
          },
          body: JSON.stringify({ id })
        });

        const data = await response.json();
        if (data?.success) {
          submissionCount.value = data.count || 0;
          hasSubmission.value = submissionCount.value > 0;
          window.loyalty_suite_admin_data.adminNotifications = data.notifications || {};
        }
      } catch (err) {
        console.error('Failed to clear and update notification count:', err);
      }
    };

    watch(
      () => route.fullPath,
      (newPath) => {
        if (/^\/submission\/\d+/.test(newPath)) {
          updateCountAfterClear();
        }
      },
      { immediate: true }
    );

    const goBack = () => {
      window.location.href = window.loyalty_suite_admin_data.adminUrl;
    };

    onBeforeMount(() => {
      fetchHasSubmission();
    });

    return { goBack, hasSubmission, submissionCount, isPro };
  }
});
</script>

<style lang="scss">
.loyalty-suite-sidebar {
  display: flex;
  flex-shrink: 0;
  flex-direction: column;
  width: 240px;
  height: 100%;
  background: $white;
  box-shadow: 2px 0 10px rgba(34, 48, 62, 0.2);
  color: $dark;
  padding: 0;
  z-index: 2;

  &__header {
    display: flex;
    width: 100%;
    padding: 20px 25px;

    &-wrapper {
      display: flex;
      width: 100%;
      align-items: center;
      padding-bottom: 10px;
    }
  }

  &__badge {
    display: flex;
    width: 18px;
    height: 18px;
    justify-content: center;
    align-items: center;
    font-size: 10px;
    line-height: 0;
    font-weight: 700;
    color: $white;
    background: $negative;
    border-radius: 50%;
    top: 3px;
    left: 30px;
    position: absolute;
  }

  &__section {
    flex-direction: row !important;
    justify-content: flex-start !important;
    align-items: center;
  }

  .q-btn {
    min-width: 31px;
    min-height: 31px;
    width: 31px;
    height: 31px;
    padding: 4px 0;

    .q-icon {
      font-size: 14px;
      margin-top: 1px;
    }
  }

  &__title {
    display: flex;
    color: $dark;
    transition: 0.3s;
    font-size: 17px;
    align-items: center;
    margin-left: 13px;

    &:hover {
      .loyalty-suite-sidebar__title {
        color: $primary;
      }
    }
  }

  &__scroll {
    flex: 1;
    max-height: 100%;
    padding: 0 25px;
  }

  &__list {
    width: 100%;
    background: $white;

    .q-item {
      display: flex;
      width: 100%;
      min-height: 42px;
      padding: 8px 16px;
      border-radius: 5px;
      margin-bottom: 0;

      &__section {
        &--avatar {
          min-width: 35px;
        }

        &--side {
          padding-right: 10px;

          .q-icon {
            color: $dark;
            transition: 0.3s;
          }
        }
      }

      .q-focus-helper {
        background: transparent;
      }

      &.router-link-exact-active {
        background: rgba(105, 108, 255, .16);
        color: $primary;

        .q-icon {
          color: $primary;
        }

        &:hover {
          color: $primary;
          background: rgba(105, 108, 255, .16) !important;

          .q-focus-helper {
            background: $white-95 !important;
          }
        }
      }

      &:hover {
        color: $primary;

        .q-focus-helper {
          background: $white-95 !important;
        }

        .q-icon {
          color: $primary !important;
        }
      }
    }
  }

  &__item {
    display: flex;
    align-items: center;
    position: relative;
    width: 100%;
    min-height: 42px;
    padding: 8px 16px;
    border-radius: 5px;
    margin-bottom: 7px;

    &.loyalty-suite-sidebar__sub-item {
      padding-left: 20px;
    }

    &__section {
      &--avatar {
        min-width: 35px;
      }

      &--side {
        padding-right: 10px;

        .q-icon {
          color: $dark;
          transition: 0.3s;
        }
      }
    }

    .q-focus-helper {
      background: transparent;
    }

    &.router-link-exact-active {
      background: rgba(105, 108, 255, .16);
      color: $primary;

      .q-icon {
        color: $primary;
      }

      &:hover {
        color: $primary;
        background: rgba(105, 108, 255, .16);

        .q-focus-helper {
          background: $white-95;
        }
      }
    }

    &:hover {
      color: $primary;

      .q-focus-helper {
        background: $white-95;
      }

      .q-icon {
        color: $primary;
      }
    }
  }

  .q-scrollarea__content {
    width: 100%;
  }
}
</style>
