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

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $reservasi_id = $_POST['reservasi_id'] ?? '';
    $gdrive_link = $_POST['gdrive_link'] ?? '';
    $admin_name = $_POST['admin_name'] ?? 'Admin'; 
    
    if (empty($reservasi_id) || empty($gdrive_link)) {
        echo json_encode(["status" => "gagal", "pesan" => "ID Reservasi dan Link GDrive wajib diisi", "data" => []]);
        exit;
    }

    $cek_query = "SELECT distribusi_id FROM dat_distribusi WHERE reservasi_id = ? AND deleted_st = 0";
    $stmt_cek = $conn->prepare($cek_query);
    $stmt_cek->bind_param("s", $reservasi_id);
    $stmt_cek->execute();
    $cek_result = $stmt_cek->get_result();
    
    $is_success = false;
    $pesan_response = "";

    if ($cek_result->num_rows > 0) {
        $update_query = "UPDATE dat_distribusi SET gdrive_link = ?, updated_by = ?, updated_at = CURRENT_TIMESTAMP WHERE reservasi_id = ?";
        $stmt_update = $conn->prepare($update_query);
        $stmt_update->bind_param("sss", $gdrive_link, $admin_name, $reservasi_id);
        if ($stmt_update->execute()) {
            $is_success = true;
            $pesan_response = "Link berhasil diperbarui";
        }
        $stmt_update->close();
    } else {
        $distribusi_id = generateDistribusiId($conn);
        $insert_query = "INSERT INTO dat_distribusi (distribusi_id, reservasi_id, gdrive_link, akses_st, created_by) VALUES (?, ?, ?, 'Active', ?)";
        $stmt_insert = $conn->prepare($insert_query);
        $stmt_insert->bind_param("ssss", $distribusi_id, $reservasi_id, $gdrive_link, $admin_name);
        if ($stmt_insert->execute()) {
            $is_success = true;
            $pesan_response = "Link berhasil dikirim ke pelanggan";
        }
        $stmt_insert->close();
    }
    $stmt_cek->close();

    // --- LOGIKA NOTIFIKASI KE PELANGGAN JIKA SUKSES ---
    if ($is_success) {
        $q_user = $conn->prepare("SELECT r.user_id, p.paket_nm, u.role_nm FROM dat_reservasi r JOIN mst_paket p ON r.paket_id = p.paket_id JOIN mst_user u ON r.user_id = u.user_id WHERE r.reservasi_id = ?");
        $q_user->bind_param("s", $reservasi_id);
        $q_user->execute();
        $r_user = $q_user->get_result();

        if ($u_row = $r_user->fetch_assoc()) {
            if ($u_row['role_nm'] === 'Pelanggan') {
                $target_user = $u_row['user_id'];
                $paket_nm = $u_row['paket_nm'];
                $tipe_notif = "distribusi";
                
                $judul_notif = "Hasil Foto Udah Siap! 📸";
                $pesan_notif = "Yeay! Hasil jepretan paket " . $paket_nm . " kamu udah bisa diunduh nih. Cek link GDrive-nya sekarang ya!";

                $notif_id = generateNotifId($conn);
                $stmt_notif = $conn->prepare("INSERT INTO dat_notifikasi (notif_id, user_id, tipe_notif, target_id, judul_tx, pesan_tx) VALUES (?, ?, ?, ?, ?, ?)");
                $stmt_notif->bind_param("ssssss", $notif_id, $target_user, $tipe_notif, $reservasi_id, $judul_notif, $pesan_notif);
                $stmt_notif->execute();
                $stmt_notif->close();
            }
        }
        $q_user->close();
        
        echo json_encode(["status" => "sukses", "pesan" => $pesan_response, "data" => []]);
    } else {
        echo json_encode(["status" => "gagal", "pesan" => "Gagal memproses link", "data" => []]);
    }
    // --------------------------------------------------

} else {
    echo json_encode(["status" => "gagal", "pesan" => "Method tidak diizinkan", "data" => []]);
}
$conn->close();
?>