"use strict";
/**
 * Express API on Azure Functions - Getting Started
 *
 * This sample shows how to create a new Express API from scratch
 * and host it on Azure Functions. Just write your Express code
 * like you normally would!
 */
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const express_1 = __importDefault(require("express"));
const functions_1 = require("@azure/functions");
const functions_extensions_express_1 = require("@azure/functions-extensions-express");
// ============================================================================
// STEP 1: Create your Express app (just like you normally would!)
// ============================================================================
const expressApp = (0, express_1.default)();
// Add middleware
expressApp.use(express_1.default.json());
// Simple request logging
expressApp.use((req, _res, next) => {
    console.log(`[${new Date().toISOString()}] ${req.method} ${req.path}`);
    next();
});
// ============================================================================
// STEP 2: Define your API routes
// ============================================================================
// Health check endpoint
expressApp.get('/health', (_req, res) => {
    res.json({ status: 'ok', timestamp: new Date().toISOString() });
});
const items = [
    { id: 1, name: 'First Item', description: 'This is the first item' },
    { id: 2, name: 'Second Item', description: 'This is the second item' },
];
let nextId = 3;
// GET all items
expressApp.get('/items', (_req, res) => {
    console.log('Fetching all items');
    res.json(items);
});
// GET single item
expressApp.get('/items/:id', (req, res) => {
    const item = items.find((i) => i.id === parseInt(req.params.id));
    if (!item) {
        res.status(404).json({ error: 'Item not found' });
        return;
    }
    res.json(item);
});
// CREATE new item
expressApp.post('/items', (req, res) => {
    const { name, description } = req.body;
    if (!name) {
        res.status(400).json({ error: 'Name is required' });
        return;
    }
    const newItem = { id: nextId++, name, description };
    items.push(newItem);
    res.status(201).json(newItem);
});
// UPDATE item
expressApp.put('/items/:id', (req, res) => {
    const item = items.find((i) => i.id === parseInt(req.params.id));
    if (!item) {
        res.status(404).json({ error: 'Item not found' });
        return;
    }
    const { name, description } = req.body;
    if (name)
        item.name = name;
    if (description !== undefined)
        item.description = description;
    res.json(item);
});
// DELETE item
expressApp.delete('/items/:id', (req, res) => {
    const index = items.findIndex((i) => i.id === parseInt(req.params.id));
    if (index === -1) {
        res.status(404).json({ error: 'Item not found' });
        return;
    }
    const deleted = items.splice(index, 1)[0];
    res.json({ message: 'Deleted', item: deleted });
});
// ============================================================================
// BONUS: Streaming endpoint - showcases sidecar's streaming capability!
// ============================================================================
expressApp.get('/stream', (_req, res) => {
    res.setHeader('Content-Type', 'text/event-stream');
    res.setHeader('Cache-Control', 'no-cache');
    res.setHeader('Connection', 'keep-alive');
    let count = 0;
    const maxEvents = 5;
    console.log('Starting SSE stream...');
    const interval = setInterval(() => {
        count++;
        const data = {
            event: count,
            timestamp: new Date().toISOString(),
            message: `Server-Sent Event #${count}`,
        };
        res.write(`data: ${JSON.stringify(data)}\n\n`);
        console.log(`Sent event ${count}`);
        if (count >= maxEvents) {
            clearInterval(interval);
            res.write(`data: ${JSON.stringify({ event: 'done', message: 'Stream ended' })}\n\n`);
            res.end();
            console.log('Stream ended');
        }
    }, 1000);
    // Handle client disconnect
    res.on('close', () => {
        clearInterval(interval);
        console.log('Client disconnected from stream');
    });
});
// Error handler
expressApp.use((err, _req, res, _next) => {
    console.error('Error:', err.message);
    res.status(500).json({ error: 'Internal Server Error' });
});
// ============================================================================
// STEP 3: Register your Express app with Azure Functions
//
// expressApp() automatically discovers ALL routes you defined above
// and creates a separate Azure Function for each one:
//
//   api-GET-health        -> route: 'health'
//   api-GET-items         -> route: 'items'
//   api-GET-items-id      -> route: 'items/{id}'
//   api-POST-items        -> route: 'items'
//   api-PUT-items-id      -> route: 'items/{id}'
//   api-DELETE-items-id   -> route: 'items/{id}'
//   api-GET-stream        -> route: 'stream'
//
// Express runs as a real HTTP server on localhost, which provides
// FULL Express compatibility including:
// - Streaming responses (SSE, file downloads)
// - All middleware works exactly as designed
// - Real socket connections
// ============================================================================
const server = (0, functions_extensions_express_1.expressApp)('endpoint', expressApp, {
    enableStreaming: true, // Enable streaming for SSE support
});
// Optional: Handle graceful shutdown
functions_1.app.hook.appTerminate(async () => {
    console.log('Shutting down Express server...');
    await server.stop();
});
// ============================================================================
// That's it! Each Express route is now a separate Azure Function.
//
// To run locally:
//   npm install
//   npm start
//
// You'll see each function registered individually:
//   api-GET-health       -> http://localhost:7071/api/health
//   api-GET-items        -> http://localhost:7071/api/items
//   api-GET-items-id     -> http://localhost:7071/api/items/:id
//   api-POST-items       -> http://localhost:7071/api/items
//   api-PUT-items-id     -> http://localhost:7071/api/items/:id
//   api-DELETE-items-id  -> http://localhost:7071/api/items/:id
//   api-GET-stream       -> http://localhost:7071/api/stream  (SSE!)
// ============================================================================
