use std::sync::Arc;

use common_contracts::messages::GenerateReportCommand;
use futures::StreamExt;
use tracing::{error, info};

use crate::{state::AppState, worker::run_report_generation_workflow};

const SUBJECT_NAME: &str = "analysis.commands.generate_report";

pub async fn subscribe_to_commands(
    app_state: AppState,
    nats_client: async_nats::Client,
) -> Result<(), anyhow::Error> {
    let mut subscriber = nats_client.subscribe(SUBJECT_NAME.to_string()).await?;
    info!(
        "Consumer started, waiting for commands on subject '{}'",
        SUBJECT_NAME
    );

    while let Some(message) = subscriber.next().await {
        info!("Received NATS command to generate report.");
        let state_clone = app_state.clone();
        tokio::spawn(async move {
            match serde_json::from_slice::<GenerateReportCommand>(&message.payload) {
                Ok(command) => {
                    info!(
                        "Deserialized command for symbol: {}, template: {}",
                        command.symbol, command.template_id
                    );
                    if let Err(e) = run_report_generation_workflow(Arc::new(state_clone), command).await
                    {
                        error!("Error running report generation workflow: {:?}", e);
                    }
                }
                Err(e) => {
                    error!("Failed to deserialize GenerateReportCommand: {}", e);
                }
            }
        });
    }

    Ok(())
}
