import { logger } from '@bamboocss/logger'
import type { ArtifactId, SpecFile } from '@bamboocss/types'
import type { BambooContext } from './create-context'

interface SpecOptions {
  outdir?: string
}

export async function spec(ctx: BambooContext, options: SpecOptions) {
  const { outdir } = options

  const specs = ctx.getSpec()

  // Use ctx.paths.specs (includes configured outdir) or override with provided outdir
  const specDir = outdir ? [ctx.config.cwd, outdir] : ctx.paths.specs
  const specDirPath = ctx.runtime.path.join(...specDir)

  const writeSpec = async (spec: SpecFile) => {
    await ctx.output.write({
      id: `spec-${spec.type}` as ArtifactId,
      dir: specDir,
      files: [{ file: `${spec.type}.json`, code: JSON.stringify(spec, null, 2) }],
    })
  }

  await Promise.all(specs.map(writeSpec))

  const specTypes = specs.map((s) => s.type)
  logger.info('spec', `Generated ${specTypes.length} spec file(s) → ${specDirPath}`)
  return specs
}
