<?php
/**
 * Run this file once to create the WhatsApp tables
 * URL: http://dashboard-update.test/swabhimanmfiADMIN/create_whatsapp_table.php
 */

session_start();
include 'include.php';

echo "<div style='font-family: Arial; padding: 20px; margin: 20px;'>";
echo "<h1>WhatsApp Tables Setup</h1>";

// Table 1: WhatsApp Settings
$sql1 = "CREATE TABLE IF NOT EXISTS `whatsapp_settings` (
    `id` int(11) NOT NULL AUTO_INCREMENT,
    `api_key` varchar(255) NOT NULL,
    `sender_id` varchar(50) NOT NULL,
    `default_footer` varchar(255) DEFAULT NULL,
    `is_active` tinyint(1) DEFAULT 1,
    `created_at` datetime DEFAULT NULL,
    `updated_at` datetime DEFAULT NULL,
    PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;";

// Table 2: WhatsApp Templates
$sql2 = "CREATE TABLE IF NOT EXISTS `whatsapp_templates` (
    `id` int(11) NOT NULL AUTO_INCREMENT,
    `template_name` varchar(100) NOT NULL,
    `template_key` varchar(50) NOT NULL UNIQUE,
    `template_message` text NOT NULL,
    `is_active` tinyint(1) DEFAULT 1,
    `created_at` datetime DEFAULT NULL,
    `updated_at` datetime DEFAULT NULL,
    PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;";

try {
    // Create settings table
    setXbyY($sql1);
    echo "<p style='color: green;'>✓ whatsapp_settings table created</p>";

    // Create templates table
    setXbyY($sql2);
    echo "<p style='color: green;'>✓ whatsapp_templates table created</p>";

    // Insert default templates
    $templates = [
        ['Registration', 'registration', 'Welcome to {site_name}!\n\nYour account has been created successfully.\n\nLogin URL: {login_url}\nUsername: {username}\nPassword: {password}\n\nThank you for joining us!'],
        ['Registration OTP', 'registration_otp', 'Your OTP for registration on {site_name} is: {otp}\n\nThis OTP is valid for 10 minutes.\n\nDo not share this OTP with anyone.'],
        ['Forgot Password', 'forgot_password', 'Your OTP for password reset on {site_name} is: {otp}\n\nIf you did not request this, please ignore this message.'],
        ['Forgot PIN', 'forgot_pin', 'Your OTP for PIN reset on {site_name} is: {otp}\n\nIf you did not request this, please ignore this message.'],
        ['Change Password', 'change_password', 'Your password has been changed successfully on {site_name}.\n\nIf you did not make this change, please contact support immediately.'],
        ['Recharge Pending', 'recharge_pending', 'Your recharge of Rs.{amount} to {number} is PENDING.\n\nTransaction ID: {txn_id}\nOperator: {operator}\n\nPlease wait for confirmation.'],
        ['Recharge Success', 'recharge_success', 'Your recharge of Rs.{amount} to {number} is SUCCESSFUL!\n\nTransaction ID: {txn_id}\nOperator: {operator}\n\nThank you for using {site_name}!'],
        ['Recharge Refund', 'recharge_refund', 'Your recharge of Rs.{amount} to {number} has been REFUNDED.\n\nTransaction ID: {txn_id}\nRefund Amount: Rs.{amount}\n\nAmount credited to your wallet.'],
        ['Credit Balance', 'credit_balance', 'Rs.{amount} has been CREDITED to your {site_name} wallet.\n\nTransaction ID: {txn_id}\nNew Balance: Rs.{balance}\n\nThank you!'],
        ['Debit Balance', 'debit_balance', 'Rs.{amount} has been DEBITED from your {site_name} wallet.\n\nTransaction ID: {txn_id}\nNew Balance: Rs.{balance}'],
        ['Payment Request Accept', 'paymentreq_accept', 'Your payment request of Rs.{amount} has been APPROVED!\n\nTransaction ID: {txn_id}\nAmount credited to your wallet.\n\nThank you!'],
        ['Payment Request Reject', 'paymentreq_reject', 'Your payment request of Rs.{amount} has been REJECTED.\n\nTransaction ID: {txn_id}\nReason: {reason}\n\nPlease contact support for more details.'],
        ['Login OTP', 'login_otp', 'Your OTP for login on {site_name} is: {otp}\n\nIf this was not you, someone is trying to login to your account.']
    ];

    $inserted = 0;
    foreach($templates as $t) {
        $check = "SELECT id FROM whatsapp_templates WHERE template_key = '".$t[1]."'";
        $res = getXbyY($check);
        if(count($res) == 0) {
            $insert = "INSERT INTO whatsapp_templates (template_name, template_key, template_message, is_active, created_at)
                       VALUES ('".$t[0]."', '".$t[1]."', '".$t[2]."', 1, NOW())";
            setXbyY($insert);
            $inserted++;
        }
    }
    echo "<p style='color: green;'>✓ $inserted default templates inserted</p>";

    echo "<div style='background: #d4edda; border: 1px solid #c3e6cb; border-radius: 5px; padding: 15px; margin-top: 20px;'>";
    echo "<h2 style='color: #155724;'>Setup Complete!</h2>";
    echo "<p><a href='whatsapp_settings.php' style='color: #155724;'>→ WhatsApp API Settings</a></p>";
    echo "<p><a href='whatsapp_templates.php' style='color: #155724;'>→ WhatsApp Templates</a></p>";
    echo "</div>";

} catch (Exception $e) {
    echo "<div style='background: #f8d7da; border: 1px solid #f5c6cb; border-radius: 5px; padding: 15px;'>";
    echo "<h2 style='color: #721c24;'>Error</h2>";
    echo "<p>" . $e->getMessage() . "</p>";
    echo "</div>";
}

echo "<br><br><b style='color:red;'>DELETE THIS FILE AFTER USE!</b>";
echo "</div>";
?>
