#!/usr/bin/env python3
"""
Create minimal but valid WASM module files
This is for development/testing. Production should use Rust + wasm-pack.
"""

import json
import os

# Create WASM binary (minimal valid WebAssembly module)
# Magic number + version + empty sections
wasm_hex = (
    "0061736d"  # Magic: \0asm
    "01000000"  # Version: 1
    # Type section (empty)
    "01020100"
    # Function section (empty)
    "03010000"
    # Export section (empty)
    "07000000"
    # Code section (empty)
    "0a000000"
)

wasm_binary = bytes.fromhex(wasm_hex)

# Write WASM file
with open('fikoyt_wasm_bg.wasm', 'wb') as f:
    f.write(wasm_binary)
print('✅ Created fikoyt_wasm_bg.wasm')

# Write JS glue code
js_code = '''let wasm;

export function __wbg_set_wasm(val) {
    wasm = val;
}

export function parseStepTopology(content) {
    return JSON.stringify({
        faces: {},
        edges: {},
        radiusGroups: {}
    });
}

export function detectEdgesFromMesh(positions, indices, normals) {
    return new Uint32Array(0);
}

export function resolveColorChain(entityId, content) {
    return new Float32Array([0.5, 0.5, 0.5]);
}

export function clearColorCache() {
    // Placeholder
}
'''

with open('fikoyt_wasm_bg.js', 'w') as f:
    f.write(js_code)
print('✅ Created fikoyt_wasm_bg.js')

# Write main export
main_js = '''import * as wasm from './fikoyt_wasm_bg.js';

export const parseStepTopology = wasm.parseStepTopology;
export const detectEdgesFromMesh = wasm.detectEdgesFromMesh;
export const resolveColorChain = wasm.resolveColorChain;
export const clearColorCache = wasm.clearColorCache;
'''

with open('fikoyt_wasm.js', 'w') as f:
    f.write(main_js)
print('✅ Created fikoyt_wasm.js')

# Write TypeScript definitions
dts = '''export type StepTopologyResult = {
    faces: Record<string, any>;
    edges: Record<string, any>;
    radiusGroups: Record<string, string[]>;
};

export function parseStepTopology(content: string): string;
export function detectEdgesFromMesh(
    positions: Float32Array,
    indices: Uint32Array,
    normals: Float32Array
): Uint32Array;
export function resolveColorChain(entityId: string, content: string): Float32Array;
export function clearColorCache(): void;
'''

with open('fikoyt_wasm.d.ts', 'w') as f:
    f.write(dts)
print('✅ Created fikoyt_wasm.d.ts')

# Write package.json
pkg = {
    'name': 'fikoyt_wasm',
    'version': '0.1.0',
    'description': 'FikoYT WASM Module - STP Parser & Geometry Optimization',
    'main': 'fikoyt_wasm.js',
    'types': 'fikoyt_wasm.d.ts',
    'files': [
        'fikoyt_wasm_bg.wasm',
        'fikoyt_wasm_bg.js',
        'fikoyt_wasm.js',
        'fikoyt_wasm.d.ts',
        'package.json'
    ]
}

with open('package.json', 'w') as f:
    json.dump(pkg, f, indent=2)
print('✅ Created package.json')

# Copy to public/assets
import shutil
dest_dir = '../public/assets/fikoyt-wasm'
os.makedirs(dest_dir, exist_ok=True)

for file in ['fikoyt_wasm_bg.wasm', 'fikoyt_wasm_bg.js', 'fikoyt_wasm.js', 'fikoyt_wasm.d.ts', 'package.json']:
    shutil.copy(file, os.path.join(dest_dir, file))
    print(f'✅ Copied {file}')

print('\n✅ WASM build complete!')
print(f'📊 Output: {dest_dir}')
print('\n⚠️  This is a placeholder WASM module for development.')
print('For production, build with Rust:\n')
print('  cd fikoyt-wasm')
print('  rustup target add wasm32-unknown-unknown')
print('  cargo install wasm-pack')
print('  wasm-pack build --target web --out-dir pkg --release\n')
