import { mkdir, writeFile } from 'node:fs/promises'
import { dirname } from 'node:path'

// Synthetic, deterministic portal bulk responses. No portal requests or installed-mod reads.
const categories = ['content', 'overhaul', 'tweaks', 'utilities', 'scenarios', 'mod-packs', 'localizations', 'internal', null]
const tags = ['planets', 'transportation', 'logistics', 'trains', 'combat', 'armor', 'character', 'enemies', 'environment', 'mining', 'fluids', 'logistic-network', 'circuit-network', 'manufacturing', 'power', 'storage', 'blueprints', 'cheats']
const installed = ['RateCalculator', 'bottleneck', 'disco-science', 'flib', 'far-reach', 'helmod']
const subjects = ['Orbital Logistics', 'Copper Foundry', 'Rail Planner', 'Factory Helper', 'Planetary Mining', 'Power Networks', 'Blueprint Workshop', 'Fluid Storage', 'Combat Robotics', 'Circuit Tools', 'Quality Transport', 'Alien Landscapes']

function catalogue(count: number) {
  return {
    results: Array.from({ length: count }, (_, i) => {
      const name = installed[i] ?? `fixture-${String(i).padStart(5, '0')}`
      return {
        name,
        title: i < installed.length ? ['Rate Calculator', 'Bottleneck', 'Disco Science', 'Factorio Library', 'Far Reach', 'Helmod'][i] : `${['', '! ', '[Beta] ', ''][i % 4]}${subjects[i % subjects.length]} ${i}`,
        owner: `FactoryAuthor${i % 143}`,
        summary: `Improve your factory with ${subjects[i % subjects.length]?.toLowerCase()}. Includes configurable machines, helpful tools and clear recipes for your production network. A deterministic offline specimen for browsing, searching and filtering mods.`,
        downloads_count: (i * 7919 + 40000) % 2000000,
        category: categories[i % categories.length],
        ...(i % 5 ? { score: (i * 73 % 10000) / 100 } : {}),
        ...(i % 3 ? { tags: [tags[i % tags.length], tags[(i + 7) % tags.length]] } : {}),
        ...(i % 4 ? {} : { thumbnail: `/assets/fixture-${i}.thumb.png` }),
        latest_release: {
          version: i < installed.length ? '99.0.0' : `1.${i % 40}.${i % 13}`,
          released_at: new Date(Date.UTC(2024, 0, 1) + (i * 97 % 10000) * 3600000).toISOString(),
          info_json: {
            factorio_version: i < installed.length || i % 5 < 3 ? '2.0' : i % 5 === 3 ? '1.1' : '2.1',
            ...(i % 4 ? {} : { dependencies: ['base >= 2.0', 'space-age >= 2.0'] }),
          },
          download_url: `/download/${name}/fixture`,
          file_name: `${name}_fixture.zip`,
          sha1: '0'.repeat(40),
        },
      }
    }),
  }
}

for (const [path, count] of [
  ['out/fixtures/catalogue-24k.json', 24000],
  ['crates/app/tests/fixtures/catalogue-small.json', 30],
] as const) {
  await mkdir(dirname(path), { recursive: true })
  const data = JSON.stringify(catalogue(count), null, count < 100 ? 2 : undefined) + '\n'
  await writeFile(path, data)
  console.log(`${path}: ${count} mods, ${Buffer.byteLength(data)} bytes`)
}

const current = catalogue(30)
for (const mod of current.results) mod.latest_release.version = '0.0.0'
await writeFile('out/fixtures/catalogue-current.json', JSON.stringify(current) + '\n')
console.log('out/fixtures/catalogue-current.json: 30 mods with no newer releases')
