#!/usr/bin/env node
import "source-map-support/register";
import * as cdk from "aws-cdk-lib";
import { ConditionCheckLambdaStack } from "../lib/condition-check-lambda-stack";
import { ImageProcessorLambdaStack } from "../lib/image-processor-lambda-stack";
import { VpcStack } from "../lib/vpc-stack";

const app = new cdk.App();

// Get environment from context or default to staging
const environment = app.node.tryGetContext("env") || "staging";

// Environment-specific configurations
const envConfig: Record<
  string,
  {
    databaseUrl: string;
    frontendUrl: string;
    sesFromEmail: string;
    emailEnabled: boolean;
    natGateways: number;
  }
> = {
  staging: {
    // These should be passed via CDK context or SSM parameters in production
    databaseUrl:
      process.env.DATABASE_URL ||
      "postgresql://user:password@localhost:5432/rentall_staging",
    frontendUrl: "https://staging.villageshare.app",
    sesFromEmail: "noreply@villageshare.app",
    emailEnabled: true,
    natGateways: 1, // Single NAT gateway for cost optimization in staging
  },
  prod: {
    databaseUrl:
      process.env.DATABASE_URL ||
      "postgresql://user:password@localhost:5432/rentall_prod",
    frontendUrl: "https://villageshare.app",
    sesFromEmail: "noreply@villageshare.app",
    emailEnabled: true,
    natGateways: 2, // Multi-AZ NAT gateways for high availability in production
  },
};

const config = envConfig[environment];

if (!config) {
  throw new Error(`Unknown environment: ${environment}`);
}

const envProps = {
  env: {
    account: process.env.CDK_DEFAULT_ACCOUNT,
    region: process.env.CDK_DEFAULT_REGION || "us-east-1",
  },
};

// Create the VPC stack first (other stacks depend on it)
const vpcStack = new VpcStack(app, `VpcStack-${environment}`, {
  environment,
  natGateways: config.natGateways,
  maxAzs: 2,
  ...envProps,
  description: `VPC infrastructure with private subnets and VPC endpoints (${environment})`,
  tags: {
    Environment: environment,
    Project: "village-share",
    Service: "networking",
  },
});

// Create the Condition Check Lambda stack
const conditionCheckStack = new ConditionCheckLambdaStack(
  app,
  `ConditionCheckLambdaStack-${environment}`,
  {
    environment,
    databaseUrl: config.databaseUrl,
    frontendUrl: config.frontendUrl,
    sesFromEmail: config.sesFromEmail,
    emailEnabled: config.emailEnabled,
    vpc: vpcStack.vpc,
    lambdaSecurityGroup: vpcStack.lambdaSecurityGroup,
    ...envProps,
    description: `Condition Check Reminder Lambda infrastructure (${environment})`,
    tags: {
      Environment: environment,
      Project: "village-share",
      Service: "condition-check-reminder",
    },
  }
);

// Add dependency on VPC stack
conditionCheckStack.addDependency(vpcStack);

// Create the Image Processor Lambda stack
const imageProcessorStack = new ImageProcessorLambdaStack(
  app,
  `ImageProcessorLambdaStack-${environment}`,
  {
    environment,
    databaseUrl: config.databaseUrl,
    frontendUrl: config.frontendUrl,
    vpc: vpcStack.vpc,
    lambdaSecurityGroup: vpcStack.lambdaSecurityGroup,
    ...envProps,
    description: `Image Processor Lambda infrastructure (${environment})`,
    tags: {
      Environment: environment,
      Project: "village-share",
      Service: "image-processor",
    },
  }
);

// Add dependency on VPC stack
imageProcessorStack.addDependency(vpcStack);
