import { fixtures, type Fixtures } from '../mock-magento.js'

/**
 * Synthetic catalog shaped like a mid-size fashion/homeware store:
 * 1/6 configurables with 5 variants each, the rest standalone simples; ~25 attributes each.
 */
export function largeCatalog(total: number): Fixtures {
  const base = fixtures()
  const products: Fixtures['products'] = []
  const sourceItems: Fixtures['sourceItems'] = []
  const ca = (o: Record<string, unknown>) => Object.entries(o).map(([attribute_code, value]) => ({ attribute_code, value }))
  const ts = (i: number) => new Date(Date.UTC(2026, 0, 1) + i * 60_000).toISOString().slice(0, 19).replace('T', ' ')
  const words = ['Linen', 'Cotton', 'Oak', 'Enamel', 'Wool', 'Ceramic', 'Classic', 'Relaxed', 'Slim', 'Vintage', 'Shirt', 'Lamp', 'Mug', 'Throw', 'Chair', 'Dress', 'Bowl', 'Rug']
  const name = (i: number) => `${words[i % 9]} ${words[9 + (i % 9)]} ${i}`
  const attrs = (i: number) => {
    const extra: Record<string, unknown> = {}
    for (let k = 0; k < 18; k++) extra[`spec_${k}`] = `value ${k}-${i % 50}`
    return extra
  }

  let id = 1000
  const configurables = Math.floor(total / 6 / 1) // each config + 5 children = 6 products
  const categories = 200
  for (let c = 0; c < configurables && products.length + 6 <= total; c++) {
    const parentId = id++
    const children: number[] = []
    for (let v = 0; v < 5; v++) {
      const cid = id++
      children.push(cid)
      const sku = `CFG${c}-${v}`
      products.push({
        id: cid, sku, name: `${name(c)} / ${v}`, attribute_set_id: 4, price: 20 + (c % 80), status: 1, visibility: 1, type_id: 'simple',
        created_at: ts(cid), updated_at: ts(cid), extension_attributes: { website_ids: [1] },
        custom_attributes: ca({ brand: 'Acme', color: String(5 + (v % 2)), url_key: sku.toLowerCase(), ...attrs(cid) }),
      })
      sourceItems.push({ sku, source_code: 'default', quantity: (cid * 7) % 30, status: (cid * 7) % 30 ? 1 : 0 })
    }
    products.push({
      id: parentId, sku: `CFG${c}`, name: name(c), attribute_set_id: 4, price: 0, status: 1, visibility: 4, type_id: 'configurable',
      created_at: ts(parentId), updated_at: ts(parentId),
      extension_attributes: { website_ids: [1], configurable_product_links: children, category_links: [{ category_id: String(3 + (c % categories)), position: c }] },
      media_gallery_entries: [{ id: parentId, media_type: 'image', label: 'Main', position: 1, disabled: false, types: ['image', 'small_image', 'thumbnail'], file: `/c/f/cfg${c}.jpg` }],
      custom_attributes: ca({ brand: 'Acme', description: `<p>${name(c)} — ${'lorem ipsum '.repeat(40)}</p>`, url_key: `cfg${c}`, ...attrs(parentId) }),
    })
  }
  while (products.length < total) {
    const pid = id++
    const sku = `SKU${pid}`
    products.push({
      id: pid, sku, name: name(pid), attribute_set_id: 4, price: 5 + (pid % 200), status: pid % 40 ? 1 : 2, visibility: 4, type_id: 'simple',
      created_at: ts(pid), updated_at: ts(pid), extension_attributes: { website_ids: [1], category_links: pid % 25 ? [{ category_id: String(3 + (pid % categories)), position: 0 }] : [] },
      media_gallery_entries: pid % 10 ? [{ id: pid, media_type: 'image', label: null as unknown as string, position: 1, disabled: false, types: ['image'], file: `/s/k/${sku}.jpg` }] : [],
      custom_attributes: ca({ brand: pid % 30 ? 'Acme' : null, description: pid % 15 ? `<p>${'lorem ipsum '.repeat(40)}</p>` : '', url_key: sku.toLowerCase(), ...attrs(pid) }),
    })
    sourceItems.push({ sku, source_code: 'default', quantity: pid % 17, status: pid % 17 ? 1 : 0 }, { sku, source_code: 'leeds', quantity: pid % 5, status: 1 })
  }

  const cats = [...base.categories.slice(0, 2)]
  for (let i = 0; i < categories; i++) cats.push({ id: 3 + i, parent_id: 2, name: `Category ${i}`, is_active: true, position: i, level: 2, path: `1/2/${3 + i}`, include_in_menu: true })

  return { ...base, products, sourceItems, categories: cats, storeOverrides: {}, salable: {} }
}
