use actix_web::{web, HttpResponse};
use service::AppService;

/// Serves robots.txt, blocking all sensitive paths from crawlers.
pub async fn robots(service: web::Data<AppService>) -> HttpResponse {
    let raw = service
        .config
        .main_domain()
        .unwrap_or_else(|_| "https://gitdata.ai".to_string());
    let sitemap_base = if raw.starts_with("https://") {
        raw.trim_end_matches('/').to_string()
    } else if raw.starts_with("http://") {
        raw.replacen("http://", "https://", 1)
    } else {
        format!("https://{raw}")
    };

    let body = format!(
        r#"User-agent: *
Disallow: /api/
Disallow: /health
Disallow: /metrics
Disallow: /ws/
Disallow: /avatar/
Disallow: /blob/
Disallow: /media/
Disallow: /static/
Disallow: /assets/

Sitemap: {sitemap_base}/sitemap.xml
"#,
    );

    HttpResponse::Ok()
        .content_type("text/plain; charset=utf-8")
        .body(body)
}
