void ExecuteAdjustReputationAndAttack(string sTargetTag)
{
    object oTarget = GetObjectByTag(sTargetTag);

    if(GetIsObjectValid(oTarget))
    {
      //  SendMessageToPC(GetFirstPC(), "ExecuteAdjustReputationAndAttack: Target found - " + GetName(oTarget));

        // Ensure AI is active
        SetAILevel(OBJECT_SELF, AI_LEVEL_DEFAULT);
        SetAILevel(oTarget, AI_LEVEL_DEFAULT);

        // Clear any current actions
        ClearAllActions();

        // Adjust reputation to make them hostile towards each other
        AdjustReputation(OBJECT_SELF, oTarget, -100);

        // Reconfirm target validity
        if(GetIsObjectValid(oTarget))
        {
            // Command to attack the target
            AssignCommand(OBJECT_SELF, ActionAttack(oTarget));
           // SendMessageToPC(GetFirstPC(), "Issued attack command against " + GetName(oTarget));
        }
        else
        {
           // SendMessageToPC(GetFirstPC(), "Target became invalid: " + sTargetTag);
        }
    }
    else
    {
      //  SendMessageToPC(GetFirstPC(), "No valid target found with tag: " + sTargetTag);
    }
}

void AdjustReputationAndAttack(string sTargetTag)
{
   // SendMessageToPC(GetFirstPC(), "AdjustReputationAndAttack: Waiting 2 seconds before executing...");

    // Delay the retrieval of the target object and subsequent actions
    DelayCommand(2.0, ExecuteAdjustReputationAndAttack(sTargetTag));
}

void main()
{
    //SendMessageToPC(GetFirstPC(), "OnSpawn script triggered.");
    
    // Disable the creature's AI so it only follows scripted commands
    SetCommandable(FALSE, OBJECT_SELF);

    // Retrieve the tag of the target from the local string variable
    string sTagOfTarget = GetLocalString(OBJECT_SELF, "CAS_ATTACKMOB");
   // SendMessageToPC(GetFirstPC(), "Target tag to look for: " + sTagOfTarget);

    // Check if the tag is not empty
    if(sTagOfTarget != "")
    {
     //   SendMessageToPC(GetFirstPC(), "Starting AdjustReputationAndAttack in 6 seconds.");
        DelayCommand(54.0, SetCommandable(TRUE, OBJECT_SELF));
        
        // Delay 6 seconds, then adjust reputation and attack
        DelayCommand(55.0, AdjustReputationAndAttack(sTagOfTarget));
    }
    else
    {
      //  SendMessageToPC(GetFirstPC(), "No target tag set on this creature.");
    }

    // Despawn the creature after 180 seconds
    DelayCommand(180.0, DestroyObject(OBJECT_SELF));
}
