<?php
require_once($_SERVER['DOCUMENT_ROOT'] . "/include/config.php");
require_once($_SERVER['DOCUMENT_ROOT'] . "/include/Function/sql.php");

$conn    = dbConnect();
$message = new USER();

$viesConn = "SELECT * FROM users WHERE acct_no=:acct_no";
$stmt = $conn->prepare($viesConn);
$stmt->execute([':acct_no' => $_SESSION['acct_no']]);
$row     = $stmt->fetch(PDO::FETCH_ASSOC);
$user_id = $row['id'];

$sql  = "SELECT * FROM settings WHERE id='1'";
$stmt = $conn->prepare($sql);
$stmt->execute();
$page = $stmt->fetch(PDO::FETCH_ASSOC);

$DomesticLimit = $page['domesticlimit'];
$TransferLimit = 50;

// ─── STEP 1: Form submit from domestic-transfer.php ───────────────────────────
if (isset($_POST['domestic-transfer'])) {

    $amount         = $_POST['amount'];
    $DomesticFee    = $_POST['fee'];
    $account_name   = $_POST['account_name'];
    $bank_name      = $_POST['bank_name'];
    $account_number = $_POST['account_number'];
    $account_type   = $_POST['account_type'];
    $bank_country   = $_POST['bank_country'];
    $description    = $_POST['description'];

    $checkFee = ($amount + $DomesticFee);

    if ($checkFee > $row['acct_balance']) {
        toast_alert('error', 'Insufficient Balance');
    } elseif ($row['acct_status'] == 'hold') {
        toast_alert('error', 'Account on Hold. Contact Support');
    } elseif ($amount > $DomesticLimit) {
        toast_alert('error', 'Transfer Limit Exceeded!');
    } elseif ($amount < $TransferLimit) {
        toast_alert('error', 'Amount Too Low!');
    } else {

        // Save to temp_trans
        $refrence_id      = uniqid();
        $trans_type       = "Domestic transfer";
        $transaction_type = "debit";
        $trans_status     = "pending";

        // Set UK Time
        date_default_timezone_set('Europe/London');
        $created_at = date('Y-m-d H:i:s');

        $sql = "INSERT INTO temp_trans 
                    (amount, refrence_id, user_id, bank_name, account_name, account_number, account_type, bank_country, trans_type, transaction_type, trans_status, description, created_at) 
                VALUES 
                    (:amount, :refrence_id, :user_id, :bank_name, :account_name, :account_number, :account_type, :bank_country, :trans_type, :transaction_type, :trans_status, :description, :created_at)";
        $tranfered = $conn->prepare($sql);
        $tranfered->execute([
            'amount'           => $amount,
            'refrence_id'      => $refrence_id,
            'user_id'          => $user_id,
            'bank_name'        => $bank_name,
            'account_name'     => $account_name,
            'account_number'   => $account_number,
            'account_type'     => $account_type,
            'bank_country'     => $bank_country,
            'trans_type'       => $trans_type,
            'transaction_type' => $transaction_type,
            'trans_status'     => $trans_status,
            'description'      => $description,
            'created_at'       => $created_at
        ]);

        // Force-update created_at to override MySQL DEFAULT CURRENT_TIMESTAMP
        $lastId = $conn->lastInsertId();
        $conn->prepare("UPDATE temp_trans SET created_at = :val WHERE trans_id = :tid")
             ->execute(['val' => $created_at, 'tid' => $lastId]);

        // Generate and send OTP
        $acct_otp = substr(number_format(time() * rand(), 0, '', ''), 0, 4);

        $sql = "UPDATE users SET acct_otp=:acct_otp WHERE id=:id";
        $stmt = $conn->prepare($sql);
        $stmt->execute(['acct_otp' => $acct_otp, 'id' => $user_id]);

        // Fetch fresh user row for email/phone
        $sql = "SELECT * FROM users WHERE id=:id";
        $stmt = $conn->prepare($sql);
        $stmt->execute(['id' => $user_id]);
        $resultCode = $stmt->fetch(PDO::FETCH_ASSOC);
        $acct_otp   = $resultCode['acct_otp'];
        $number     = $resultCode['acct_phone'];

        // SMS (if Twilio enabled)
        if ($page['twillio_status'] == '1') {
            $APP_NAME    = WEB_TITLE;
            $messageText = "Dear " . $resultCode['firstname'] . " You just made a Transaction of " . $amount . " in Your " . $APP_NAME . " Account. Kindly use this code " . $acct_otp . " to complete your Transaction. Thanks";
            $sendSms->sendSmsCode($number, $messageText);
        }

        // Email OTP
        $full_name    = $row['firstname'] . " " . $row['lastname'];
        $APP_NAME     = WEB_TITLE;
        $APP_URL      = WEB_URL;
        $SITE_ADDRESS = $page['url_address'];
        $email        = $row['acct_email'];

        $emailBody = $sendMail->pinRequest($full_name, $acct_otp, $APP_NAME, $APP_URL, $SITE_ADDRESS);
        $subject   = "One-Time Code - $APP_NAME";
        $email_message->send_mail($email, $emailBody, $subject);

        $_SESSION['is_dom_code']     = "None";
        $_SESSION['is_dom_transfer'] = "Dom";
        $_SESSION['is_transfer']     = "None";

        header("Location:./dom-pin.php");
        exit;
    }
}

// ─── STEP 2: kept for backward compatibility (domestic-preview.php fallback) ──
if (isset($_POST['domestic-preview'])) {

    $acct_otp = substr(number_format(time() * rand(), 0, '', ''), 0, 4);

    $sql = "UPDATE users SET acct_otp=:acct_otp WHERE id=:id";
    $stmt = $conn->prepare($sql);
    $stmt->execute(['acct_otp' => $acct_otp, 'id' => $user_id]);

    $sql = "SELECT * FROM users WHERE id=:id";
    $stmt = $conn->prepare($sql);
    $stmt->execute(['id' => $user_id]);
    $resultCode = $stmt->fetch(PDO::FETCH_ASSOC);
    $acct_otp   = $resultCode['acct_otp'];
    $number     = $resultCode['acct_phone'];

    if ($page['twillio_status'] == '1') {
        $APP_NAME    = WEB_TITLE;
        $messageText = "Dear " . $resultCode['firstname'] . " You just made a Transaction of $" . $amount . " in Your " . $APP_NAME . " Account. Kindly use this code " . $acct_otp . " to complete your Transaction. Thanks";
        $sendSms->sendSmsCode($number, $messageText);
    }

    $full_name    = $row['firstname'] . " " . $row['lastname'];
    $APP_NAME     = WEB_TITLE;
    $APP_URL      = WEB_URL;
    $SITE_ADDRESS = $page['url_address'];
    $email        = $row['acct_email'];

    $emailBody = $sendMail->pinRequest($full_name, $acct_otp, $APP_NAME, $APP_URL, $SITE_ADDRESS);
    $subject   = "One-Time Code - $APP_NAME";
    $email_message->send_mail($email, $emailBody, $subject);

    $_SESSION['is_dom_code']     = "None";
    $_SESSION['is_dom_transfer'] = "Dom";
    $_SESSION['is_transfer']     = "None";

    header("Location:./dom-pin.php");
    exit;
}