<?php
// File: c:/laragon/www/api_koneksi/config/id_generator.php

function generateRandomHex($length = 8) {
    return strtoupper(substr(md5(uniqid(mt_rand(), true)), 0, $length));
}

function generateUserId($conn) {
    do {
        $id = "USR-" . generateRandomHex(8);
        $stmt = $conn->prepare("SELECT user_id FROM mst_user WHERE user_id = ?");
        $stmt->bind_param("s", $id);
        $stmt->execute();
        $stmt->store_result();
        $exists = $stmt->num_rows > 0;
        $stmt->close();
    } while ($exists);
    return $id;
}

function generatePromoId($conn) {
    do {
        $id = "PRM-" . generateRandomHex(8);
        $stmt = $conn->prepare("SELECT promo_id FROM mst_promo WHERE promo_id = ?");
        $stmt->bind_param("s", $id);
        $stmt->execute();
        $stmt->store_result();
        $exists = $stmt->num_rows > 0;
        $stmt->close();
    } while ($exists);
    return $id;
}

function generatePaketId($conn) {
    do {
        $id = "PKT-" . generateRandomHex(8);
        $stmt = $conn->prepare("SELECT paket_id FROM mst_paket WHERE paket_id = ?");
        $stmt->bind_param("s", $id);
        $stmt->execute();
        $stmt->store_result();
        $exists = $stmt->num_rows > 0;
        $stmt->close();
    } while ($exists);
    return $id;
}

function generateMetodeId($conn) {
    do {
        $id = "MET-" . generateRandomHex(8);
        $stmt = $conn->prepare("SELECT metode_id FROM mst_metode_bayar WHERE metode_id = ?");
        $stmt->bind_param("s", $id);
        $stmt->execute();
        $stmt->store_result();
        $exists = $stmt->num_rows > 0;
        $stmt->close();
    } while ($exists);
    return $id;
}

function generateReservasiId($conn) {
    $datePart = date('Ymd');
    $prefix = "INV-" . $datePart . "-";
    
    // Count existing reservations for today to get sequential sequence
    $stmt = $conn->prepare("SELECT COUNT(*) FROM dat_reservasi WHERE reservasi_id LIKE ?");
    $likePattern = $prefix . "%";
    $stmt->bind_param("s", $likePattern);
    $stmt->execute();
    $stmt->bind_result($count);
    $stmt->fetch();
    $stmt->close();
    
    $sequence = $count + 1;
    do {
        $id = $prefix . sprintf("%04d", $sequence);
        $stmt = $conn->prepare("SELECT reservasi_id FROM dat_reservasi WHERE reservasi_id = ?");
        $stmt->bind_param("s", $id);
        $stmt->execute();
        $stmt->store_result();
        $exists = $stmt->num_rows > 0;
        $stmt->close();
        $sequence++;
    } while ($exists);
    
    return $id;
}

function generatePembayaranId($conn) {
    $datePart = date('Ymd');
    $prefix = "PAY-" . $datePart . "-";
    
    $stmt = $conn->prepare("SELECT COUNT(*) FROM dat_pembayaran WHERE pembayaran_id LIKE ?");
    $likePattern = $prefix . "%";
    $stmt->bind_param("s", $likePattern);
    $stmt->execute();
    $stmt->bind_result($count);
    $stmt->fetch();
    $stmt->close();
    
    $sequence = $count + 1;
    do {
        $id = $prefix . sprintf("%04d", $sequence);
        $stmt = $conn->prepare("SELECT pembayaran_id FROM dat_pembayaran WHERE pembayaran_id = ?");
        $stmt->bind_param("s", $id);
        $stmt->execute();
        $stmt->store_result();
        $exists = $stmt->num_rows > 0;
        $stmt->close();
        $sequence++;
    } while ($exists);
    
    return $id;
}

function generateDistribusiId($conn) {
    do {
        $id = "DST-" . generateRandomHex(8);
        $stmt = $conn->prepare("SELECT distribusi_id FROM dat_distribusi WHERE distribusi_id = ?");
        $stmt->bind_param("s", $id);
        $stmt->execute();
        $stmt->store_result();
        $exists = $stmt->num_rows > 0;
        $stmt->close();
    } while ($exists);
    return $id;
}

function generateNotifId($conn) {
    do {
        $id = "NTF-" . generateRandomHex(8);
        $stmt = $conn->prepare("SELECT notif_id FROM dat_notifikasi WHERE notif_id = ?");
        $stmt->bind_param("s", $id);
        $stmt->execute();
        $stmt->store_result();
        $exists = $stmt->num_rows > 0;
        $stmt->close();
    } while ($exists);
    return $id;
}

function generateTutupId($conn) {
    do {
        $id = "TTP-" . generateRandomHex(8);
        $stmt = $conn->prepare("SELECT tutup_id FROM dat_jadwal_tutup WHERE tutup_id = ?");
        $stmt->bind_param("s", $id);
        $stmt->execute();
        $stmt->store_result();
        $exists = $stmt->num_rows > 0;
        $stmt->close();
    } while ($exists);
    return $id;
}
?>
