using Microsoft.AspNetCore.Mvc;
using Nexus.Api.Controllers;
using Nexus.Api.Data;
using Nexus.Api.DTOs;
using Nexus.Api.Models;
using Nexus.Api.Services;
using Xunit;

namespace Nexus.Api.Tests;

public sealed class ProjectRelationsTests
{
    [Fact]
    public async Task GetTasks_ProjectsExistingAgentCorrelationFields()
    {
        var project = new Project
        {
            Id = Guid.NewGuid(),
            Name = "Release readiness"
        };
        var task = new WorkTask
        {
            Id = Guid.NewGuid(),
            Title = "Verify release",
            State = "In progress",
            Priority = "High",
            ProjectId = project.Id,
            AssignedTo = "iris",
            ExpectedFrom = "iris",
            IsAgentTask = true
        };
        var controller = new ProjectsController(
            new ProjectRelationsService(project, task));

        var response = await controller.GetTasks(project.Id, CancellationToken.None);

        var ok = Assert.IsType<OkObjectResult>(response.Result);
        var items = Assert.IsAssignableFrom<IReadOnlyList<ProjectTaskDto>>(ok.Value);
        var projected = Assert.Single(items);
        Assert.Equal(project.Id, projected.ProjectId);
        Assert.Equal("iris", projected.AssignedTo);
        Assert.Equal("iris", projected.ExpectedFrom);
        Assert.True(projected.IsAgentTask);
    }

    private sealed class ProjectRelationsService(Project project, WorkTask task)
        : IProjectService
    {
        public Task<IReadOnlyList<Project>> GetAllAsync(CancellationToken ct = default)
            => Task.FromResult<IReadOnlyList<Project>>([project]);

        public Task<Project?> GetByIdAsync(Guid id, CancellationToken ct = default)
            => Task.FromResult<Project?>(id == project.Id ? project : null);

        public Task<IReadOnlyList<WorkTask>> GetTasksAsync(
            Guid id,
            CancellationToken ct = default)
            => Task.FromResult<IReadOnlyList<WorkTask>>(
                id == project.Id ? [task] : []);

        public Task<Project> CreateAsync(
            CreateProjectRequest request,
            CancellationToken ct = default)
            => throw new NotSupportedException();

        public Task<Project?> UpdateAsync(
            Guid id,
            UpdateProjectRequest request,
            CancellationToken ct = default)
            => throw new NotSupportedException();

        public Task<ProjectDeleteResult> DeleteAsync(
            Guid id,
            CancellationToken ct = default)
            => throw new NotSupportedException();
    }
}
