import axios from 'axios';
import fs from 'fs';
import path from 'path';

// API配置
const API_BASE_URL = process.env.NODE_ENV === 'development' 
  ? 'https://api.dev.mingyanglg.com/http/router'
  : 'https://api.mingyanglg.com/http/router';

// 创建API客户端
const apiClient = axios.create({
  baseURL: API_BASE_URL,
  timeout: 10000,
  headers: {
    'Content-Type': 'application/json'
  }
});

// 获取轮播图数据
async function getCarouselData(locale) {
  try {
    const response = await apiClient.post('/website/get/homePageCarousel_list', {
      locale: locale
    });
    return response.data;
  } catch (error) {
    console.warn(`获取轮播图数据失败 (${locale}):`, error.message);
    return { rows: [] };
  }
}

// 获取产品分类数据
async function getProductCategoryData(locale) {
  try {
    const response = await apiClient.post('/website/get/product_category_list', {
      locale: locale
    });
    return response.data;
  } catch (error) {
    console.warn(`获取产品分类数据失败 (${locale}):`, error.message);
    return { rows: [] };
  }
}

// 获取产品列表数据
async function getProductListData(locale) {
  try {
    const response = await apiClient.post('/website/get/product_list', {
      locale: locale,
      page: 1,
      limit: 50
    });
    return response.data;
  } catch (error) {
    console.warn(`获取产品列表数据失败 (${locale}):`, error.message);
    return { rows: [] };
  }
}

// 获取新闻列表数据
async function getNewsListData(locale) {
  try {
    const response = await apiClient.post('/website/get/news_list', {
      locale: locale,
      page: 1,
      limit: 20
    });
    return response.data;
  } catch (error) {
    console.warn(`获取新闻列表数据失败 (${locale}):`, error.message);
    return { rows: [] };
  }
}

// 获取证书数据
async function getCertificateData(locale) {
  try {
    const response = await apiClient.post('/website/get/certificate_list', {
      locale: locale
    });
    return response.data;
  } catch (error) {
    console.warn(`获取证书数据失败 (${locale}):`, error.message);
    return { rows: [] };
  }
}

// 获取APP下载数据
async function getDownloadData(locale) {
  try {
    const response = await apiClient.post('/website/get/appInstallPackage', {
      locale: locale
    });
    return response.data;
  } catch (error) {
    console.warn(`获取APP下载数据失败 (${locale}):`, error.message);
    return { rows: [] };
  }
}

// 主函数
async function generateStaticData() {
  
  const locales = ['en', 'cn'];
  const staticData = {};
  
  for (const locale of locales) {    
    const [carousel, categories, products, news, certificates, downloads] = await Promise.allSettled([
      getCarouselData(locale),
      getProductCategoryData(locale),
      getProductListData(locale),
      getNewsListData(locale),
      getCertificateData(locale),
      getDownloadData(locale)
    ]);
    
    staticData[locale] = {
      carousel: carousel.status === 'fulfilled' ? carousel.value : { rows: [] },
      categories: categories.status === 'fulfilled' ? categories.value : { rows: [] },
      products: products.status === 'fulfilled' ? products.value : { rows: [] },
      news: news.status === 'fulfilled' ? news.value : { rows: [] },
      certificates: certificates.status === 'fulfilled' ? certificates.value : { rows: [] },
      downloads: downloads.status === 'fulfilled' ? downloads.value : { rows: [] }
    };
  }
  
  // 确保目录存在
  const outputDir = path.join(process.cwd(), 'src', 'static-data');
  if (!fs.existsSync(outputDir)) {
    fs.mkdirSync(outputDir, { recursive: true });
  }
  
  // 写入静态数据文件
  const outputPath = path.join(outputDir, 'api-data.json');
  fs.writeFileSync(outputPath, JSON.stringify(staticData, null, 2));
}

// 执行脚本
generateStaticData().catch(console.error);
