<?php
/**
 * NotificationManager - Handles sending notifications via email
 * Supports email channel with templates
 */

require_once 'email-templates.php';

class NotificationManager {
    
    private $conn;
    private $from_email;
    private $from_name;
    
    public function __construct($conn) {
        $this->conn = $conn;
        $this->from_email = 'noreply@healthlink.com';
        $this->from_name = 'HealthLink';
    }
    
    /**
     * Send appointment confirmation email
     */
    public function sendAppointmentConfirmation($patient_email, $patient_name, $doctor_name, $appointment_date, $appointment_time, $appointment_id) {
        $template = EmailTemplates::appointmentConfirmation($patient_name, $doctor_name, $appointment_date, $appointment_time, $appointment_id);
        return $this->sendEmail($patient_email, $template['subject'], $template['body']);
    }
    
    /**
     * Send appointment reminder email (24 hours before)
     */
    public function sendAppointmentReminder($patient_email, $patient_name, $doctor_name, $appointment_date, $appointment_time, $appointment_id) {
        $template = EmailTemplates::appointmentReminder($patient_name, $doctor_name, $appointment_date, $appointment_time, $appointment_id);
        return $this->sendEmail($patient_email, $template['subject'], $template['body']);
    }
    
    /**
     * Send registration confirmation email
     */
    public function sendRegistrationConfirmation($email, $name, $health_id) {
        $template = EmailTemplates::registrationConfirmation($name, $health_id);
        return $this->sendEmail($email, $template['subject'], $template['body']);
    }
    
    /**
     * Send doctor approval email
     */
    public function sendDoctorApprovalEmail($doctor_email, $doctor_name, $license_number) {
        $template = EmailTemplates::doctorApprovalConfirmation($doctor_name, $license_number);
        return $this->sendEmail($doctor_email, $template['subject'], $template['body']);
    }
    
    /**
     * Send payment confirmation email
     */
    public function sendPaymentConfirmation($patient_email, $patient_name, $amount, $payment_id, $appointment_id) {
        $template = EmailTemplates::paymentConfirmation($patient_name, $amount, $payment_id, $appointment_id);
        return $this->sendEmail($patient_email, $template['subject'], $template['body']);
    }
    
    /**
     * Send consultation update email
     */
    public function sendConsultationUpdate($patient_email, $patient_name, $doctor_name, $status, $notes = '') {
        $template = EmailTemplates::consultationUpdate($patient_name, $doctor_name, $status, $notes);
        return $this->sendEmail($patient_email, $template['subject'], $template['body']);
    }
    
    /**
     * Send support ticket response email
     */
    public function sendSupportResponse($email, $name, $ticket_id, $response, $status) {
        $template = EmailTemplates::supportTicketResponse($name, $ticket_id, $response, $status);
        return $this->sendEmail($email, $template['subject'], $template['body']);
    }
    
    /**
     * Generic email sending function using PHP mail()
     */
    private function sendEmail($to, $subject, $body) {
        $headers = "From: {$this->from_name} <{$this->from_email}>\r\n";
        $headers .= "Content-Type: text/plain; charset=UTF-8\r\n";
        $headers .= "X-Mailer: HealthLink\r\n";
        
        // Log to database
        $stmt = $this->conn->prepare("
            INSERT INTO email_logs (recipient, subject, body, status, sent_at)
            VALUES (?, ?, ?, 'pending', NOW())
        ");
        $status = 'pending';
        $stmt->bind_param('sss', $to, $subject, $body);
        $stmt->execute();
        
        // Try to send email
        $send_result = mail($to, $subject, $body, $headers);
        
        // Update status in database
        if ($send_result) {
            $update_stmt = $this->conn->prepare("UPDATE email_logs SET status = 'sent', sent_at = NOW() WHERE recipient = ? AND subject = ? ORDER BY id DESC LIMIT 1");
            $update_stmt->bind_param('ss', $to, $subject);
            $update_stmt->execute();
        }
        
        return $send_result;
    }
    
    /**
     * Store notification in database
     */
    public function storeNotification($user_type, $user_id, $title, $message, $channel = 'email', $type = null) {
        $stmt = $this->conn->prepare("
            INSERT INTO notifications (user_type, user_id, title, message, channel, type, created_at)
            VALUES (?, ?, ?, ?, ?, ?, NOW())
        ");
        $stmt->bind_param('ssisss', $user_type, $user_id, $title, $message, $channel, $type);
        return $stmt->execute();
    }
}
?>
