using ColaFlow.API.Services;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;

namespace ColaFlow.API.Controllers;

[ApiController]
[Route("api/[controller]")]
[Authorize]
public class SignalRTestController : ControllerBase
{
    private readonly IRealtimeNotificationService _notificationService;

    public SignalRTestController(IRealtimeNotificationService notificationService)
    {
        _notificationService = notificationService;
    }

    /// <summary>
    /// Test sending notification to current user
    /// </summary>
    [HttpPost("test-user-notification")]
    public async Task<IActionResult> TestUserNotification([FromBody] string message)
    {
        var userId = Guid.Parse(User.FindFirst("sub")!.Value);

        await _notificationService.NotifyUser(userId, message, "test");

        return Ok(new { message = "Notification sent", userId });
    }

    /// <summary>
    /// Test sending notification to entire tenant
    /// </summary>
    [HttpPost("test-tenant-notification")]
    public async Task<IActionResult> TestTenantNotification([FromBody] string message)
    {
        var tenantId = Guid.Parse(User.FindFirst("tenant_id")!.Value);

        await _notificationService.NotifyUsersInTenant(tenantId, message, "test");

        return Ok(new { message = "Tenant notification sent", tenantId });
    }

    /// <summary>
    /// Test sending project update
    /// </summary>
    [HttpPost("test-project-update")]
    public async Task<IActionResult> TestProjectUpdate([FromBody] TestProjectUpdateRequest request)
    {
        var tenantId = Guid.Parse(User.FindFirst("tenant_id")!.Value);

        await _notificationService.NotifyProjectUpdate(tenantId, request.ProjectId, new
        {
            Message = request.Message,
            UpdatedBy = User.FindFirst("sub")!.Value,
            Timestamp = DateTime.UtcNow
        });

        return Ok(new { message = "Project update sent", projectId = request.ProjectId });
    }

    /// <summary>
    /// Test sending issue status change
    /// </summary>
    [HttpPost("test-issue-status-change")]
    public async Task<IActionResult> TestIssueStatusChange([FromBody] TestIssueStatusChangeRequest request)
    {
        var tenantId = Guid.Parse(User.FindFirst("tenant_id")!.Value);

        await _notificationService.NotifyIssueStatusChanged(
            tenantId,
            request.ProjectId,
            request.IssueId,
            request.OldStatus,
            request.NewStatus
        );

        return Ok(new
        {
            message = "Issue status change notification sent",
            projectId = request.ProjectId,
            issueId = request.IssueId
        });
    }

    /// <summary>
    /// Get connection info for debugging
    /// </summary>
    [HttpGet("connection-info")]
    public IActionResult GetConnectionInfo()
    {
        return Ok(new
        {
            userId = User.FindFirst("sub")?.Value,
            tenantId = User.FindFirst("tenant_id")?.Value,
            roles = User.Claims.Where(c => c.Type == "role").Select(c => c.Value).ToList(),
            hubEndpoints = new[]
            {
                "/hubs/project",
                "/hubs/notification"
            },
            instructions = "Connect to SignalR hubs using the endpoints above with access_token query parameter"
        });
    }
}

public record TestProjectUpdateRequest(Guid ProjectId, string Message);
public record TestIssueStatusChangeRequest(Guid ProjectId, Guid IssueId, string OldStatus, string NewStatus);
