<?php
// /public_html/erp.jewellersbd.com/api/gold/set_rate.php

require_once __DIR__ . '/../../config/session.php';
require_once __DIR__ . '/../../config/db.php';
require_once __DIR__ . '/../../config/response.php';

if (!is_logged_in()) {
    send_response(401, false, 'লগইন প্রয়োজন', null);
}

if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
    send_response(405, false, 'শুধু POST অনুরোধ গ্রহণযোগ্য', null);
}

$data = json_decode(file_get_contents('php://input'), true);

$rate_date = $data['rate_date'] ?? date('Y-m-d');
$rate_24k  = $data['rate_24k'] ?? null;
$rate_22k  = $data['rate_22k'] ?? null;
$rate_21k  = $data['rate_21k'] ?? null;
$rate_18k  = $data['rate_18k'] ?? null;

if ($rate_24k === null || $rate_22k === null || $rate_21k === null || $rate_18k === null) {
    send_response(400, false, 'সব ক্যারেটের রেট দিন', null);
}

$rate_24k = (float)$rate_24k;
$rate_22k = (float)$rate_22k;
$rate_21k = (float)$rate_21k;
$rate_18k = (float)$rate_18k;

if ($rate_24k <= 0 || $rate_22k <= 0 || $rate_21k <= 0 || $rate_18k <= 0) {
    send_response(400, false, 'সব রেট ০ এর বেশি হতে হবে', null);
}

$vori = 11.664;

$rate_24k_gram = round($rate_24k / $vori, 4);
$rate_22k_gram = round($rate_22k / $vori, 4);
$rate_21k_gram = round($rate_21k / $vori, 4);
$rate_18k_gram = round($rate_18k / $vori, 4);

$created_by = (int)($_SESSION['user_id'] ?? 0);

try {
    $check = $pdo->prepare("
        SELECT id
        FROM gold_rates
        WHERE rate_date = ?
        LIMIT 1
    ");
    $check->execute([$rate_date]);
    $existing = $check->fetch(PDO::FETCH_ASSOC);

    if ($existing) {
        $stmt = $pdo->prepare("
            UPDATE gold_rates
            SET
                rate_24k = ?,
                rate_22k = ?,
                rate_21k = ?,
                rate_18k = ?
            WHERE rate_date = ?
        ");
        $stmt->execute([
            $rate_24k_gram,
            $rate_22k_gram,
            $rate_21k_gram,
            $rate_18k_gram,
            $rate_date
        ]);
    } else {
        $stmt = $pdo->prepare("
            INSERT INTO gold_rates
            (
                rate_date,
                rate_24k,
                rate_22k,
                rate_21k,
                rate_18k,
                created_by
            )
            VALUES (?, ?, ?, ?, ?, ?)
        ");
        $stmt->execute([
            $rate_date,
            $rate_24k_gram,
            $rate_22k_gram,
            $rate_21k_gram,
            $rate_18k_gram,
            $created_by
        ]);
    }

    send_response(200, true, 'সোনার রেট সফলভাবে সেভ হয়েছে', [
        'rate_date' => $rate_date,
        'rate_24k_vori' => $rate_24k,
        'rate_22k_vori' => $rate_22k,
        'rate_21k_vori' => $rate_21k,
        'rate_18k_vori' => $rate_18k,
        'rate_24k_gram' => $rate_24k_gram,
        'rate_22k_gram' => $rate_22k_gram,
        'rate_21k_gram' => $rate_21k_gram,
        'rate_18k_gram' => $rate_18k_gram
    ]);

} catch (Exception $e) {
    send_response(500, false, 'সার্ভার সমস্যা', [
        'error' => $e->getMessage()
    ]);
}