<?php
require 'vendor/autoload.php'; // Include PHPMailer autoload or Twilio SDK
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

// Function to send email notification
function sendEmailNotification($recipient, $subject, $message) {
    $mail = new PHPMailer(true);
    try {
        // Server settings
        $mail->isSMTP();
        $mail->Host = 'smtp.example.com';
        $mail->SMTPAuth = true;
        $mail->Username = 'your-email@example.com';
        $mail->Password = 'your-password';
        $mail->SMTPSecure = 'tls';
        $mail->Port = 587;

        // Recipients
        $mail->setFrom('your-email@example.com', 'Your Name');
        $mail->addAddress($recipient);

        // Content
        $mail->isHTML(true);
        $mail->Subject = $subject;
        $mail->Body = $message;

        $mail->send();
        return true;
    } catch (Exception $e) {
        return false;
    }
}

// Function to send SMS notification using Twilio
function sendSMSNotification($recipient, $message) {
    // Your Twilio credentials
    $account_sid = 'AC1aa0f47da1a322e4160c8bc891e6938d';
    $auth_token = '68d589185fd7b0d66a4399790af164e8';
    $twilio_number = '+13345003515';

    // Initialize Twilio client
    $client = new Twilio\Rest\Client($account_sid, $auth_token);

    try {
        // Send SMS
        $client->messages->create(
            $recipient,
            array(
                'from' => $twilio_number,
                'body' => $message
            )
        );
        return true;
    } catch (Exception $e) {
        return false;
    }
}

// Example usage:
$user_id = 1; // Replace with recipient's user ID
$message = "Your notification message goes here.";
$subject = "Notification Subject";

// Send email notification
$recipientEmail = "recipient@example.com"; // Replace with recipient's email
$emailSent = sendEmailNotification($recipientEmail, $subject, $message);

// Send SMS notification
$recipientPhone = "+1234567890"; // Replace with recipient's phone number
$smsSent = sendSMSNotification($recipientPhone, $message);

// Insert notification into database
if ($emailSent || $smsSent) {
    // Insert notification record into database (notifications table)
    $timestamp = date('Y-m-d H:i:s');
    // Perform database insert operation here
    $notificationMessage = "Notification sent successfully!";
} else {
    $notificationMessage = "Failed to send notification.";
}

echo $notificationMessage;
?>
