<template>
  <div class="page-container">
    <div v-if="pending" class="flex justify-center items-center h-64">
      <el-skeleton :rows="6" animated />
    </div>
    <div v-else>
      <support-tabs model-value="faq" />
      <div class="page-header">
        <h1 class="page-title">{{ $t('navigation.faq') }}</h1>
        <app-breadcrumb class="breadcrumb" :items="breadcrumbItems" />
      </div>

      <div class="page-content">
        <product-filter
          v-model="filters"
          :product-type-options="productTypeOptions"
          :product-options="productOptions"
        />

        <question-list :questions="filteredQuestions" />
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
  const localePath = useLocalePath();

  const filters = reactive({
    selectedType: null as number | null,
    selectedProduct: null as number | null,
    keyword: '',
  });

  const breadcrumbItems = [
    { label: $t('navigation.home'), to: localePath('/') },
    { label: $t('navigation.support'), to: localePath('/support') },
    { label: $t('navigation.faq') },
  ];

  const { data, pending, error } = await useQuestionList();

  const questions = computed(
    () => data.value.map((item) => toQuestionListView(item)) ?? null
  );

  const productTypeOptions = computed(() => {
    const types: QuestionListProductType[] = [];
    questions.value.forEach((q: QuestionListView) => {
      q.products.forEach((product: QuestionListProduct) => {
        const productType = product.type;
        if (!types.some((p) => p.id === productType.id)) {
          types.push(productType);
        }
      });
    });
    return types;
  });

  const productOptions = computed(() => {
    if (!filters.selectedType) return [];
    const products: QuestionListProduct[] = [];
    questions.value.forEach((q: QuestionListView) => {
      q.products.forEach((product: QuestionListProduct) => {
        if (
          product.type.id === filters.selectedType &&
          !products.some((p) => p.id === product.id)
        ) {
          products.push(product);
        }
      });
    });
    return products;
  });

  const filteredQuestions = computed(() => {
    const fuzzyMatchedQuestions = fuzzyMatch(questions.value, {
      keyword: filters.keyword,
      keys: ['title', 'content'],
      threshold: 0.2,
    });
    return fuzzyMatchedQuestions.filter((question: QuestionListView) => {
      const matchProduct = filters.selectedProduct
        ? question.products?.some(
            (product: QuestionListProduct) =>
              product.id === filters.selectedProduct
          )
        : filters.selectedType
          ? question.products?.some(
              (product: QuestionListProduct) =>
                product.type.id === filters.selectedType
            )
          : true;

      return matchProduct;
    });
  });

  watch(
    () => filters.selectedType,
    () => {
      filters.selectedProduct = null;
    }
  );

  watch(error, (value) => {
    if (value) {
      logger.error('数据获取失败: ', value);
    }
  });

  const pageTitle = $t('page-title.faq');
  usePageSeo({
    title: pageTitle,
  });
</script>

<style scoped>
  .page-container {
    max-width: 1200px;
    margin: 0 auto;
  }

  .page-header {
    display: flex;
    padding: 2rem 2rem 0rem;
  }

  .page-title {
    font-size: 2rem;
    font-weight: bold;
    color: var(--el-color-primary);
    margin-bottom: 1rem;
  }

  .breadcrumb {
    margin-left: auto;
  }

  .page-content {
    padding: 1rem 2rem 2rem;
  }
</style>
