mod api;
mod config;
mod error;
mod mapping;
mod message_consumer;
// mod persistence; // Removed
mod state;
mod worker;
mod yfinance;

use crate::config::AppConfig;
use crate::error::Result;
use crate::state::AppState;
use tracing::{info, warn};
use common_contracts::lifecycle::ServiceRegistrar;
use common_contracts::registry::ServiceRegistration;
use std::sync::Arc;

#[tokio::main]
async fn main() -> Result<()> {
    // Initialize logging
    tracing_subscriber::fmt()
        .with_env_filter(tracing_subscriber::EnvFilter::from_default_env())
        .init();

    info!("Starting yfinance-provider-service...");

    // Load configuration
    let config = AppConfig::load().map_err(|e| error::AppError::Configuration(e.to_string()))?;
    let port = config.server_port;

    // Initialize application state
    let app_state = AppState::new(config.clone());

    // Create the Axum router
    let app = api::create_router(app_state.clone());

    // --- Start the message consumer ---
    tokio::spawn(message_consumer::run(app_state));

    // --- Service Registration ---
    let registrar = ServiceRegistrar::new(
        config.api_gateway_url.clone(),
        ServiceRegistration {
            service_id: format!("{}-{}", "yfinance-provider", uuid::Uuid::new_v4()),
            service_name: "yfinance".to_string(),
            role: common_contracts::registry::ServiceRole::DataProvider,
            base_url: format!("http://{}:{}", config.service_host, port),
            health_check_url: format!("http://{}:{}/health", config.service_host, port),
        }
    );

    let _ = registrar.register().await;

    let registrar = Arc::new(registrar);
    tokio::spawn(registrar.clone().start_heartbeat_loop());

    // Start the HTTP server
    let listener = tokio::net::TcpListener::bind(format!("0.0.0.0:{}", port))
        .await
        .unwrap();
    info!("HTTP server listening on port {}", port);
    
    axum::serve(listener, app)
        .with_graceful_shutdown(shutdown_signal(registrar))
        .await
        .unwrap();

    Ok(())
}

async fn shutdown_signal(registrar: Arc<ServiceRegistrar>) {
    let ctrl_c = async {
        tokio::signal::ctrl_c()
            .await
            .expect("failed to install Ctrl+C handler");
    };

    #[cfg(unix)]
    let terminate = async {
        tokio::signal::unix::signal(tokio::signal::unix::SignalKind::terminate())
            .expect("failed to install signal handler")
            .recv()
            .await;
    };

    #[cfg(not(unix))]
    let terminate = std::future::pending::<()>();

    tokio::select! {
        _ = ctrl_c => {},
        _ = terminate => {},
    }

    info!("Shutdown signal received, deregistering service...");
    let _ = registrar.deregister().await;
}
