void main()
{
    // Get the player who is disturbing the inventory
    object oPlayer = GetLastDisturbed();
    SendMessageToPC(oPlayer, "Script started.");

    // Get the item that was disturbed
    object oDisturbedItem = GetInventoryDisturbItem();

    // Check if the disturbed item has a cost in tickets
    int iCostTickets = GetLocalInt(oDisturbedItem, "iCostTickets");
    if (iCostTickets <= 0)
    {
        SendMessageToPC(oPlayer, "You need more minerals.");
        return;
    }

    // Check if the player has an item tagged "I_CAS_CASINOHANDLE" in their inventory
    object oHandle = GetItemPossessedBy(oPlayer, "I_CAS_CASINOHANDLE");
    if (!GetIsObjectValid(oHandle))
    {
        SendMessageToPC(oPlayer, "You need a Casino Handle to make purchases.");
        return;
    }

    // Get the number of tickets the player has
    int nPlayerTickets = GetLocalInt(oHandle, "nCAS_TICKET");

    // Check if the player has enough tickets
    if (nPlayerTickets < iCostTickets)
    {
        SendMessageToPC(oPlayer, "You need " + IntToString(iCostTickets) + " tickets to get this item.");
    }

        // Create a copy of the item in a chest
        object oChest = GetNearestObjectByTag("CAS_REAGENTCONTAINER");

        // Debug message to check if the chest is found
        if (GetIsObjectValid(oChest))
        {
            SendMessageToPC(oPlayer, "Chest found successfully.");
        }
        else
        {
            SendMessageToPC(oPlayer, "Error: Chest not found.");
            return; // Exit the script if the chest is not found
        }

        // Cycle through the player's inventory
        object oItemInInventory = GetFirstItemInInventory(oPlayer);
        while (GetIsObjectValid(oItemInInventory))
        {
            // Check if this is the correct item
            if (oItemInInventory == oDisturbedItem)
            {
                // Copy the item to the chest
                object oNewItem = CopyItem(oItemInInventory, oChest, TRUE);
                DelayCommand(3.0, DestroyObject(oDisturbedItem)); // Destroy the original disturbed item
                return;
            }

            // Move to the next item in the inventory
            oItemInInventory = GetNextItemInInventory(oPlayer);
        }

        // If no matching item is found, handle the error
        SendMessageToPC(oPlayer, "Error: No matching item found to copy.");
        return;
    }

    // Deduct the required tickets
    SetLocalInt(oHandle, "nCAS_TICKET", nPlayerTickets - iCostTickets);

    // Retrieve the total number of rows in the "cas_reagents" 2DA
    int nRowCount = Get2DARowCount("cas_reagents");
    
    // Get the corresponding 2DA row from the disturbed item
    int i2DARow = GetLocalInt(oDisturbedItem, "i2DARowIndex");

    // Check if the specified row is valid
    if (i2DARow >= 0 && i2DARow < nRowCount)
    {
        string sBagResRef = "cas_reagentbag";
        int nQuantity = StringToInt(Get2DAStringByColumnIndex("cas_reagents", 1, i2DARow));
        string sItemResRef = Get2DAStringByColumnIndex("cas_reagents", 3, i2DARow);
        string sItemName = Get2DAStringByColumnIndex("cas_reagents", 0, i2DARow);

        // Create the bag inside the player's inventory
        object oBag = CreateItemOnObject(sBagResRef, oPlayer, nQuantity);
        
        // Optional: You can give the bag a name or description here
        SetName(oBag, sItemName + " (" + IntToString(nQuantity) + ")");
        
        int i; // Declare the loop variable outside the loop
        // Loop to create and add items to the bag
        for (i = 0; i < nQuantity; i++)
        {
            // Create the item and add it to the bag
            object oItemToAdd = CreateItemOnObject(sItemResRef, oBag);
        }
        
        // Destroy the original item that triggered the event
        DelayCommand(3.0, DestroyObject(oDisturbedItem));
    }
    else
    {
        // Handle the case where the specified row in the 2DA is invalid
        SendMessageToPC(oPlayer, "Error: Invalid 2DA row specified.");
    }
}
