using System;
using System.Text.RegularExpressions;
using System.IO;
using System.Linq;

public class CPHInline
{
    public bool Execute()
    {
        // File path
        string blockedPath = @"C:\Program Files\OBS-Studio\files\txt_files\blockedUsers.txt";
        // Arguments
        string targetUser = args.ContainsKey("targetUser") ? args["targetUser"].ToString().ToLower() : "";
        string message = args.ContainsKey("messageStripped") ? args["messageStripped"].ToString().ToLower() : "";
        // Ensure file exists
        if (!File.Exists(blockedPath))
        {
            File.Create(blockedPath).Close();
        }

        // Check if user is in file
        var blockedUsers = File.ReadAllLines(blockedPath).Select(line => line.Trim().ToLower()).ToList();
        bool userExists = blockedUsers.Contains(targetUser);
        // Set whether user is in file
        CPH.SetArgument("ContainsInTextFile", userExists ? "true" : "false");
        // If already in file, skip link check
        if (userExists)
        {
            return true;
        }

        // Normalize "domain .com" to "domain.com"
        string cleanedMessage = Regex.Replace(message, @"\s*\.\s*", ".");
        // Regex to match common links/domains
        string pattern = @"(https?:\/\/\S+|www\.\S+|\b\S+\.(com|net|org|gov|edu|ca|uk|co|io|app|gg|tv|info|xyz|site|live|link|shop|store|biz|us|me|cc|de|jp|fr|au|nl|es|it|se|ch|no|ru)(\/\S*)?)";
        bool containsLink = Regex.IsMatch(cleanedMessage, pattern, RegexOptions.IgnoreCase);
        if (containsLink)
        {
            // Add user to file
            File.AppendAllText(blockedPath, targetUser + Environment.NewLine);
            CPH.SetArgument("containsLink", "true");
        }
        else
        {
            CPH.SetArgument("containsLink", "false");
        }

        return true;
    }
}