export type Version = {
  id: string
  label: string
}

export type Product = {
  id: string
  productId: number
  title: string
  price: number
  minPrice?: number
  desc?: string
  tag?: string
  card?: string
  originalPrice?: number
  duration?: string
  version?: string
  features?: string[]
  isTest?: boolean
}

export type Brand = {
  id: string
  name: string
  versions?: Version[]
  products: Product[]
}

export type Tab = {
  id: string
  label: string
  brands: Brand[]
}

export type TabSource = {
  id: string
  label: string
  data: ProductItem[]
}

export type ProductItem = {
  Product: {
    Id: number
    Name: string
    Image: string
    Sort: number
    Content: string
    Profile: string
    TenantId: number
    OnLine: number
    ContentLine: string[]
  }
  Packages: PackageItem[]
}

export type PackageItem = {
  Id: number
  TenantId: number
  ProductId: number
  PackageType: number
  Name: string
  Title: string
  Image: string
  Status: number
  Profile: string
  Price: number
  LinePrice: number
  DayPrice: number
  MinPrice: number
  DayCount: number
  OriginKey: string
  OriginName: string
  IsTest: number
  DeleteTag: number
}

function extractTier(name: string): string {
  const match = name.match(/\((.+?)\)$/)
  return match ? match[1] : "默认"
}

function transformToBrands(data: ProductItem[]): Brand[] {
  const activeProducts = data.filter(p => p.Product.OnLine === 1)

  activeProducts.sort((a, b) => a.Product.Sort - b.Product.Sort)

  return activeProducts.map(item => {
    const activePackages = item.Packages.filter(pkg => pkg.Status === 1)

    const tierMap = new Map<string, PackageItem[]>()
    for (const pkg of activePackages) {
      const tier = extractTier(pkg.Name)
      if (!tierMap.has(tier)) tierMap.set(tier, [])
      tierMap.get(tier)?.push(pkg)
    }

    const tiers = Array.from(tierMap.entries())

    const versions: Version[] | undefined =
      tiers.length > 1
        ? tiers.map(([tier]) => ({ id: tier, label: tier }))
        : undefined

    const products: Product[] = []
    for (const [tier, packages] of tiers) {
      for (const pkg of packages) {
        products.push({
          id: pkg.Id.toString(),
          productId: item.Product.Id,
          title: pkg.Name,
          price: pkg.Price,
          minPrice: pkg.MinPrice,
          originalPrice: pkg.LinePrice,
          duration: pkg.Profile,
          card: pkg.OriginName,
          version: tier,
          isTest: pkg.IsTest === 1,
        })
      }
    }

    return {
      id: item.Product.Name,
      name: item.Product.Name,
      versions,
      products,
    }
  })
}

export function transformToTabs(sources: TabSource[]): Tab[] {
  const tabs: Tab[] = []
  for (const source of sources) {
    const brands = transformToBrands(source.data)
    if (brands.length === 0) continue
    tabs.push({ id: source.id, label: source.label, brands })
  }

  const firstTab = tabs[0]
  if (firstTab) {
    firstTab.brands.push({
      id: "info",
      name: "购买须知",
      products: [],
    })
  }

  return tabs
}
