using GTANetworkAPI;
using ReallifeGamemode.Database.Entities;
using ReallifeGamemode.Database.Entities.Logs.Chat;
using ReallifeGamemode.Database.Models;
using ReallifeGamemode.Server.Extensions;
using ReallifeGamemode.Server.Services;

namespace ReallifeGamemode.Server.Commands
{
  internal class GroupCommands : Script
  {
    #region Chat Commands

    [Command("gc", "~m~Benutzung: ~s~/gc [Nachricht]", GreedyArg = true)]
    public void CmdGroupG(Player player, string message)
    {
      if(!player.IsLoggedIn())
      {
        return;
      }

      using var dbContext = new DatabaseContext();
      User user = player.GetUser(dbContext);

      Group group = user?.Group;
      if (group == null)
      {
        ChatService.NotAuthorized(player);
        return;
      }


      var logEntry = new GroupChatLogEntry()
      {
        Text = message,
        UserId = user.Id,
        GroupId = user.Group.Id
      };

      dbContext.GroupChatLogs.Add(logEntry);
      dbContext.SaveChanges();

      message = System.Text.RegularExpressions.Regex.Replace(message, "(~[a-zA-Z]~)|(!{(.*)})", "");
      message = $"{player.Name}: {message}";

      ChatService.BroadcastGroup(message, group);
    }

    #endregion Chat Commands
  }
}
