<?php
// File: API_KONEKSI/promo/cek_promo.php
header('Content-Type: application/json');
include '../config/koneksi.php';

date_default_timezone_set('Asia/Jakarta');

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $kode_promo = $_POST['kode_promo'] ?? '';
    $total_belanja = isset($_POST['total_belanja']) ? (float) $_POST['total_belanja'] : 0;

    if (empty($kode_promo)) {
        echo json_encode(["status" => "gagal", "pesan" => "Kode promo kosong", "data" => []]);
        exit;
    }

    $current_date = date('Y-m-d');
    $current_time = date('H:i:s');

    // Ambil data promo
    $query = "SELECT promo_id, potongan_nominal, tipe_potongan, min_pembelian, maks_potongan, kuota_promo, total_terpakai, wajib_upload_st, mulai_tgl, selesai_tgl, mulai_jam, selesai_jam 
              FROM mst_promo 
              WHERE kode_promo = ? AND active_st = 1 AND deleted_st = 0 LIMIT 1";

    $stmt = $conn->prepare($query);
    $stmt->bind_param("s", $kode_promo);
    $stmt->execute();
    $result = $stmt->get_result();

    if ($row = $result->fetch_assoc()) {

        // 1. Validasi Tanggal
        if ($current_date < $row['mulai_tgl'] || $current_date > $row['selesai_tgl']) {
            echo json_encode(["status" => "gagal", "pesan" => "Promo tidak berlaku pada tanggal ini.", "data" => []]);
            exit;
        }

        // 2. Validasi Jam
        if ($current_time < $row['mulai_jam'] || $current_time > $row['selesai_jam']) {
            echo json_encode(["status" => "gagal", "pesan" => "Promo belum bisa digunakan pada jam ini.", "data" => []]);
            exit;
        }

        // 3. Validasi Minimal Belanja
        if ($total_belanja > 0 && $total_belanja < $row['min_pembelian']) {
            echo json_encode(["status" => "gagal", "pesan" => "Minimal transaksi untuk promo ini adalah Rp" . number_format($row['min_pembelian'], 0, ',', '.'), "data" => []]);
            exit;
        }

        // 4. Validasi Kuota
        // Asumsi: Jika kuota_promo > 0, maka ada batasan. Jika 0, maka unlimited.
        if ($row['kuota_promo'] > 0 && $row['total_terpakai'] >= $row['kuota_promo']) {
            echo json_encode(["status" => "gagal", "pesan" => "Maaf, kuota promo ini sudah habis digunakan.", "data" => []]);
            exit;
        }

        // Jika semua validasi lulus
        echo json_encode([
            "status" => "sukses",
            "pesan" => "Promo berhasil diterapkan!",
            "data" => [
                "promo_id" => $row['promo_id'],
                "potongan" => (double) $row['potongan_nominal'],
                "tipe_potongan" => $row['tipe_potongan'],
                "maks_potongan" => (double) $row['maks_potongan'],
                "wajib_upload" => (int) $row['wajib_upload_st']
            ]
        ]);
    } else {
        echo json_encode(["status" => "gagal", "pesan" => "Kode promo tidak ditemukan atau tidak aktif.", "data" => []]);
    }
    $stmt->close();
} else {
    echo json_encode(["status" => "gagal", "pesan" => "Metode tidak valid", "data" => []]);
}
$conn->close();
?>