import * as apigateway from 'aws-cdk-lib/aws-apigateway';
import { Construct } from 'constructs';
import * as certManager from 'aws-cdk-lib/aws-certificatemanager';
import * as logs from 'aws-cdk-lib/aws-logs';
import { ApplicationLoadBalancer } from 'aws-cdk-lib/aws-elasticloadbalancingv2/lib/alb/application-load-balancer';
import { aws_apigatewayv2, aws_apigatewayv2_integrations, Duration } from 'aws-cdk-lib';
import { VpcLink } from 'aws-cdk-lib/aws-apigatewayv2';
import { ApplicationListener } from 'aws-cdk-lib/aws-elasticloadbalancingv2';

export const createFlowerApiGateway = (scope: Construct, certificate: certManager.ICertificate, accountStage: string, vpcLink: VpcLink, httpsListener: ApplicationListener) => {

    const domainName = new aws_apigatewayv2.DomainName(scope, `ecs${accountStage.charAt(0).toUpperCase() + accountStage.slice(1)}FlowerDomain`, {
        domainName: `celery-flower.${accountStage}.suno.com`,
        certificate
    });

    // Create a CloudWatch Log Group
    const logGroup = new logs.LogGroup(scope, 'CeleryFlowerApiGatewayAccessLogs', {
        retention: logs.RetentionDays.ONE_WEEK,
    });

    const celeryFlowerHttpIntegration = new aws_apigatewayv2_integrations.HttpAlbIntegration('CeleryFlowerHttpAlbIntegration', httpsListener, {
        vpcLink,
    });
    const api = new aws_apigatewayv2.HttpApi(scope, 'CeleryFlowerApiGateway', {
        apiName: 'Fargate Service API for Flower',
        description: 'This service serves the Studio API for Flower Fargate service.',
        createDefaultStage: true,
        corsPreflight: {
            allowHeaders: ['Content-Type'],
            allowMethods: [aws_apigatewayv2.CorsHttpMethod.ANY],
            allowOrigins: ['*'],
        },
        defaultIntegration: celeryFlowerHttpIntegration
    });

    // Add routes to the HTTP API
    api.addRoutes({
        path: '/api/{proxy+}',
        methods: [aws_apigatewayv2.HttpMethod.ANY],
        integration: celeryFlowerHttpIntegration
    });

    const stage = new aws_apigatewayv2.HttpStage(scope, 'CeleryFlowerDefaultStage', {
        httpApi: api,
        stageName: 'Prod',
        autoDeploy: true
    });

    new aws_apigatewayv2.ApiMapping(scope, 'CeleryFlowerBasePathMapping', {
        domainName,
        api,
        stage
    });

    // // Create an API Gateway
    // const api = new apigateway.RestApi(scope, 'StudioApiFlowerApi', {
    //     restApiName: 'Fargate Service API for Flower',
    //     description: 'This service serves the Studio API for Flower Fargate service.',
    //     cloudWatchRole: true,
    //     deployOptions: {
    //         accessLogDestination: new apigateway.LogGroupLogDestination(logGroup),
    //         accessLogFormat: apigateway.AccessLogFormat.jsonWithStandardFields({
    //             caller: true,
    //             httpMethod: true,
    //             ip: true,
    //             protocol: true,
    //             requestTime: true,
    //             resourcePath: true,
    //             responseLength: true,
    //             status: true,
    //             user: true,
    //         }),
    //       },
    // });

    // Add a proxy resource to forward all requests
    // const proxy = api.root.addResource('{proxy+}');
    // const apiResource = api.root.addResource('{proxy+}');
    // apiResource.addMethod('ANY', new apigateway.HttpIntegration(`https://studio-api-flower-${accountStage}-lb.${accountStage}.suno.com/api/{proxy}`, {
    //     proxy: true,
    //     httpMethod: 'ANY',
    //     options: {
    //         requestParameters: {
    //             'integration.request.path.proxy': 'method.request.path.proxy'
    //         },
    //     },
    //     }), {
    //     requestParameters: {
    //         'method.request.path.proxy': true
    //     },
    // });

    // // Map the custom domain to the API Gateway
    // new apigateway.BasePathMapping(scope, 'FlowerBasePathMapping', {
    //     domainName: customDomain,
    //     restApi: api,
    // });

    return api;
};
