/*
 * For any questions, contact us at: raman@worktif.com
 */

/**
 * Runtime Web Core Example Application
 *
 * This is the main entry point for the example application.
 * It demonstrates how to configure and use the Runtime Web Core layer.
 */

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

// Import Ties classes
import { UsersTies } from './ties';
//
// // Import Lambda handlers
import { createUserHandler, deleteUserHandler, getUserHandler, updateUserHandler } from './handlers/users';

import App from './App';

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

import runtimeConfig from 'runtime.config';


/**
 * Start the application
 *
 * The runtime() function automatically detects the execution context:
 * - Browser: Hydrates React app with client-side routing
 * - Lambda: Executes handlers with DI injection
 * - CDK: Synthesizes CloudFormation templates
 */
runtime({
  // React application factory
  app: ({ router }: any) => <App router={router}/>,

  // Router instance
  // @ts-ignore
  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: {
    // Service name (used for AWS resource naming)
    serviceName:
      typeof window === 'undefined'
        ? process.env.PURE_ENV_STACK_NAME || runtimeConfig.projectName
        : 'runtime-app',

    // Deployment stage
    stage:
      typeof window === 'undefined'
        ? (process.env.PURE_ENV_STAGE as 'dev' | 'staging' | 'prod')
        : 'dev',
  },

  // Microservice registry
  register: {
    // Users microservice
    users: {
      ties: [UsersTies],
      lambdas: [
        createUserHandler, 
        getUserHandler, 
        updateUserHandler, 
        deleteUserHandler,
      ],
    },
  },
});
