#!/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";

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;
  }
> = {
  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,
  },
  prod: {
    databaseUrl:
      process.env.DATABASE_URL ||
      "postgresql://user:password@localhost:5432/rentall_prod",
    frontendUrl: "https://villageshare.app",
    sesFromEmail: "noreply@villageshare.app",
    emailEnabled: true,
  },
};

const config = envConfig[environment];

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

// Create the Condition Check Lambda stack
new ConditionCheckLambdaStack(app, `ConditionCheckLambdaStack-${environment}`, {
  environment,
  databaseUrl: config.databaseUrl,
  frontendUrl: config.frontendUrl,
  sesFromEmail: config.sesFromEmail,
  emailEnabled: config.emailEnabled,
  env: {
    account: process.env.CDK_DEFAULT_ACCOUNT,
    region: process.env.CDK_DEFAULT_REGION || "us-east-1",
  },
  description: `Condition Check Reminder Lambda infrastructure (${environment})`,
  tags: {
    Environment: environment,
    Project: "village-share",
    Service: "condition-check-reminder",
  },
});

// Create the Image Processor Lambda stack
new ImageProcessorLambdaStack(app, `ImageProcessorLambdaStack-${environment}`, {
  environment,
  databaseUrl: config.databaseUrl,
  frontendUrl: config.frontendUrl,
  env: {
    account: process.env.CDK_DEFAULT_ACCOUNT,
    region: process.env.CDK_DEFAULT_REGION || "us-east-1",
  },
  description: `Image Processor Lambda infrastructure (${environment})`,
  tags: {
    Environment: environment,
    Project: "village-share",
    Service: "image-processor",
  },
});
