<?php
// admin/save-sms-config.php - Save SMS Configuration
session_start();
require_once '../includes/config.php';

if(!isset($_SESSION['user_id']) || $_SESSION['user_role'] != 'admin') {
    header('Location: ../auth.php');
    exit;
}

if($_SERVER['REQUEST_METHOD'] == 'POST') {
    $api_key = trim($_POST['api_key'] ?? '');
    $sender_id = trim($_POST['sender_id'] ?? '');
    $default_template = trim($_POST['default_template'] ?? '');
    $enable_otp = isset($_POST['enable_otp']) ? 1 : 0;
    $enable_purchase = isset($_POST['enable_purchase_sms']) ? 1 : 0;
    
    // Update sms_config.php file
    $config_file = '../includes/sms_config.php';
    
    $config_content = "<?php
// includes/sms_config.php - BulkSMSBD Configuration

class BulkSMSBD {
    public \$api_key = '" . addslashes($api_key) . "';
    public \$sender_id = '" . addslashes($sender_id) . "';
    public \$default_template = '" . addslashes($default_template) . "';
    public \$enable_otp = " . ($enable_otp ? 'true' : 'false') . ";
    public \$enable_purchase = " . ($enable_purchase ? 'true' : 'false') . ";
    
    public function sendSMS(\$phone, \$message) {
        if(empty(\$this->api_key)) return false;
        
        \$phone = \$this->formatPhone(\$phone);
        
        \$data = [
            'api_key' => \$this->api_key,
            'senderid' => \$this->sender_id,
            'number' => \$phone,
            'message' => \$message
        ];
        
        \$curl = curl_init('https://bulksmsbd.net/api/smsapi');
        curl_setopt_array(\$curl, [
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_POST => true,
            CURLOPT_POSTFIELDS => http_build_query(\$data),
            CURLOPT_TIMEOUT => 30,
        ]);
        
        \$response = curl_exec(\$curl);
        curl_close(\$curl);
        return \$response;
    }
    
    public function sendOTP(\$phone, \$otp) {
        \$message = \"আপনার OTP: \$otp\\n৫ মিনিটের মধ্যে ব্যবহার করুন।\\n- বাংলা LMS\";
        return \$this->sendSMS(\$phone, \$message);
    }
    
    public function sendPurchaseConfirmation(\$phone, \$details) {
        \$message = \"অভিনন্দন! এনরোলমেন্ট সফল!\\nকোর্স: {\$details['name']}\\nমূল্য: ৳\" . number_format(\$details['amount']) . \"\\n- বাংলা LMS\";
        return \$this->sendSMS(\$phone, \$message);
    }
    
    public function sendWelcomeSMS(\$phone, \$name) {
        \$message = \"স্বাগতম \$name!\\nবাংলা LMS এ আপনার অ্যাকাউন্ট তৈরি হয়েছে।\\n- বাংলা LMS\";
        return \$this->sendSMS(\$phone, \$message);
    }
    
    private function formatPhone(\$phone) {
        \$phone = preg_replace('/[^0-9]/', '', \$phone);
        if(strlen(\$phone) == 11 && substr(\$phone, 0, 2) == '01') {
            return '88' . \$phone;
        }
        return \$phone;
    }
}

\$sms = new BulkSMSBD();
?>";
    
    if(file_put_contents($config_file, $config_content)) {
        $_SESSION['sms_config_saved'] = true;
    }
    
    header('Location: dashboard.php?tab=sms&msg=SMS+কনফিগারেশন+সংরক্ষিত+হয়েছে&type=success');
    exit;
}