using Microsoft.AspNetCore.SignalR;
using ColaFlow.API.Hubs;

namespace ColaFlow.API.Services;

public class RealtimeNotificationService : IRealtimeNotificationService
{
    private readonly IHubContext<ProjectHub> _projectHubContext;
    private readonly IHubContext<NotificationHub> _notificationHubContext;
    private readonly ILogger<RealtimeNotificationService> _logger;

    public RealtimeNotificationService(
        IHubContext<ProjectHub> projectHubContext,
        IHubContext<NotificationHub> notificationHubContext,
        ILogger<RealtimeNotificationService> logger)
    {
        _projectHubContext = projectHubContext;
        _notificationHubContext = notificationHubContext;
        _logger = logger;
    }

    public async Task NotifyProjectCreated(Guid tenantId, Guid projectId, object project)
    {
        var tenantGroupName = $"tenant-{tenantId}";

        _logger.LogInformation("Notifying tenant {TenantId} of new project {ProjectId}", tenantId, projectId);

        await _projectHubContext.Clients.Group(tenantGroupName).SendAsync("ProjectCreated", project);
    }

    public async Task NotifyProjectUpdated(Guid tenantId, Guid projectId, object project)
    {
        var projectGroupName = $"project-{projectId}";
        var tenantGroupName = $"tenant-{tenantId}";

        _logger.LogInformation("Notifying project {ProjectId} updated", projectId);

        await _projectHubContext.Clients.Group(projectGroupName).SendAsync("ProjectUpdated", project);
        await _projectHubContext.Clients.Group(tenantGroupName).SendAsync("ProjectUpdated", project);
    }

    public async Task NotifyProjectArchived(Guid tenantId, Guid projectId)
    {
        var projectGroupName = $"project-{projectId}";
        var tenantGroupName = $"tenant-{tenantId}";

        _logger.LogInformation("Notifying project {ProjectId} archived", projectId);

        await _projectHubContext.Clients.Group(projectGroupName).SendAsync("ProjectArchived", new { ProjectId = projectId });
        await _projectHubContext.Clients.Group(tenantGroupName).SendAsync("ProjectArchived", new { ProjectId = projectId });
    }

    public async Task NotifyProjectUpdate(Guid tenantId, Guid projectId, object data)
    {
        var groupName = $"project-{projectId}";

        _logger.LogInformation("Sending project update to group {GroupName}", groupName);

        await _projectHubContext.Clients.Group(groupName).SendAsync("ProjectUpdated", data);
    }

    public async Task NotifyIssueCreated(Guid tenantId, Guid projectId, object issue)
    {
        var groupName = $"project-{projectId}";

        await _projectHubContext.Clients.Group(groupName).SendAsync("IssueCreated", issue);
    }

    public async Task NotifyIssueUpdated(Guid tenantId, Guid projectId, object issue)
    {
        var groupName = $"project-{projectId}";

        await _projectHubContext.Clients.Group(groupName).SendAsync("IssueUpdated", issue);
    }

    public async Task NotifyIssueDeleted(Guid tenantId, Guid projectId, Guid issueId)
    {
        var groupName = $"project-{projectId}";

        await _projectHubContext.Clients.Group(groupName).SendAsync("IssueDeleted", new { IssueId = issueId });
    }

    public async Task NotifyIssueStatusChanged(
        Guid tenantId,
        Guid projectId,
        Guid issueId,
        string oldStatus,
        string newStatus)
    {
        var groupName = $"project-{projectId}";

        await _projectHubContext.Clients.Group(groupName).SendAsync("IssueStatusChanged", new
        {
            IssueId = issueId,
            OldStatus = oldStatus,
            NewStatus = newStatus,
            ChangedAt = DateTime.UtcNow
        });
    }

    public async Task NotifyUser(Guid userId, string message, string type = "info")
    {
        var userConnectionId = $"user-{userId}";

        await _notificationHubContext.Clients.User(userId.ToString()).SendAsync("Notification", new
        {
            Message = message,
            Type = type,
            Timestamp = DateTime.UtcNow
        });
    }

    public async Task NotifyUsersInTenant(Guid tenantId, string message, string type = "info")
    {
        var groupName = $"tenant-{tenantId}";

        await _notificationHubContext.Clients.Group(groupName).SendAsync("Notification", new
        {
            Message = message,
            Type = type,
            Timestamp = DateTime.UtcNow
        });
    }
}
