using Microsoft.AspNetCore.Http.HttpResults;
using Microsoft.AspNetCore.Mvc;
using WorkClub.Api.Services;
using WorkClub.Application.Tasks.DTOs;

namespace WorkClub.Api.Endpoints.Tasks;

public static class TaskEndpoints
{
    public static void MapTaskEndpoints(this IEndpointRouteBuilder app)
    {
        var group = app.MapGroup("/api/tasks");

        group.MapGet("", GetTasks)
            .RequireAuthorization("RequireMember")
            .WithName("GetTasks");

        group.MapGet("{id:guid}", GetTask)
            .RequireAuthorization("RequireMember")
            .WithName("GetTask");

        group.MapPost("", CreateTask)
            .RequireAuthorization("RequireManager")
            .WithName("CreateTask");

        group.MapPatch("{id:guid}", UpdateTask)
            .RequireAuthorization("RequireMember")
            .WithName("UpdateTask");

        group.MapDelete("{id:guid}", DeleteTask)
            .RequireAuthorization("RequireAdmin")
            .WithName("DeleteTask");
    }

    private static async Task<Ok<TaskListDto>> GetTasks(
        TaskService taskService,
        [FromQuery] string? status = null,
        [FromQuery] int page = 1,
        [FromQuery] int pageSize = 20)
    {
        var result = await taskService.GetTasksAsync(status, page, pageSize);
        return TypedResults.Ok(result);
    }

    private static async Task<Results<Ok<TaskDetailDto>, NotFound>> GetTask(
        Guid id,
        TaskService taskService)
    {
        var result = await taskService.GetTaskByIdAsync(id);

        if (result == null)
            return TypedResults.NotFound();

        return TypedResults.Ok(result);
    }

    private static async Task<Results<Created<TaskDetailDto>, BadRequest<string>>> CreateTask(
        CreateTaskRequest request,
        TaskService taskService,
        HttpContext httpContext)
    {
        var userIdClaim = httpContext.User.FindFirst("sub")?.Value;
        if (string.IsNullOrEmpty(userIdClaim) || !Guid.TryParse(userIdClaim, out var createdById))
        {
            return TypedResults.BadRequest("Invalid user ID");
        }

        var (task, error) = await taskService.CreateTaskAsync(request, createdById);

        if (error != null || task == null)
            return TypedResults.BadRequest(error ?? "Failed to create task");

        return TypedResults.Created($"/api/tasks/{task.Id}", task);
    }

    private static async Task<Results<Ok<TaskDetailDto>, NotFound, UnprocessableEntity<string>, Conflict<string>>> UpdateTask(
        Guid id,
        UpdateTaskRequest request,
        TaskService taskService)
    {
        var (task, error, isConflict) = await taskService.UpdateTaskAsync(id, request);

        if (error != null)
        {
            if (error == "Task not found")
                return TypedResults.NotFound();

            if (isConflict)
                return TypedResults.Conflict(error);

            return TypedResults.UnprocessableEntity(error);
        }

        return TypedResults.Ok(task!);
    }

    private static async Task<Results<NoContent, NotFound>> DeleteTask(
        Guid id,
        TaskService taskService)
    {
        var deleted = await taskService.DeleteTaskAsync(id);

        if (!deleted)
            return TypedResults.NotFound();

        return TypedResults.NoContent();
    }
}
