using MediatR;
using Microsoft.AspNetCore.Mvc;
using ColaFlow.Modules.ProjectManagement.Application.DTOs;
using ColaFlow.Modules.ProjectManagement.Application.Commands.CreateEpic;
using ColaFlow.Modules.ProjectManagement.Application.Commands.UpdateEpic;
using ColaFlow.Modules.ProjectManagement.Application.Queries.GetEpicById;
using ColaFlow.Modules.ProjectManagement.Application.Queries.GetEpicsByProjectId;

namespace ColaFlow.API.Controllers;

/// <summary>
/// Epics API Controller
/// </summary>
[ApiController]
[Route("api/v1")]
public class EpicsController(IMediator mediator) : ControllerBase
{
    private readonly IMediator _mediator = mediator ?? throw new ArgumentNullException(nameof(mediator));

    /// <summary>
    /// Get all epics for a project
    /// </summary>
    [HttpGet("projects/{projectId:guid}/epics")]
    [ProducesResponseType(typeof(List<EpicDto>), StatusCodes.Status200OK)]
    [ProducesResponseType(StatusCodes.Status404NotFound)]
    public async Task<IActionResult> GetProjectEpics(Guid projectId, CancellationToken cancellationToken = default)
    {
        var query = new GetEpicsByProjectIdQuery(projectId);
        var result = await _mediator.Send(query, cancellationToken);
        return Ok(result);
    }

    /// <summary>
    /// Get epic by ID
    /// </summary>
    [HttpGet("epics/{id:guid}")]
    [ProducesResponseType(typeof(EpicDto), StatusCodes.Status200OK)]
    [ProducesResponseType(StatusCodes.Status404NotFound)]
    public async Task<IActionResult> GetEpic(Guid id, CancellationToken cancellationToken = default)
    {
        var query = new GetEpicByIdQuery(id);
        var result = await _mediator.Send(query, cancellationToken);

        if (result == null)
        {
            return NotFound();
        }

        return Ok(result);
    }

    /// <summary>
    /// Create a new epic (independent endpoint)
    /// </summary>
    [HttpPost("epics")]
    [ProducesResponseType(typeof(EpicDto), StatusCodes.Status201Created)]
    [ProducesResponseType(StatusCodes.Status400BadRequest)]
    [ProducesResponseType(StatusCodes.Status404NotFound)]
    public async Task<IActionResult> CreateEpicIndependent(
        [FromBody] CreateEpicCommand command,
        CancellationToken cancellationToken = default)
    {
        var result = await _mediator.Send(command, cancellationToken);
        return CreatedAtAction(nameof(GetEpic), new { id = result.Id }, result);
    }

    /// <summary>
    /// Create a new epic (nested endpoint)
    /// </summary>
    [HttpPost("projects/{projectId:guid}/epics")]
    [ProducesResponseType(typeof(EpicDto), StatusCodes.Status201Created)]
    [ProducesResponseType(StatusCodes.Status400BadRequest)]
    [ProducesResponseType(StatusCodes.Status404NotFound)]
    public async Task<IActionResult> CreateEpic(
        Guid projectId,
        [FromBody] CreateEpicRequest request,
        CancellationToken cancellationToken = default)
    {
        var command = new CreateEpicCommand
        {
            ProjectId = projectId,
            Name = request.Name,
            Description = request.Description,
            CreatedBy = request.CreatedBy
        };

        var result = await _mediator.Send(command, cancellationToken);
        return CreatedAtAction(nameof(GetEpic), new { id = result.Id }, result);
    }

    /// <summary>
    /// Update an existing epic
    /// </summary>
    [HttpPut("epics/{id:guid}")]
    [ProducesResponseType(typeof(EpicDto), StatusCodes.Status200OK)]
    [ProducesResponseType(StatusCodes.Status400BadRequest)]
    [ProducesResponseType(StatusCodes.Status404NotFound)]
    public async Task<IActionResult> UpdateEpic(
        Guid id,
        [FromBody] UpdateEpicRequest request,
        CancellationToken cancellationToken = default)
    {
        var command = new UpdateEpicCommand
        {
            EpicId = id,
            Name = request.Name,
            Description = request.Description
        };

        var result = await _mediator.Send(command, cancellationToken);

        if (result == null)
        {
            return NotFound();
        }

        return Ok(result);
    }
}

/// <summary>
/// Request model for creating an epic
/// </summary>
public record CreateEpicRequest
{
    public string Name { get; init; } = string.Empty;
    public string Description { get; init; } = string.Empty;
    public Guid CreatedBy { get; init; }
}

/// <summary>
/// Request model for updating an epic
/// </summary>
public record UpdateEpicRequest
{
    public string Name { get; init; } = string.Empty;
    public string Description { get; init; } = string.Empty;
}
