<?php
include '_common.php';
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
ini_set('log_errors', 1);

// 입력 데이터 검증
$required_fields = ['cpu', 'gpu', 'ram', 'ram_type', 'ssd', 'ssd_type', 'hdd', 'hdd_type'];
$missing_fields = [];

foreach ($required_fields as $field) {
    if (!isset($_POST[$field]) || trim($_POST[$field]) === '') {
        $missing_fields[] = $field;
    }
}

if (!empty($missing_fields)) {
    echo json_encode(['error' => 'All fields are required', 'missing_fields' => $missing_fields]);
    exit;
}

// 데이터 처리
$cpu = $_POST['cpu'];
$gpu = $_POST['gpu'];
$ram = intval($_POST['ram']);
$ram_type = $_POST['ram_type'] != "" ? $_POST['ram_type'] : "DDR3";
$ssd_input = trim($_POST['ssd']);
if ($ssd_input === '0' || $ssd_input === '') {
    $ssd = 0;
} else {
    // "GB" 또는 "TB" 제거 및 숫자 추출
    $ssd_value = preg_replace('/[^0-9.]/', '', $ssd_input);

    if (stripos($ssd_input, 'TB') !== false) {
        // TB인 경우 1000을 곱함
        $ssd = intval(floatval($ssd_value) * 1000);
    } else {
        // GB인 경우 또는 단위가 없는 경우
        $ssd = intval($ssd_value);
    }
}
$ssd_type = $_POST['ssd_type'] != "" ? $_POST['ssd_type'] : "SSD";

// HDD 용량 처리
$hdd_input = trim($_POST['hdd']);
if ($hdd_input === '0' || $hdd_input === '') {
    $hdd = 0;
} else {
    // "GB" 또는 "TB" 제거 및 숫자 추출
    $hdd_value = preg_replace('/[^0-9.]/', '', $hdd_input);

    if (stripos($hdd_input, 'TB') !== false) {
        // TB인 경우 1000을 곱함
        $hdd = intval(floatval($hdd_value) * 1000);
    } else {
        // GB인 경우 또는 단위가 없는 경우
        $hdd = intval($hdd_value);
    }
}

$hdd_type = $_POST['hdd_type'] != "" ? $_POST['hdd_type'] : "HDD";

// CPU 가격 계산
$cpu_query = "SELECT cpu_name, price FROM api___ai_suggested_price_of_cpu WHERE cpu_name LIKE ?";
$stmt = $g5['connect_db']->prepare($cpu_query);
$cpu_param = "%$cpu%";
$stmt->bind_param("s", $cpu_param);
$stmt->execute();
$cpu_result = $stmt->get_result();

if ($cpu_result->num_rows > 0) {
    $cpu_row = $cpu_result->fetch_assoc();
    $cpu_price = $cpu_row['price'];
} else {
    $cpu_price = 0;
}

// GPU 가격 계산
$gpu_query = "SELECT videocard_name, price FROM api___ai_suggested_price_of_gpu WHERE videocard_name LIKE ?";
$stmt = $g5['connect_db']->prepare($gpu_query);
$gpu_param = "%$gpu%";
$stmt->bind_param("s", $gpu_param);
$stmt->execute();
$gpu_result = $stmt->get_result();

if ($gpu_result->num_rows > 0) {
    $gpu_row = $gpu_result->fetch_assoc();
    $gpu_price = $gpu_row['price'];
} else {
    $gpu_price = 0;
}

// RAM 가격 계산
$ram_query = "SELECT price FROM api___ai_suggested_price_of_ram WHERE ramtype = ? AND memory_size = 1";
$stmt = $g5['connect_db']->prepare($ram_query);
$ram_type_avg = $ram_type . " AVG";
$stmt->bind_param("s", $ram_type_avg);
$stmt->execute();
$ram_result = $stmt->get_result();

if ($ram_result->num_rows > 0) {
    $ram_row = $ram_result->fetch_assoc();
    $ram_price = $ram_row['price'] * $ram;
} else {
    $ram_price = 0;
}

// HDD 가격 계산
$hdd_query = "SELECT price FROM api___ai_suggested_price_of_hdd WHERE storage_type = ? AND size = 1";
$stmt = $g5['connect_db']->prepare($hdd_query);
$hdd_type_avg = "HDD " . $hdd_type . " AVG";
$stmt->bind_param("s", $hdd_type_avg);
$stmt->execute();
$hdd_result = $stmt->get_result();

if ($hdd_result->num_rows > 0) {
    $hdd_row = $hdd_result->fetch_assoc();
    $hdd_price = $hdd_row['price'] * $hdd;
} else {
    $hdd_price = 0;
}

// SSD 가격 계산
$ssd_query = "SELECT price FROM api___ai_suggested_price_of_ssd WHERE storage_type LIKE ? AND size = 1";
$stmt = $g5['connect_db']->prepare($ssd_query);

// SSD 타입 처리
$ssd_type_avg = "SSD " . $ssd_type . " AVG";

$ssd_type_param = $ssd_type_avg . "%";
$stmt->bind_param("s", $ssd_type_param);
$stmt->execute();
$ssd_result = $stmt->get_result();

if ($ssd_result->num_rows > 0) {
    $ssd_row = $ssd_result->fetch_assoc();
    $ssd_price = $ssd_row['price'] * $ssd;
} else {
    $ssd_price = 0;
}


// 총 가격 계산
$total_price = $cpu_price + $gpu_price + $ram_price + $hdd_price + $ssd_price;

// 결과 반환
$result = [
    'cpu_price' => $cpu_price,
    'gpu_price' => $gpu_price,
    'ram_price' => $ram_price,
    'hdd_price' => $hdd_price,
    'ssd_price' => $ssd_price,
    'total_price' => $total_price,
    'daily_price' => $total_price / 30 / 20  # 1일 요금
];

echo json_encode($result);

$g5['connect_db']->close();


?>
