<?php
/**
 * Non-Masking SMS Setup Script
 * 
 * This script creates the necessary database tables for non-masking SMS functionality
 */

require_once(__DIR__ . '/config/database.php');

try {
    // Create messages_log table if it doesn't exist
    $pdo->exec("
        CREATE TABLE IF NOT EXISTS messages_log (
            id INT AUTO_INCREMENT PRIMARY KEY,
            message_type VARCHAR(50) NOT NULL,
            message_content LONGTEXT NOT NULL,
            recipient_count INT DEFAULT 0,
            recipients JSON,
            sent_recipients JSON,
            failed_recipients JSON,
            sender_id INT,
            status ENUM('pending', 'sent', 'delivered', 'failed', 'partial') DEFAULT 'pending',
            method ENUM('sms', 'email', 'push_notification', 'non_musking') DEFAULT 'sms',
            delivery_report TEXT,
            sent_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
            delivered_at TIMESTAMP NULL,
            created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
            updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
            INDEX idx_status (status),
            INDEX idx_method (method),
            INDEX idx_sent_at (sent_at),
            INDEX idx_sender (sender_id)
        )
    ");
    
    echo "✓ messages_log table created successfully\n";
    
    // Create settings table if it doesn't exist (if not already created)
    $pdo->exec("
        CREATE TABLE IF NOT EXISTS settings (
            id INT PRIMARY KEY AUTO_INCREMENT,
            setting_key VARCHAR(100) UNIQUE,
            setting_value TEXT,
            created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
            updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
        )
    ");
    
    echo "✓ settings table verified/created successfully\n";
    
    // Insert default non-masking settings if they don't exist
    $stmt = $pdo->prepare("
        INSERT IGNORE INTO settings (setting_key, setting_value) 
        VALUES (?, ?)
    ");
    
    $default_settings = [
        ['non_musking_enabled', '0'],
        ['non_musking_method', 'sms'],
        ['non_musking_api_url', ''],
        ['non_musking_api_key', ''],
        ['non_musking_username', ''],
        ['non_musking_password', ''],
        ['non_musking_sender_id', 'SCHOOL'],
        ['non_musking_tpl_general_message', 'প্রিয় {name}, {message}. ধন্যবাদ।'],
        ['non_musking_rate_limit', '60'],
        ['non_musking_max_retries', '3']
    ];
    
    foreach ($default_settings as $setting) {
        $stmt->execute($setting);
    }
    
    echo "✓ Default non-masking settings added successfully\n";
    
    echo "\n✅ Non-Masking SMS setup completed successfully!\n";
    echo "\nNext steps:\n";
    echo "1. Configure non-masking settings in Admin Panel → Non-Masking Settings\n";
    echo "2. Test the connection\n";
    echo "3. Start sending non-masking SMS messages\n";

} catch (PDOException $e) {
    echo "❌ Error: " . $e->getMessage() . "\n";
    exit(1);
}
?>