#include "nw_i0_plot"
#include "cas_casinoinc" // Include the file containing LogCasinoTransaction

void main() 
{
    DelayCommand(6.0, DestroyObject(OBJECT_SELF));

    object oPC = GetFirstPC();
    object oArea = GetArea(OBJECT_SELF);

    while (GetIsObjectValid(oPC)) 
    {
        if (GetArea(oPC) == oArea) 
        {
            int nBetOnBlack = GetLocalInt(oPC, "I_BET_ON_BLACK");

            if (nBetOnBlack > 0) 
            {
                object oHandle = GetItemPossessedBy(oPC, "I_CAS_CASINOHANDLE");
                if (GetIsObjectValid(oHandle)) 
                {
                    int nCurrentTickets = GetLocalInt(oHandle, "nCAS_TICKET");
                    int nRewardTickets = nBetOnBlack * 3;
                    SetLocalInt(oHandle, "nCAS_TICKET", nCurrentTickets + nRewardTickets);

                    // Prepare and log the message
                    string sCharacterName = GetName(oPC);
                    string sPlayerUsername = GetPCPlayerName(oPC);
                    string sCDKey = GetPCPublicCDKey(oPC, TRUE);
                    string sLogMessage = "Character: " + sCharacterName + ", Player Name: " + sPlayerUsername + " (" + sCDKey + ") was awarded " + IntToString(nRewardTickets) + " tickets. Monster Betting.";
                    Log(sLogMessage, "cas");

                    SendMessageToPC(oPC, "Rewarding Tickets for Betting on BLACK: " + IntToString(nRewardTickets) + ". Total Tickets: " + IntToString(nCurrentTickets + nRewardTickets));
                }
                else 
                {
                    SendMessageToPC(oPC, "No valid handle found for rewards.");
                }
            }

            DeleteLocalInt(oPC, "I_BET_ON_RED");
            DeleteLocalInt(oPC, "I_BET_ON_BLACK");
        }
        oPC = GetNextPC();
    }

    object oHandler = GetLocalObject(OBJECT_SELF, "HANDLER");
    ExecuteScript("cas_clear_arena", oHandler);
}
