package com.gnx.telemedicine.health;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.actuate.health.Health;
import org.springframework.boot.actuate.health.HealthIndicator;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * Custom health indicator for email service connectivity.
 */
@Component
@RequiredArgsConstructor
@Slf4j
public class EmailServiceHealthIndicator implements HealthIndicator {

    private final JavaMailSender mailSender;

    @Override
    public Health health() {
        Map<String, Object> details = new HashMap<>();
        
        try {
            // Test email service connectivity by checking session
            // Cast to JavaMailSenderImpl to access session
            if (!(mailSender instanceof JavaMailSenderImpl)) {
                return Health.down()
                        .withDetail("status", "DOWN")
                        .withDetail("error", "JavaMailSender is not an instance of JavaMailSenderImpl")
                        .withDetail("service", "Email (SMTP)")
                        .build();
            }
            
            JavaMailSenderImpl mailSenderImpl = (JavaMailSenderImpl) mailSender;
            var session = mailSenderImpl.getSession();
            details.put("host", session.getProperty("mail.smtp.host"));
            details.put("port", session.getProperty("mail.smtp.port"));
            details.put("protocol", session.getProperty("mail.transport.protocol"));
            
            // Try to connect to the mail server
            var transport = session.getTransport();
            try {
                String host = session.getProperty("mail.smtp.host");
                String username = session.getProperty("mail.smtp.user");
                String password = session.getProperty("mail.smtp.password");
                
                if (host != null) {
                    // Test connection (without actually sending)
                    transport.connect(host, username, password);
                    transport.close();
                    
                    details.put("status", "UP");
                    details.put("connectivity", "Connected");
                    
                    return Health.up()
                            .withDetails(details)
                            .withDetail("service", "Email (SMTP)")
                            .build();
                } else {
                    return Health.down()
                            .withDetail("status", "DOWN")
                            .withDetail("error", "Email host not configured")
                            .withDetail("service", "Email (SMTP)")
                            .build();
                }
            } catch (Exception e) {
                log.warn("Email service connectivity check failed: {}", e.getMessage());
                return Health.down()
                        .withDetails(details)
                        .withDetail("status", "DOWN")
                        .withDetail("error", "Cannot connect to email server: " + e.getMessage())
                        .withDetail("service", "Email (SMTP)")
                        .withException(e)
                        .build();
            }
        } catch (Exception e) {
            log.error("Email service health check failed", e);
            return Health.down()
                    .withDetail("status", "DOWN")
                    .withDetail("error", e.getMessage())
                    .withDetail("service", "Email (SMTP)")
                    .withException(e)
                    .build();
        }
    }
}

