// retrigger-build-marker-2
// retrigger-build-marker
#![allow(dead_code)]

mod handlers;

use axum::{extract::DefaultBodyLimit, routing::get, Router};
use std::net::SocketAddr;
use std::sync::Arc;
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt};
use contracts::ProfessionState;

#[tokio::main]
async fn main() {
    tracing_subscriber::registry()
        .with(tracing_subscriber::EnvFilter::new(
            std::env::var("RUST_LOG").unwrap_or_else(|_| "info".into()),
        ))
        .with(tracing_subscriber::fmt::layer())
        .init();

    let database_url = std::env::var("DATABASE_URL").expect("DATABASE_URL must be set");
    let pool = sqlx::postgres::PgPoolOptions::new()
        .max_connections(5)
        .connect(&database_url)
        .await
        .expect("Failed to connect to postgres");

    let redis_url = std::env::var("REDIS_URL")
        .unwrap_or_else(|_| "redis://127.0.0.1:6379".to_string());
    let redis = cache::connect(&redis_url)
        .await
        .expect("Failed to connect to Redis");

    tracing::info!("UGC Content Creators service — connected to DB and Redis");

    let storage = Arc::new(storage::StorageClient::from_env().await);
    let state = ProfessionState { pool, redis, storage };

    let app = Router::new()
        .nest("/api/ugc-content-creators", handlers::router())
        .route("/health", get(|| async { "UGC Content Creators OK" }))
        .layer(DefaultBodyLimit::max(11 * 1024 * 1024))
        .with_state(state);

    let port: u16 = std::env::var("PORT")
        .unwrap_or_else(|_| "9117".to_string())
        .parse()
        .expect("PORT must be a number");
    let addr = SocketAddr::from(([0, 0, 0, 0], port));

    tracing::info!("UGC Content Creators service listening on {}", addr);

    let listener = tokio::net::TcpListener::bind(&addr).await.unwrap();
    axum::serve(listener, app).await.unwrap();
}
