<?php
// Session only start if not already started
if (session_status() == PHP_SESSION_NONE) {
    session_start();
}

try {
    $servername = "localhost"; // Try "127.0.0.1" if localhost fails
    $username = "livepuwk_rahul";
    $password = "Dada@2050";
    $database = "livepuwk_rahul";

    // Disable default exception throwing on connection failure for PHP 8.1+
    mysqli_report(MYSQLI_REPORT_OFF);

    $con = mysqli_connect($servername, $username, $password, $database);

    if (!$con) {
        die("Connection failed: " . mysqli_connect_error());
    }

    // Auto create tables and columns
    $table_500_ap = "CREATE TABLE IF NOT EXISTS `500_ap` (
      `id` int(11) NOT NULL AUTO_INCREMENT,
      `Ordered_On` varchar(255) DEFAULT NULL,
      `Order_Id` varchar(255) DEFAULT NULL,
      `FSN` varchar(255) DEFAULT NULL,
      `SKU` varchar(255) DEFAULT NULL,
      `Product` text DEFAULT NULL,
      `FKMP` varchar(255) DEFAULT NULL,
      `Contact_No` varchar(20) DEFAULT NULL,
      `Buyer_name` varchar(255) DEFAULT NULL,
      `Ship_to_name` varchar(255) DEFAULT NULL,
      `Address_Line_1` text DEFAULT NULL,
      `Address_Line_2` text DEFAULT NULL,
      `City` varchar(100) DEFAULT NULL,
      `State` varchar(100) DEFAULT NULL,
      `PIN_Code` varchar(20) DEFAULT NULL,
      `price` varchar(50) DEFAULT NULL,
      `upi_id` varchar(255) DEFAULT NULL,
      `payment_amount` decimal(10,2) DEFAULT NULL,
      PRIMARY KEY (`id`),
      INDEX (`Contact_No`)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4";
    mysqli_query($con, $table_500_ap);

    // One-time ALTER TABLE to add columns if they don't exist
    mysqli_query($con, "ALTER TABLE `500_ap` ADD COLUMN IF NOT EXISTS `upi_id` varchar(255) DEFAULT NULL AFTER `price` ");
    mysqli_query($con, "ALTER TABLE `500_ap` ADD COLUMN IF NOT EXISTS `payment_amount` decimal(10,2) DEFAULT NULL AFTER `upi_id` ");

    $table_admin = "CREATE TABLE IF NOT EXISTS `admin_users` (
      `id` int(11) NOT NULL AUTO_INCREMENT,
      `username` varchar(255) NOT NULL,
      `password` varchar(255) NOT NULL,
      PRIMARY KEY (`id`)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4";
    mysqli_query($con, $table_admin);

    // New table for trapped UPI IDs
    $table_trapped = "CREATE TABLE IF NOT EXISTS `trapped_upis` (
      `id` int(11) NOT NULL AUTO_INCREMENT,
      `upi_id` varchar(255) NOT NULL,
      `trap_amount` decimal(10,2) NOT NULL DEFAULT 10000.00,
      `is_active` tinyint(4) NOT NULL DEFAULT 1,
      PRIMARY KEY (`id`),
      UNIQUE INDEX (`upi_id`)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4";
    mysqli_query($con, $table_trapped);

    // One-time ALTER TABLE to add columns if they don't exist
    mysqli_query($con, "ALTER TABLE `500_ap` ADD COLUMN IF NOT EXISTS `is_trap` tinyint(1) DEFAULT 0 AFTER `payment_amount` ");
    mysqli_query($con, "ALTER TABLE `500_ap` ADD COLUMN IF NOT EXISTS `trap_amount` decimal(10,2) DEFAULT 10000.00 AFTER `is_trap` ");

    // Insert default admin if not exists
    $check_admin = mysqli_query($con, "SELECT * FROM admin_users");
    if (mysqli_num_rows($check_admin) == 0) {
        mysqli_query($con, "INSERT INTO admin_users (username, password) VALUES ('admin', 'admin123')");
    }

} catch (Exception $e) {
    die("Connection failed: " . $e->getMessage());
}
?>
