using Monitoring.AppHost;

var builder = DistributedApplication.CreateBuilder(args);

// var cache = builder.AddRedis(Constants.CacheModuleName).WithLifetime(ContainerLifetime.Persistent);

var cache = builder.AddConnectionString(Constants.CacheModuleName);

// var databaseServer = builder
// 	.AddPostgres(Constants.DatabaseModuleName)
// 	.WithPgAdmin(x => x.WithHostPort(5050).WithLifetime(ContainerLifetime.Persistent))
// 	.WithDataVolume(Constants.DatabaseVolumeName)
// 	.WithLifetime(ContainerLifetime.Persistent);

var monitoringDatabase = builder.AddConnectionString(Constants.MonitoringDatabaseName);
var integrationsDatabase = builder.AddConnectionString(Constants.ForwarderDatabaseName);

// var messageQueue = builder
// 	.AddRabbitMQ(Constants.MessageQueueModuleName)
// 	.WithDataVolume("monitoring-rabbitmq-dev")
// 	.WithLifetime(ContainerLifetime.Persistent)
// 	.WithManagementPlugin(15672);

// var messageQueue = builder.AddConnectionString(Constants.MessageQueueModuleName);

const string JWT_ISSUER = "Monitoring Issuer";
const string JWT_AUDIENCE = "Monitoring Audience";
const string ENVIRONMENT = "Development";
const string JWT_KEY =
	"SUPER_SECRET_KEY_OF_THE_WORLD_WHICH_SHOULD_BE_CHANGED_BEFORE_DEPLOYMENT_PLEASE";

var notifications = builder
	.AddProject<Projects.Monitoring_Notifications>("notifications")
	.WithHttpHealthCheck("/health")
	.WithReference(cache)
	.WaitFor(cache)
	// .WithReference(messageQueue)
	// .WaitFor(messageQueue)
	.WithEnvironment(async context =>
	{
		await AddCommonEnvironment(
			context,
			Constants.CacheModuleName,
			// messageQueue,
			ENVIRONMENT,
			JWT_ISSUER,
			JWT_AUDIENCE,
			JWT_KEY
		);
	});

var ping = builder
	.AddProject<Projects.Ping>("ping")
	.WithReference(monitoringDatabase)
	.WaitFor(monitoringDatabase)
	.WithReference(cache)
	.WaitFor(cache)
	// .WithReference(messageQueue)
	// .WaitFor(messageQueue)
	.WithEnvironment(async context =>
	{
		await AddCommonEnvironment(
			context,
			Constants.CacheModuleName,
			// messageQueue,
			ENVIRONMENT,
			JWT_ISSUER,
			JWT_AUDIENCE,
			JWT_KEY
		);

		context.EnvironmentVariables["DATABASE_ASPIRE_NAME"] = Constants.MonitoringDatabaseName;
	});

var api = builder
	.AddProject<Projects.Monitoring>("api")
	.WithReference(monitoringDatabase)
	.WaitFor(monitoringDatabase)
	.WithHttpHealthCheck("/health")
	.WithReference(cache)
	.WaitFor(cache)
	// .WithReference(messageQueue)
	// .WaitFor(messageQueue)
	.WithEnvironment(async context =>
	{
		await AddCommonEnvironment(
			context,
			Constants.CacheModuleName,
			// messageQueue,
			Constants.ENVIRONMENT,
			Constants.JWT_ISSUER,
			Constants.JWT_AUDIENCE,
			Constants.JWT_KEY
		);

		context.EnvironmentVariables["DATABASE_ASPIRE_NAME"] = Constants.MonitoringDatabaseName;
	});

var frontend = builder
	.AddPnpmApp("frontend", "../Monitoring.Web", "dev")
	.WithPnpmPackageInstallation(x =>
	{
		x.WithIconName("WindowConsole");
	})
	.WithHttpEndpoint(port: 3000, name: "http", targetPort: 3001)
	.WithReference(api)
	.WaitFor(api)
	.WithReference(notifications)
	.WaitFor(notifications);

builder
	.AddPnpmApp("api-generator", "../Monitoring.Web", "generate-api")
	.WithParentRelationship(frontend)
	.WaitFor(api)
	.WithIconName("CodeBlockEdit");

var forwarder = builder
	.AddProject<Projects.Forwarder>("forwarder-backend")
	.WithReference(integrationsDatabase)
	.WaitFor(integrationsDatabase)
	.WithHttpHealthCheck("/health")
	.WithReference(cache)
	.WaitFor(cache)
	// .WithReference(messageQueue)
	// .WaitFor(messageQueue)
	.WithEnvironment(async context =>
	{
		context.EnvironmentVariables["API_URL"] = api.GetEndpoint("http");
		context.EnvironmentVariables["API_TOKEN"] =
			"bk7TUBgM-65_N_PhnRNOB8iIHiiCnNHVxbmx4_vVe32h2Tx4FqrTNqEPGmtMIuQLh7d4aoKHz_gDpLrmZjInQzLfgEdF4jnBXNb1vX69HLC0a4RyA0XJP0fKUWlYtAscga9KNYwSTPl87sFbeoRio1OusSaPSg0DfvTt1ifs_y4A7BK0qyb_qce3W9nJpzrz-87XJXDf6fEj-4HE8EhB84ke6S2zGNRRLgq4NXsZcnh23bvYvK3FvNJ7UUp6Nw8GCpjK4CSVu_gcn0JIvNACE0f0RfGt1Hs5yeO_7SMLDx26eTgp8k45yUID0E42Y-91LA_fAVqPoHSy2jNbFxKclZS4OWa_VGRqNX9YqJKXjxkS6hdYGWRNA6mkjV-OkPaYnGwNz7XlWIHqHY5IAJtFEJHYkMZjWwIeiuIr50wt6fyLgbZPUnE_bgLyIy1AyrkR2UUEq7LyxzBpRADkcmOK2rIcrqaQokq3Wew7AzEVraBD7ofaB_4-FR99ymIgAC1jrLbFVcW9iCvMdSxfbkKjSuYvMI89Q0OrLZw63hGlIHj2HiwQgHSJnk7A9QBsW6HNK8B9CBQNQT8SiHpehMbkEf9agDZLOJYEU9HjYOcKUzg7Ax4PLYehVGBTsQ-OP-11Qlu8p3IzZqors1CPaOtISiCx--U";

		context.EnvironmentVariables["ADEMCO_PORTS"] = "8010,8011,5252";

		context.EnvironmentVariables["DATABASE_ASPIRE_NAME"] = Constants.ForwarderDatabaseName;
	});

var forwarderFrontend = builder
	.AddExecutable(
		"forwarder-frontend",
		"pnpm",
		Path.Combine("..", "..", "Forwarder", "forwarder.web"),
		"run",
		"dev",
		"--port",
		"3011"
	)
	.WithHttpEndpoint(port: 3010, name: "http", targetPort: 3011)
	.WaitFor(forwarder);

await builder.Build().RunAsync();

static async Task AddCommonEnvironment(
	EnvironmentCallbackContext context,
	string cacheModuleName,
	// IResourceBuilder<RabbitMQServerResource> messageQueue,
	string environment,
	string jwtIssuer,
	string jwtAudience,
	string jwtKey
)
{
	// Provide individual connection details
	context.EnvironmentVariables["ASPNETCORE_ENVIRONMENT"] = environment;
	context.EnvironmentVariables["JWT_KEY"] = jwtKey;
	context.EnvironmentVariables["JWT_ISSUER"] = jwtIssuer;
	context.EnvironmentVariables["JWT_AUDIENCE"] = jwtAudience;
	context.EnvironmentVariables["CACHE_ASPIRE_NAME"] = cacheModuleName;
	context.EnvironmentVariables["MESSAGE_QUEUE_URL"] = $"amqp://rabbitmq.wband.ru:5672";
	context.EnvironmentVariables["MESSAGE_QUEUE_USERNAME"] =
		Constants.MESSAGE_QUEUE_DEFAULT_USERNAME;
	context.EnvironmentVariables["MESSAGE_QUEUE_PASSWORD"] =
		Constants.MESSAGE_QUEUE_DEFAULT_USERNAME;
}
