import * as cdk from "aws-cdk-lib";
import "source-map-support/register";
import { AlertStack } from "../lib/alert-stack";
import { CloudTrailStack } from "../lib/cloud-trail-stack";
import { ConfigStackSet } from "../lib/config-stackset";
import { COST_DISTRIBUTION_TAG_KEY } from "../lib/const";
import { context } from "../lib/context";
import { ContractEventAlertStack } from "../lib/contract-event-alert-stack";
import { DomainStack } from "../lib/domain-stack";
import { HealthAlertStackSet } from "../lib/health-alert-stackset";
import { IAMStack } from "../lib/iam-stack";
import { SesStack } from "../lib/ses-stack";
import { StackSetRoleStack } from "../lib/stackset-role-stack";
import { StageAlertStack } from "../lib/stage-alert-stack";
import { SupportAppStack } from "../lib/support-app-stack";
import { WorkspacesWebStack } from "../lib/workspaces-web-stack";

const app = new cdk.App();

cdk.Tags.of(app).add(COST_DISTRIBUTION_TAG_KEY, "account");

const accountId = process.env.CDK_DEFAULT_ACCOUNT!;
const {
  accountAlias,
  domainProps,
  iamProps,
  alertProps,
  stageAlertProps,
  healthAlertProps,
  supportAppProps,
  contractEventAlertProps,
  sesProps,
} = context[accountId]!;

new CloudTrailStack(app, "CloudTrailStack", {
  stackName: `account-cloudtrail-stack`,
  accountAlias,
});

const domain = new DomainStack(app, "DomainStack", {
  stackName: `account-domain-stack`,
  domainProps,
});

const workspacesWeb = new WorkspacesWebStack(app, "WorkspacesWebStack", {
  stackName: `account-workspaces-web-stack`,
  accountAlias,
});

new IAMStack(app, "IAMStack", {
  stackName: `account-iam-stack`,
  iamProps,
  workspacesWebIp: workspacesWeb.natIp,
});

const stacksetRole = new StackSetRoleStack(app, "StackSetRoleStack", {
  stackName: `account-stackset-role-stack`,
});

const config = new ConfigStackSet(app, "ConfigStackSet", {
  stackName: `account-config-stacks`,
  accountAlias,
  centralRegion: process.env.CDK_DEFAULT_REGION!,
  administrationRole: stacksetRole.administrationRole,
  executionRole: stacksetRole.executionRole,
});
config.addDependency(stacksetRole);

const alert = new AlertStack(app, "AlertStack", {
  stackName: `account-alert-stack`,
  accountAlias,
  alertProps,
  additionalTopic: workspacesWeb.topic,
});
alert.addDependency(workspacesWeb);

const healthAlert = new HealthAlertStackSet(app, "HealthAlertStackSet", {
  stackName: `account-health-alert-stacks`,
  accountAlias,
  administrationRole: stacksetRole.administrationRole,
  executionRole: stacksetRole.executionRole,
  healthAlertProps,
});
healthAlert.addDependency(stacksetRole);

new StageAlertStack(app, "StageAlertStack", {
  stackName: `account-stage-alert-stack`,
  accountAlias,
  stageAlertProps,
});

new ContractEventAlertStack(app, "ContractEventAlertStack", {
  stackName: `account-contract-event-alert-stack`,
  accountAlias,
  accountId,
  contractEventAlertProps,
});

new SupportAppStack(app, "SupportAppStack", {
  stackName: `account-support-app-stack`,
  accountAlias,
  supportAppProps,
});

const ses = new SesStack(app, "SesStack", {
  stackName: `account-ses-stack`,
  sesProps,
});
ses.addDependency(domain);
