<?php
set_time_limit(0); 
require 'config.php';

echo "<body style='font-family:sans-serif; background:#f4f4f4; padding:20px;'>";
echo "<div style='background:white; padding:30px; border-radius:20px; box-shadow:0 10px 15px -3px rgba(0,0,0,0.1);'>";
echo "<h2 style='color:#4f46e5;'>🚀 Generator Data Dummy: Oktober 2025 - Maret 2026</h2>";

// Hapus data lama agar tidak menumpuk jadi miliaran
$pdo->exec("TRUNCATE TABLE transactions");
echo "<p style='color:orange;'>⚠️ Database telah dikosongkan (Truncated).</p>";

// 1. Ambil produk untuk simulasi belanja
$products = $pdo->query("SELECT * FROM products WHERE category IN ('food','drink')")->fetchAll();
if (empty($products)) {
    die("<b style='color:red;'>❌ Isi produk dulu di menu admin!</b>");
}

// 2. Setting Rentang Waktu
$months = [
    '2025-10' => 31,
    '2025-11' => 30,
    '2025-12' => 31,
    '2026-01' => 31,
    '2026-02' => 28,
    '2026-03' => 31
];

$grand_total_omzet = 0;

foreach ($months as $bulan => $max_hari) {
    list($tahun, $bln) = explode('-', $bulan);
    $omzet_bulan_ini = 0;
    $jumlah_trx_bulan_ini = 0;

    for ($tgl = 1; $tgl <= $max_hari; $tgl++) {
        $date_string = "$tahun-$bln-" . str_pad($tgl, 2, '0', STR_PAD_LEFT);
        $day_name = date('l', strtotime($date_string));

        // Senin LIBUR
        if ($day_name == 'Monday') continue;

        // Target omzet harian agar total sebulan Rp 70jt - 80jt (sekitar 2.5jt - 3jt per hari)
        $target_harian = rand(2500000, 3500000);
        $current_harian = 0;

        while ($current_harian < $target_harian) {
            // Simulasi Item Belanja
            $cart = [];
            $subtotal = 0;
            $item_count = rand(1, 4);
            for ($i = 0; $i < $item_count; $i++) {
                $p = $products[array_rand($products)];
                $qty = rand(1, 2);
                $cart[] = [
                    'id' => $p['id'],
                    'name' => $p['name'],
                    'price' => (int)$p['price'],
                    'qty' => $qty
                ];
                $subtotal += ($p['price'] * $qty);
            }

            // Hitung Biaya Tambahan
            $order_type = (rand(1, 10) > 7) ? (rand(1, 2) == 1 ? 'takeaway' : 'catering') : 'dinein';
            $discount = (rand(1, 10) > 8) ? 10000 : 0;
            $service = round($subtotal * 0.15);
            $fee = ($order_type === 'takeaway') ? round($subtotal * 0.05) : ($order_type === 'catering' ? round($subtotal * 0.10) : 0);
            $total = ($subtotal - $discount) + $service + $fee;

            // Jam acak antara 09:00 - 22:00
            $jam = str_pad(rand(9, 22), 2, '0', STR_PAD_LEFT);
            $menit = str_pad(rand(0, 59), 2, '0', STR_PAD_LEFT);
            $detik = str_pad(rand(0, 59), 2, '0', STR_PAD_LEFT);
            $created_at = "$date_string $jam:$menit:$detik";

            $invoice = "INV-" . date('YmdHis', strtotime($created_at)) . rand(10, 99);

            // Simpan
            $stmt = $pdo->prepare("INSERT INTO transactions 
                (invoice, user_id, customer_name, order_type, subtotal, discount, service_charge, takeaway_fee, total, cart, status, created_at) 
                VALUES (?, 1, 'Umum', ?, ?, ?, ?, ?, ?, ?, 'paid', ?)");
            
            $stmt->execute([
                $invoice, $order_type, $subtotal, $discount, $service, $fee, (int)$total, json_encode($cart), $created_at
            ]);

            $current_harian += $total;
            $omzet_bulan_ini += $total;
            $jumlah_trx_bulan_ini++;
        }
    }
    
    $grand_total_omzet += $omzet_bulan_ini;
    echo "✅ <b>$bulan</b>: " . number_format($jumlah_trx_bulan_ini) . " Trx | <span style='color:green;'>Omzet: Rp " . number_format($omzet_bulan_ini) . "</span><br>";
}

echo "<hr><h3 style='color:#4f46e5;'>Total Omzet 6 Bulan: Rp " . number_format($grand_total_omzet) . "</h3>";
echo "<a href='transactions.php' style='display:inline-block; background:#4f46e5; color:white; padding:10px 20px; border-radius:10px; text-decoration:none; margin-top:10px;'>Cek Riwayat Transaksi</a>";
echo "</div></body>";