<?php
// booking_test.php - Booking kapal via CMD (PAKAI KONEKSI MANUAL)

// 🔥 KONEKSI DATABASE MANUAL
$host = 'localhost';
$dbname = 'platform_kapal_samosir';
$username = 'root';
$password = ''; // Kosongkan jika pakai Laragon default

try {
    $pdo = new PDO("mysql:host=$host;dbname=$dbname;charset=utf8mb4", $username, $password);
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
    die("Koneksi database gagal: " . $e->getMessage());
}

// 🔥 DATA BOOKING
$user_id = 11;
$kapal_id = 89;
$route_id = 47;
$tanggal = '2026-07-25';
$jumlah_penumpang = 40;
$total_harga = 1800000;

$nama_lengkap = 'Faustina Samosir';
$no_hp = '082173343353';
$nik_ktp = '1234567890123456';

$kode = 'BOOK-TEST-' . date('YmdHis');

echo "\n========================================\n";
echo "   PROSES BOOKING\n";
echo "========================================\n\n";

echo "Data Booking:\n";
echo "   Kode Pesanan    : $kode\n";
echo "   Wisatawan ID    : $user_id\n";
echo "   Kapal ID        : $kapal_id\n";
echo "   Rute ID         : $route_id\n";
echo "   Tanggal         : $tanggal\n";
echo "   Penumpang       : $jumlah_penumpang orang\n";
echo "   Total Harga     : Rp " . number_format($total_harga, 0, ',', '.') . "\n\n";

try {
    // Ambil nama kapal
    $stmt = $pdo->prepare("SELECT nama_kapal FROM kapal WHERE id = ?");
    $stmt->execute([$kapal_id]);
    $kapal = $stmt->fetch(PDO::FETCH_ASSOC);
    $nama_kapal = $kapal['nama_kapal'] ?? 'Kapal Wisata';

    $stmt = $pdo->prepare("
        INSERT INTO pesanan (
            user_id, kapal_id, route_id, kode_pesanan, 
            nama_kapal, tanggal_berangkat, durasi_jam, total_harga,
            status_pembayaran, status_pembayaran_admin, status_pesanan,
            jenis_booking, nama_lengkap, no_hp, nik_ktp, jumlah_tiket,
            metode_pembayaran, snap_token, created_at
        ) VALUES (
            ?, ?, ?, ?,
            ?, ?, 1, ?,
            'belum_bayar', 'menunggu_konfirmasi', 'pending',
            'private', ?, ?, ?, ?,
            'midtrans', NULL, NOW()
        )
    ");
    
    $stmt->execute([
        $user_id, $kapal_id, $route_id, $kode,
        $nama_kapal, $tanggal, $total_harga,
        $nama_lengkap, $no_hp, $nik_ktp, $jumlah_penumpang
    ]);
    
    $pesanan_id = $pdo->lastInsertId();
    
    echo "✅ BOOKING BERHASIL!\n";
    echo "   ID Pesanan     : $pesanan_id\n";
    echo "   Kode Pesanan   : $kode\n";
    echo "   Status         : pending / belum_bayar\n\n";
    
    echo "📌 NEXT STEP: Buat pembayaran\n";
    echo "   php create_payment.php $pesanan_id\n";
    
} catch (Exception $e) {
    echo "❌ ERROR: " . $e->getMessage() . "\n";
}

echo "========================================\n";