use std::{path::PathBuf, sync::Arc};

use harmony::{
    data::Id,
    inventory::Inventory,
    maestro::Maestro,
    modules::{
        application::{
            ApplicationScore, RustWebFramework, RustWebapp,
            features::{ContinuousDelivery, Monitoring},
        },
        monitoring::alert_channel::{
            discord_alert_channel::DiscordWebhook, webhook_receiver::WebhookReceiver,
        },
        tenant::TenantScore,
    },
    topology::{K8sAnywhereTopology, Url, tenant::TenantConfig},
};

#[tokio::main]
async fn main() {
    env_logger::init();

    let topology = K8sAnywhereTopology::from_env();
    let mut maestro = Maestro::initialize(Inventory::autoload(), topology)
        .await
        .unwrap();
    //TODO there is a bug where the application is deployed into the namespace matching the
    //application name and the tenant is created in the namesapce matching the tenant name
    //in order for the application to be deployed in the tenant namespace the application.name and
    //the TenantConfig.name must match
    let tenant = TenantScore {
        config: TenantConfig {
            id: Id::from_str("test-tenant-id"),
            name: "example-monitoring".to_string(),
            ..Default::default()
        },
    };
    let application = Arc::new(RustWebapp {
        name: "example-monitoring".to_string(),
        domain: Url::Url(url::Url::parse("https://rustapp.harmony.example.com").unwrap()),
        project_root: PathBuf::from("./examples/rust/webapp"),
        framework: Some(RustWebFramework::Leptos),
    });

    let webhook_receiver = WebhookReceiver {
        name: "sample-webhook-receiver".to_string(),
        url: Url::Url(url::Url::parse("https://webhook-doesnt-exist.com").unwrap()),
    };

    let app = ApplicationScore {
        features: vec![Box::new(Monitoring {
            alert_receiver: vec![Box::new(webhook_receiver)],
            application: application.clone(),
        })],
        application,
    };

    maestro.register_all(vec![Box::new(tenant), Box::new(app)]);
    harmony_cli::init(maestro, None).await.unwrap();
}
