/**
 * Generate TypeScript types from compiled smart contracts
 * Run: pnpm exec ts-node scripts/generate-types.ts
 */

import { execSync } from 'child_process'
import * as fs from 'fs'
import * as path from 'path'

async function generateTypes() {
  console.log('Generating TypeScript types from smart contracts...')

  try {
    // Run typechain to generate types
    execSync('npx typechain --target ethers-v6 --out-dir ../api/src/types/contracts artifacts/contracts/**/*.json', {
      cwd: path.join(__dirname, '..'),
      stdio: 'inherit',
    })

    console.log('✓ TypeScript types generated successfully')
    console.log('Types are available in: api/src/types/contracts/')
    
    // Create index file for easy imports
    const typesDir = path.join(__dirname, '../../api/src/types/contracts')
    if (fs.existsSync(typesDir)) {
      const indexContent = `// Auto-generated contract type exports
// This file is generated by scripts/generate-types.ts

export * from './ResourceProvisioning'
export * from './IdentityManagement'
export * from './Billing'
export * from './Compliance'
`
      fs.writeFileSync(path.join(typesDir, 'index.ts'), indexContent)
      console.log('✓ Created index.ts for contract types')
    }
  } catch (error) {
    console.error('Failed to generate types:', error)
    process.exit(1)
  }
}

generateTypes()

