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

import { LambdaDefinition, LambdaEvent } from '@worktif/runtime';
import type { APIGatewayProxyEvent, APIGatewayProxyResult } from 'aws-lambda';

// Example service (you would import your actual services)
class ExampleService {
    async getData(id: string): Promise<{ id: string; data: string }> {
        return { id, data: `Example data for ${id}` };
    }
}

// Type definition for our ties object
type ExampleHandlerTies = {
    exampleService: ExampleService;
};

/**
 * Example Handler
 *
 * Demonstrates the new object-based ties pattern with full type safety.
 *
 * GET /api/example/{id}
 */
export const exampleHandler: LambdaDefinition<ExampleHandlerTies> = {
    id: 'example',
    ties: {
        exampleService: ExampleService
    },
    handler: async (event: LambdaEvent<ExampleHandlerTies>): Promise<APIGatewayProxyResult> => {
        try {
            const id = event.pathParameters?.id;

            if (!id) {
                return {
                    statusCode: 400,
                    headers: { 'Content-Type': 'application/json' },
                    body: JSON.stringify({ error: 'ID parameter is required' }),
                };
            }

            // Access exampleService directly from event.ties with full type safety
            const data = await event.ties.exampleService.getData(id);

            return {
                statusCode: 200,
                headers: { 'Content-Type': 'application/json' },
                body: JSON.stringify(data),
            };
        } catch (error) {
            return {
                statusCode: 500,
                headers: { 'Content-Type': 'application/json' },
                body: JSON.stringify({
                    error: 'Internal server error',
                    message: error instanceof Error ? error.message : 'Unknown error',
                }),
            };
        }
    },
    http: {
        method: 'GET',
        path: '/api/example/{id}',
        auth: 'none',
        cors: true,
    },
};

/**
 * Multi-Service Example Handler
 *
 * Demonstrates using multiple services with the new pattern.
 */
class ConfigService {
    getConfig(): { timeout: number; retries: number } {
        return { timeout: 5000, retries: 3 };
    }
}

type MultiServiceTies = {
    exampleService: ExampleService;
    configService: ConfigService;
};

export const multiServiceHandler: LambdaDefinition<MultiServiceTies> = {
    id: 'multi-service',
    ties: {
        exampleService: ExampleService,
        configService: ConfigService
    },
    handler: async (event: LambdaEvent<MultiServiceTies, APIGatewayProxyEvent>) => {
        try {
            // Access multiple services with full type safety
            const config = event.ties.configService.getConfig();
            const data = await event.ties.exampleService.getData('multi-service');

            return {
                statusCode: 200,
                headers: { 'Content-Type': 'application/json' },
                body: JSON.stringify({ data, config }),
            };
        } catch (error) {
            return {
                statusCode: 500,
                headers: { 'Content-Type': 'application/json' },
                body: JSON.stringify({
                    error: 'Internal server error',
                    message: error instanceof Error ? error.message : 'Unknown error',
                }),
            };
        }
    },
    http: {
        method: 'GET',
        path: '/api/multi-service',
        auth: 'none',
        cors: true,
    },
};
