<?php
date_default_timezone_set('America/New_York');
error_reporting(E_ALL);
ini_set('display_errors', 1);

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

require_once __DIR__ . '/PHPMailer/Exception.php';
require_once __DIR__ . '/PHPMailer/PHPMailer.php';
require_once __DIR__ . '/PHPMailer/SMTP.php';

// Recognized Carrier Domains Map
$carrier_map = [
    'verizon'  => 'vtext.com',
    'spectrum' => 'vtext.com',
    'verizonmms' => 'vzwpix.com',
    'att'      => 'txt.att.net',
    'tmobile'  => 'tmomail.net',
    'googlefi' => 'msg.fi.google.com'
];

// Read Command-Line Arguments ($argv[0] is always script name)
// Argument 1: Phone Number (10 digits)
// Argument 2: Carrier (e.g., verizon, spectrum, att, tmobile, or explicit domain like vtext.com)
// Argument 3: Message / Verification Code (Optional - generates random 4-digit code if omitted)

$raw_phone   = $argv[1] ?? '7323196727';
$raw_carrier = strtolower($argv[2] ?? 'vtext.com');
$custom_code = $argv[3] ?? rand(1000, 9999);

// Sanitize phone number (strip spaces, hyphens, parentheses)
$phone_number = preg_replace('/[^0-9]/', '', $raw_phone);

// Resolve Carrier Domain from alias or raw domain
$carrier_domain = $carrier_map[$raw_carrier] ?? $raw_carrier;

// Validate phone format
if (strlen($phone_number) !== 10) {
    echo "ERROR: Please provide a valid 10-digit phone number.\n";
    echo "Usage: php send.sms.php <phone_number> <carrier_or_domain> [message_or_code]\n";
    exit(1);
}

// Build recipient address and message payload
$to      = "{$phone_number}@{$carrier_domain}";
$subject = "Alert";
$message = "Your verification code is: {$custom_code}";

$mail = new PHPMailer(true);

try {
    // Authenticate via InMotion cPanel SMTP
    $mail->isSMTP();
    $mail->Host       = 'mail.intellihometech.com';
    $mail->SMTPAuth   = true;
    $mail->Username   = 'alerts@intellihometech.com';
    $mail->Password   = 'YOUR_UPDATED_PASSWORD_HERE'; // Replace with active password
    $mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS;
    $mail->Port       = 465;

    // Header & Recipient Setup
    $mail->setFrom('alerts@intellihometech.com', 'AlertSystem');
    $mail->addAddress($to);

    $mail->Subject = $subject;
    $mail->Body    = $message;

    $mail->send();

    // Print CLI Execution Report
    echo "========================================\n";
    echo "      SMS DISPATCH DEBUG REPORT         \n";
    echo "========================================\n";
    echo "Status:       SUCCESS\n";
    echo "Phone:        {$phone_number}\n";
    echo "Gateway:      {$carrier_domain}\n";
    echo "Recipient:    {$to}\n";
    echo "Message:      {$message}\n";
    echo "========================================\n";

    // Write persistent log entry
    $timestamp = date('Y-m-d H:i:s');
    $log_entry = "[{$timestamp}] STATUS: OK | TO: {$to} | MSG: {$message}\n";
    file_put_contents(__DIR__ . '/sms_debug.log', $log_entry, FILE_APPEND);

} catch (Exception $e) {
    echo "========================================\n";
    echo "      SMS DISPATCH FAILED               \n";
    echo "========================================\n";
    echo "Mailer Error: {$mail->ErrorInfo}\n";
    echo "========================================\n";
    exit(1);
}
?>
