// PUBLIC DOMAIN - NO LICENSE, NO WARRANTY
// Copyright 2025 TimeHexOn & foxhop & russell@unturf
// https://www.permacomputer.com

import * as https from 'https';
import * as fs from 'fs';

console.log('=== TypeScript AI API Examples (Dynamic Model Discovery) ===\n');

interface ChatMessage {
    role: string;
    content: string;
}

interface ChatRequest {
    model: string;
    messages: ChatMessage[];
    max_tokens: number;
}

interface TTSRequest {
    model: string;
    voice: string;
    input: string;
}

interface ModelInfo {
    id: string;
    endpoint: string;
    max_tokens: number;
}

interface ModelsResponse {
    data: Array<{ id: string; max_model_len?: number }>;
}

function getJSON<T>(url: string): Promise<T> {
    return new Promise((resolve, reject) => {
        const urlObj = new URL(url);
        const options: https.RequestOptions = {
            hostname: urlObj.hostname,
            port: 443,
            path: urlObj.pathname,
            method: 'GET',
            timeout: 10000
        };

        const req = https.request(options, (res) => {
            let body = '';
            res.on('data', (chunk) => body += chunk);
            res.on('end', () => {
                if (res.statusCode === 200) {
                    resolve(JSON.parse(body));
                } else {
                    reject(new Error(`HTTP ${res.statusCode}`));
                }
            });
        });

        req.on('error', reject);
        req.on('timeout', () => {
            req.destroy();
            reject(new Error('Request timeout'));
        });

        req.end();
    });
}

function postJSON<T>(url: string, data: any): Promise<T> {
    return new Promise((resolve, reject) => {
        const jsonData = JSON.stringify(data);
        const urlObj = new URL(url);

        const options: https.RequestOptions = {
            hostname: urlObj.hostname,
            port: 443,
            path: urlObj.pathname,
            method: 'POST',
            headers: {
                'Content-Type': 'application/json',
                'Content-Length': jsonData.length
            },
            timeout: 30000
        };

        const req = https.request(options, (res) => {
            let body = '';
            res.on('data', (chunk) => body += chunk);
            res.on('end', () => {
                if (res.headers['content-type']?.includes('application/json')) {
                    resolve(JSON.parse(body));
                } else {
                    resolve(body as any);
                }
            });
        });

        req.on('error', reject);
        req.on('timeout', () => {
            req.destroy();
            reject(new Error('Request timeout'));
        });

        req.write(jsonData);
        req.end();
    });
}

function postJSONBinary(url: string, data: any): Promise<Buffer> {
    return new Promise((resolve, reject) => {
        const jsonData = JSON.stringify(data);
        const urlObj = new URL(url);

        const options: https.RequestOptions = {
            hostname: urlObj.hostname,
            port: 443,
            path: urlObj.pathname,
            method: 'POST',
            headers: {
                'Content-Type': 'application/json',
                'Content-Length': jsonData.length
            },
            timeout: 30000
        };

        const req = https.request(options, (res) => {
            const chunks: Buffer[] = [];
            res.on('data', (chunk) => chunks.push(chunk));
            res.on('end', () => resolve(Buffer.concat(chunks)));
        });

        req.on('error', reject);
        req.on('timeout', () => {
            req.destroy();
            reject(new Error('Request timeout'));
        });

        req.write(jsonData);
        req.end();
    });
}

async function discoverModels(): Promise<{ models: ModelInfo[]; ttsEndpoints: string[] }> {
    console.log('Discovering models from environment variables...');

    const models: ModelInfo[] = [];
    const ttsEndpoints: string[] = [];

    // Discover chat/code models from MODEL_ENDPOINT_1..9999
    for (let i = 1; i < 10000; i++) {
        const endpoint = process.env[`MODEL_ENDPOINT_${i}`];
        if (!endpoint) break;

        console.log(`Discovering from: ${endpoint}`);

        try {
            const response = await getJSON<ModelsResponse>(`${endpoint}/models`);
            for (const model of response.data || []) {
                models.push({
                    id: model.id,
                    endpoint: endpoint,
                    max_tokens: model.max_model_len || 8192
                });
            }
        } catch (error) {
            console.log(`  Error: ${(error as Error).message}`);
        }
    }

    // Discover TTS endpoints from TTS_ENDPOINT_1..9999
    for (let i = 1; i < 10000; i++) {
        const endpoint = process.env[`TTS_ENDPOINT_${i}`];
        if (!endpoint) break;

        console.log(`Discovering TTS from: ${endpoint}`);
        ttsEndpoints.push(endpoint);
    }

    console.log('');
    console.log(`Discovered ${models.length} model(s) and ${ttsEndpoints.length} TTS endpoint(s)`);
    console.log('');

    return { models, ttsEndpoints };
}

async function chatExample(model: ModelInfo, systemMsg: string, userMsg: string, maxTokens: number = 100): Promise<void> {
    console.log('\n=== Non-Streaming Chat ===');
    console.log(`Model: ${model.id}`);
    console.log(`Endpoint: ${model.endpoint}`);
    console.log('');

    const request: ChatRequest = {
        model: model.id,
        messages: [
            { role: 'system', content: systemMsg },
            { role: 'user', content: userMsg }
        ],
        max_tokens: maxTokens
    };

    try {
        const response: any = await postJSON(`${model.endpoint}/chat/completions`, request);
        console.log('Response:');
        console.log(response.choices[0].message.content);
    } catch (error) {
        console.log('Error:', (error as Error).message);
    }
}

async function chatStreamExample(model: ModelInfo, systemMsg: string, userMsg: string, maxTokens: number = 500): Promise<void> {
    console.log('\n=== Streaming Chat ===');
    console.log(`Model: ${model.id}`);
    console.log(`Endpoint: ${model.endpoint}`);
    console.log('');

    const request = {
        model: model.id,
        messages: [
            { role: 'system', content: systemMsg },
            { role: 'user', content: userMsg }
        ],
        max_tokens: maxTokens,
        stream: true
    };

    return new Promise((resolve, reject) => {
        const jsonData = JSON.stringify(request);
        const urlObj = new URL(`${model.endpoint}/chat/completions`);

        const options: https.RequestOptions = {
            hostname: urlObj.hostname,
            port: 443,
            path: urlObj.pathname,
            method: 'POST',
            headers: {
                'Content-Type': 'application/json',
                'Content-Length': jsonData.length
            },
            timeout: 60000
        };

        const req = https.request(options, (res) => {
            process.stdout.write('Response: ');

            let buffer = '';

            res.on('data', (chunk) => {
                buffer += chunk.toString();

                const lines = buffer.split('\n');
                buffer = lines.pop() || '';

                for (const line of lines) {
                    if (line.startsWith('data: ')) {
                        const data = line.slice(6).trim();
                        if (data === '[DONE]') {
                            process.stdout.write('\n');
                            resolve();
                            return;
                        }

                        try {
                            const parsed = JSON.parse(data);
                            if (parsed.choices?.[0]?.delta?.content) {
                                process.stdout.write(parsed.choices[0].delta.content);
                            }
                        } catch {
                            // Ignore parse errors
                        }
                    }
                }
            });

            res.on('end', () => {
                process.stdout.write('\n');
                resolve();
            });
        });

        req.on('error', reject);
        req.on('timeout', () => {
            req.destroy();
            reject(new Error('Request timeout'));
        });

        req.write(jsonData);
        req.end();
    });
}

async function ttsExample(endpoint: string): Promise<void> {
    console.log('');
    console.log('---');
    console.log('');
    console.log('=== TTS Speech Generation Example ===');
    console.log(`Endpoint: ${endpoint}`);
    console.log('');

    const request: TTSRequest = {
        model: 'tts-1',
        voice: 'alloy',
        input: 'Hello from TypeScript! This is a text to speech example with dynamic model discovery.'
    };

    try {
        const audioData = await postJSONBinary(`${endpoint}/audio/speech`, request);
        fs.writeFileSync('speech.mp3', audioData);
        console.log(`✓ Speech file created: speech.mp3 (${audioData.length} bytes)`);
    } catch (error) {
        console.log('✗ Error:', (error as Error).message);
    }
}

async function main(): Promise<void> {
    const { models, ttsEndpoints } = await discoverModels();

    if (models.length === 0) {
        console.log('ERROR: No models discovered. Set environment variables:');
        console.log('  MODEL_ENDPOINT_1, MODEL_ENDPOINT_2, etc.');
        process.exit(1);
    }

    await chatExample(models[0], 'You are a helpful AI assistant.', 'Explain quantum computing in one sentence.');

    const modelIdx = models.length > 1 ? 1 : 0;
    await chatStreamExample(models[modelIdx], 'You are a coding assistant.', 'Write a TypeScript function to check if a number is prime', 200);

    if (ttsEndpoints.length > 0) {
        await ttsExample(ttsEndpoints[0]);
    } else {
        console.log('\n=== TTS Speech Generation Example ===');
        console.log('ERROR: No TTS endpoints available. Set TTS_ENDPOINT_1');
    }

    console.log('');
    console.log('=== Examples Complete ===');
}

main().catch(console.error);
