<?php
session_start();
include 'config.php';

if(!isset($_SESSION['user_id'])) exit();

$user_id = $_SESSION['user_id'];
$brand_id = $_POST['brand_id'];
$model_name = $_POST['model_name'];
$ram = $_POST['ram'];
$rom = $_POST['rom'];

$brand_data = $conn->query("SELECT * FROM brands WHERE id=$brand_id")->fetch_assoc();
$brand_name = strtoupper($brand_data['name']);
$price = $brand_data['price'];

$user = $conn->query("SELECT balance FROM users WHERE id=$user_id")->fetch_assoc();
if($user['balance'] < $price) die("<script>alert('Low Balance'); window.location.href='dashboard.php';</script>");

// আইফোন বনাম অ্যান্ড্রয়েড লজিক
if($brand_name == 'IPHONE') {
    $dpi = "Not Required (Use Display Zoom)";
    $fire_button = rand(38, 42) . "%";
    $guide = "🎯 IPHONE HEADSHOT OPTIMIZATION:
    1️⃣ Go to Settings > Accessibility > Touch.
    2️⃣ Turn ON 'AssistiveTouch' and set 'Tracking Sensitivity' to MAX.
    3️⃣ Go to Settings > Display & Brightness > Display Zoom > Set to 'More Space'.
    4️⃣ In Settings > Accessibility > Touch > Touch Accommodations > Turn ON 'Ignore Repeat' (0.10s).";
} else {
    $dpi_val = 400 + ((int)$ram * 10) + rand(5, 20);
    $dpi = $dpi_val . " DPI";
    $fire_button = rand(45, 52) . "%";
    $guide = "🚀 ANDROID POWER CONFIG:
    1️⃣ Developer Options > Smallest Width > Set to $dpi.
    2️⃣ Pointer Speed > Set to 100%.
    3️⃣ Graphics Driver > Search Free Fire > Set to 'System Graphics Driver'.
    4️⃣ Background Process Limit > Set to 'At most 2 processes'.";
}

$sensi = "General: ".rand(98,100).", Red Dot: ".rand(94,97).", 2x Scope: 95, 4x Scope: 100";
$expiry = date('Y-m-d', strtotime('+30 days'));

// পেমেন্ট ও সেভ
$conn->query("UPDATE users SET balance = balance - $price WHERE id=$user_id");

$stmt = $conn->prepare("INSERT INTO purchases (user_id, product_name, amount, dpi, sensitivity, ram, rom, status, guide, expiry_date, fire_button, game_version) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
$full_device = "$brand_name $model_name";
$status = "active";
$version = "OB45 Premium";
$stmt->bind_param("isdsssssssss", $user_id, $full_device, $price, $dpi, $sensi, $ram, $rom, $status, $guide, $expiry, $fire_button, $version);

if($stmt->execute()){
    $_SESSION['last_purchase_id'] = $conn->insert_id;
    header("Location: result.php");
}
?>