//! Email routes module
//!
//! This module defines the HTTP routes for email functionality including
//! form submissions, custom emails, notifications, and status endpoints.

use crate::email::EmailService;
use crate::handlers::email::{
    get_email_status, send_contact_form, send_custom_email, send_notification, send_support_form,
};
use axum::{
    Router,
    routing::{get, post},
};
use std::sync::Arc;

/// Create email routes
pub fn create_email_routes() -> Router<Arc<EmailService>> {
    Router::new()
        // GET /api/email/status - Get email service status
        .route("/status", get(get_email_status))
        // POST /api/email/contact - Send contact form
        .route("/contact", post(send_contact_form))
        // POST /api/email/support - Send support form
        .route("/support", post(send_support_form))
        // POST /api/email/send - Send custom email
        .route("/send", post(send_custom_email))
        // POST /api/email/notification - Send notification
        .route("/notification", post(send_notification))
}

/// Create public email routes (for forms that don't require authentication)
#[allow(dead_code)]
pub fn create_public_email_routes() -> Router<Arc<EmailService>> {
    Router::new()
        // POST /api/email/contact - Send contact form (public)
        .route("/contact", post(send_contact_form))
        // POST /api/email/support - Send support form (public)
        .route("/support", post(send_support_form))
        // GET /api/email/status - Get email service status (public)
        .route("/status", get(get_email_status))
}

/// Create admin email routes (for administrative email functions)
#[allow(dead_code)]
pub fn create_admin_email_routes() -> Router<Arc<EmailService>> {
    Router::new()
        // POST /api/email/send - Send custom email (admin only)
        .route("/send", post(send_custom_email))
        // POST /api/email/notification - Send notification (admin only)
        .route("/notification", post(send_notification))
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::email::{EmailService, EmailServiceBuilder};
    // use axum::http::StatusCode;
    // use axum_test::TestServer; // Disabled due to missing dependency
    //use serde_json::json;
    use tempfile::TempDir;

    #[allow(dead_code)]
    async fn create_test_email_service() -> Arc<EmailService> {
        let temp_dir = TempDir::new().unwrap();
        let service = EmailServiceBuilder::new()
            .console_provider()
            .template_dir(temp_dir.path().to_str().unwrap_or_default())
            .build()
            .await
            .unwrap();
        Arc::new(service)
    }

    // #[tokio::test]
    // async fn test_email_status_endpoint() {
    //     let email_service = create_test_email_service().await;
    //     let app = create_email_routes().with_state(email_service);
    //     let server = TestServer::new(app).unwrap();

    //     let response = server.get("/status").await;
    //     assert_eq!(response.status_code(), StatusCode::OK);

    //     let json: serde_json::Value = response.json();
    //     assert!(json["enabled"].as_bool().unwrap());
    //     assert_eq!(json["provider"].as_str().unwrap(), "Console");
    //     assert!(json["configured"].as_bool().unwrap());
    // }

    // #[tokio::test]
    // async fn test_contact_form_endpoint() {
    //     let email_service = create_test_email_service().await;
    //     let app = create_email_routes().with_state(email_service);
    //     let server = TestServer::new(app).unwrap();

    //     let contact_form = json!({
    //         "name": "John Doe",
    //         "email": "john@example.com",
    //         "subject": "Test Contact",
    //         "message": "This is a test contact form message"
    //     });

    //     let response = server.post("/contact").json(&contact_form).await;
    //     assert_eq!(response.status_code(), StatusCode::OK);

    //     let json: serde_json::Value = response.json();
    //     assert_eq!(json["status"].as_str().unwrap(), "sent");
    //     assert!(json["message_id"].as_str().unwrap().starts_with("console-"));
    // }

    // #[tokio::test]
    // async fn test_support_form_endpoint() {
    //     let email_service = create_test_email_service().await;
    //     let app = create_email_routes().with_state(email_service);
    //     let server = TestServer::new(app).unwrap();

    //     let support_form = json!({
    //         "name": "Jane Smith",
    //         "email": "jane@example.com",
    //         "subject": "Support Request",
    //         "message": "I need help with something",
    //         "priority": "high",
    //         "category": "technical"
    //     });

    //     let response = server.post("/support").json(&support_form).await;
    //     assert_eq!(response.status_code(), StatusCode::OK);

    //     let json: serde_json::Value = response.json();
    //     assert_eq!(json["status"].as_str().unwrap(), "sent");
    // }

    // #[tokio::test]
    // async fn test_custom_email_endpoint() {
    //     let email_service = create_test_email_service().await;
    //     let app = create_email_routes().with_state(email_service);
    //     let server = TestServer::new(app).unwrap();

    //     let custom_email = json!({
    //         "to": "recipient@example.com",
    //         "subject": "Custom Email",
    //         "text_body": "This is a custom email"
    //     });

    //     let response = server.post("/custom").json(&custom_email).await;
    //     assert_eq!(response.status_code(), StatusCode::OK);

    //     let json: serde_json::Value = response.json();
    //     assert_eq!(json["status"].as_str().unwrap(), "sent");
    // }

    // #[tokio::test]
    // async fn test_notification_endpoint() {
    //     let email_service = create_test_email_service().await;
    //     let app = create_email_routes().with_state(email_service);
    //     let server = TestServer::new(app).unwrap();

    //     let notification = json!({
    //         "to": "user@example.com",
    //         "title": "Test Notification",
    //         "message": "This is a test notification",
    //         "content": "Additional content here"
    //     });

    //     let response = server.post("/notification").json(&notification).await;
    //     assert_eq!(response.status_code(), StatusCode::OK);

    //     let json: serde_json::Value = response.json();
    //     assert_eq!(json["status"].as_str().unwrap(), "sent");
    // }

    // #[tokio::test]
    // async fn test_invalid_contact_form() {
    //     let email_service = create_test_email_service().await;
    //     let app = create_email_routes().with_state(email_service);
    //     let server = TestServer::new(app).unwrap();

    //     let invalid_form = json!({
    //         "name": "",
    //         "email": "invalid-email",
    //         "subject": "",
    //         "message": ""
    //     });

    //     let response = server.post("/contact").json(&invalid_form).await;
    //     assert_eq!(response.status_code(), StatusCode::BAD_REQUEST);
    // }

    // #[tokio::test]
    // async fn test_invalid_custom_email() {
    //     let email_service = create_test_email_service().await;
    //     let app = create_email_routes().with_state(email_service);
    //     let server = TestServer::new(app).unwrap();

    //     let invalid_email = json!({
    //         "to": "",
    //         "subject": "Test",
    //         // Missing body and template
    //     });

    //     let response = server.post("/send").json(&invalid_email).await;
    //     assert_eq!(response.status_code(), StatusCode::BAD_REQUEST);

    //     let json: serde_json::Value = response.json();
    //     assert_eq!(json["code"].as_str().unwrap(), "VALIDATION_ERROR");
    // }
}
