/*
 * Runtime Web – Unified Serverless Framework for Full-Stack TypeScript Applications
 * Documentation: https://runtimeweb.com/docs/getting-started
 *
 * @template: ai
 */

import * as React from 'react';
import { ErrorBoundary, runtime, runtimeRouter } from '@worktif/runtime';

import { AiTies } from './ties/ai.ties';
import { chatHandler } from './handlers/chat.handlers';

import runtimeConfig from 'runtime.config';

import App from './App';
import { routes } from './routes';

// Initialize Runtime Web application with AI chat microservice support
runtime({
    app: ({ router }) => <App router={router} />,
    router: runtimeRouter({
        router: routes,
        defaults: {
            HydrateFallback: () => <div>Loading...</div>,
            ErrorBoundary: ({ error }: any) =>
                <ErrorBoundary>{error?.message || 'An unexpected error occurred. Please try again later.'}</ErrorBoundary>,
        },
    }),
    config: {
        serviceName:
            typeof window === 'undefined'
                ? process.env.RUNTIME_ENV_STACK_NAME || runtimeConfig.projectName
                : 'ai-chat-app',
        stage:
            typeof window === 'undefined'
                ? (process.env.RUNTIME_ENV_STAGE as 'dev' | 'staging' | 'prod')
                : 'dev',
    },

    // Microservice registry with service definitions
    // Each key is a domain name, value contains ties and lambdas
    register: {
      // ================================================================
      // AI Chat Microservice
      // ================================================================
      // The chat microservice handles AI-powered conversations using:
      // - AWS Bedrock for AI model invocation
      // - DynamoDB for session memory persistence
      // - OpenSearch for vector similarity search (RAG pattern)
      ai: {
        // Ties – shared domain models / dependencies used by this module
        // These are injected into all related Lambdas
        ties: [AiTies],

        // Lambda handlers for the "ai" domain
        // Each handler is wired into the API according to Runtime Web conventions
        lambdas: [
          chatHandler,
        ],
      },
    },
});
