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

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $promo_id = $_POST['promo_id'] ?? '';
    $promo_nm = $_POST['promo_nm'] ?? '';
    $kode_promo = $_POST['kode_promo'] ?? '';
    $tipe_potongan = $_POST['tipe_potongan'] ?? 'nominal'; // 'nominal' atau 'persen'
    $potongan_nominal = $_POST['potongan_nominal'] ?? 0;
    $min_pembelian = $_POST['min_pembelian'] ?? 0;
    $maks_potongan = $_POST['maks_potongan'] ?? 0;
    $kuota_promo = $_POST['kuota_promo'] ?? 0;
    $mulai_tgl = $_POST['mulai_tgl'] ?? '';
    $selesai_tgl = $_POST['selesai_tgl'] ?? '';
    $mulai_jam = $_POST['mulai_jam'] ?? '00:00:00';
    $selesai_jam = $_POST['selesai_jam'] ?? '23:59:00';
    $wajib_upload_st = $_POST['wajib_upload_st'] ?? 0;
    $instruksi_syarat = $_POST['instruksi_syarat'] ?? '';
    $active_st = $_POST['active_st'] ?? 1;
    $payload = authenticateJWT();
    $user_id = $payload['user_id'] ?? '';

    $is_new_promo = empty($promo_id);

    if ($is_new_promo) {
        $promo_id = generatePromoId($conn);
        $stmt = $conn->prepare("INSERT INTO mst_promo (promo_id, promo_nm, kode_promo, tipe_potongan, potongan_nominal, min_pembelian, maks_potongan, kuota_promo, mulai_tgl, selesai_tgl, mulai_jam, selesai_jam, wajib_upload_st, instruksi_syarat, active_st, created_by) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
        $stmt->bind_param("ssssdddissssisss", $promo_id, $promo_nm, $kode_promo, $tipe_potongan, $potongan_nominal, $min_pembelian, $maks_potongan, $kuota_promo, $mulai_tgl, $selesai_tgl, $mulai_jam, $selesai_jam, $wajib_upload_st, $instruksi_syarat, $active_st, $user_id);
        $pesan = "Promo berhasil ditambahkan!";
    } else {
        $stmt = $conn->prepare("UPDATE mst_promo SET promo_nm=?, kode_promo=?, tipe_potongan=?, potongan_nominal=?, min_pembelian=?, maks_potongan=?, kuota_promo=?, mulai_tgl=?, selesai_tgl=?, mulai_jam=?, selesai_jam=?, wajib_upload_st=?, instruksi_syarat=?, active_st=?, updated_by=? WHERE promo_id=?");
        $stmt->bind_param("sssdddissssissss", $promo_nm, $kode_promo, $tipe_potongan, $potongan_nominal, $min_pembelian, $maks_potongan, $kuota_promo, $mulai_tgl, $selesai_tgl, $mulai_jam, $selesai_jam, $wajib_upload_st, $instruksi_syarat, $active_st, $user_id, $promo_id);
        $pesan = "Promo berhasil diperbarui!";
    }

    if ($stmt->execute()) {
        
        // --- LOGIKA NOTIFIKASI KHUSUS PROMO BARU ---
        if ($is_new_promo) {
            $new_promo_id = $promo_id;
            
            $judul_notif = "Promo Spesial: " . $promo_nm . " 🎁";
            $pesan_notif = "Koneksi Studio lagi ada promo nih! Pakai kode " . $kode_promo . " biar foto makin hemat.";
            $tipe_notif = "promo";
            
            // Broadcast ke semua pelanggan
            $query_pelanggan = "SELECT user_id FROM mst_user WHERE role_nm = 'Pelanggan' AND active_st = 1 AND deleted_st = 0";
            $res_pelanggan = $conn->query($query_pelanggan);
            if ($res_pelanggan && $res_pelanggan->num_rows > 0) {
                $stmt_notif = $conn->prepare("INSERT INTO dat_notifikasi (notif_id, user_id, tipe_notif, target_id, judul_tx, pesan_tx) VALUES (?, ?, ?, ?, ?, ?)");
                while ($row_pelanggan = $res_pelanggan->fetch_assoc()) {
                    $target_user = $row_pelanggan['user_id'];
                    $notif_id = generateNotifId($conn);
                    $stmt_notif->bind_param("ssssss", $notif_id, $target_user, $tipe_notif, $new_promo_id, $judul_notif, $pesan_notif);
                    $stmt_notif->execute();
                }
                $stmt_notif->close();
            }
        }
        // -------------------------------------------

        echo json_encode(["status" => "sukses", "pesan" => $pesan, "data" => []]);
    } else {
        echo json_encode(["status" => "gagal", "pesan" => "Gagal menyimpan data database", "data" => []]);
    }
    $stmt->close();
}
$conn->close();
?>
