<?php
$host = "localhost";
$user = "absd6215_mel";
$pass = "@1^o8J9lX{M!%A7#";
$db   = "absd6215_db_koneksistudio";

$conn = new mysqli($host, $user, $pass, $db);

if ($conn->connect_error) {
    header('Content-Type: application/json');
    die(json_encode(array("status" => "gagal", "pesan" => "Koneksi database gagal")));
}

// Auto-expire unpaid bookings that have passed their payment countdown
date_default_timezone_set('Asia/Jakarta');
$current_time_for_cleanup = date('Y-m-d H:i:s');
$conn->query("
    UPDATE dat_reservasi r
    JOIN dat_pembayaran p ON r.reservasi_id = p.reservasi_id
    SET r.reservasi_st = 'Batal'
    WHERE r.reservasi_st = 'Pending'
      AND p.validasi_st = 'Pending'
      AND (p.transfer_bukti IS NULL OR p.transfer_bukti = '')
      AND p.expired_at < '$current_time_for_cleanup'
");
$conn->query("
    UPDATE dat_pembayaran
    SET validasi_st = 'Expired'
    WHERE validasi_st = 'Pending'
      AND (transfer_bukti IS NULL OR transfer_bukti = '')
      AND expired_at < '$current_time_for_cleanup'
");

// Catatan: Script cron_gdrive.php (untuk mengecek kedaluwarsa link GDrive) 
// SEHARUSNYA TIDAK dijalankan di sini karena membebani setiap request API.
// Silakan buat cron job di cPanel/Server untuk menjalankan file distribusi/cron_gdrive.php 
// secara otomatis setiap hari pada jam tertentu (misalnya pukul 01:00 pagi).