using System.Reflection;
using ErsatzTV.Application.Filler;
using ErsatzTV.Controllers.Api;
using ErsatzTV.Controllers.Api.Requests;
using ErsatzTV.Core;
using ErsatzTV.Core.Api.Filler;
using ErsatzTV.Core.Domain;
using ErsatzTV.Core.Domain.Filler;
using ErsatzTV.Core.Errors;
using LanguageExt;
using MediatR;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Routing;
using NSubstitute;
using NUnit.Framework;
using Shouldly;
using static LanguageExt.Prelude;
using Unit = LanguageExt.Unit;

namespace ErsatzTV.Tests.Controllers;

[TestFixture]
public class FillerPresetControllerTests
{
    [SetUp]
    public void SetUp()
    {
        _mediator = Substitute.For<IMediator>();
        _controller = new FillerPresetController(_mediator);
    }

    private FillerPresetController _controller = null!;
    private IMediator _mediator = null!;

    [Test]
    public void Controller_Should_Expose_Idiomatic_Rest_Routes()
    {
        ShouldHaveActionRoute("GET", "/api/v1/filler-presets");
        ShouldHaveActionRoute("GET", "/api/v1/filler-presets/{id:int}");
        ShouldHaveActionRoute("POST", "/api/v1/filler-presets");
        ShouldHaveActionRoute("PUT", "/api/v1/filler-presets/{id:int}");
        ShouldHaveActionRoute("DELETE", "/api/v1/filler-presets/{id:int}");
    }

    [Test]
    public void Mutations_Should_Use_Request_Dtos_For_Wire_Contract()
    {
        ParameterInfo createRequest = typeof(FillerPresetController)
            .GetMethod(nameof(FillerPresetController.Create))!
            .GetParameters()
            .Single(p => p.Name == "request");
        ParameterInfo updateRequest = typeof(FillerPresetController)
            .GetMethod(nameof(FillerPresetController.Update))!
            .GetParameters()
            .Single(p => p.Name == "request");

        createRequest.ParameterType.ShouldBe(typeof(CreateFillerPresetRequest));
        updateRequest.ParameterType.ShouldBe(typeof(UpdateFillerPresetRequest));
    }

    [Test]
    public async Task GetAll_Should_Return_FillerPresets()
    {
        List<FillerPresetResponseModel> models =
        [
            new FillerPresetResponseModel(1, "Intro", FillerKind.PreRoll),
            new FillerPresetResponseModel(2, "Outro", FillerKind.PostRoll)
        ];
        _mediator.Send(Arg.Any<GetAllFillerPresetsForApi>(), Arg.Any<CancellationToken>())
            .Returns(models);

        List<FillerPresetResponseModel> result = await _controller.GetAll(null, CancellationToken.None);

        result.ShouldBe(models);
    }

    [Test]
    public async Task GetAll_Should_Forward_FillerKind_Filter()
    {
        _mediator.Send(Arg.Any<GetAllFillerPresetsForApi>(), Arg.Any<CancellationToken>())
            .Returns([]);

        await _controller.GetAll(FillerKind.MidRoll, CancellationToken.None);

        await _mediator.Received(1).Send(
            Arg.Is<GetAllFillerPresetsForApi>(q => q.FillerKind == FillerKind.MidRoll),
            Arg.Any<CancellationToken>());
    }

    [Test]
    public async Task GetById_Should_Return_200_For_Some()
    {
        FillerPresetFullResponseModel vm = MakeVm(4);
        _mediator.Send(Arg.Any<GetFillerPresetByIdForApi>(), Arg.Any<CancellationToken>())
            .Returns(Option<FillerPresetFullResponseModel>.Some(vm));

        IActionResult result = await _controller.GetById(4, CancellationToken.None);

        result.ShouldBeOfType<OkObjectResult>().Value.ShouldBe(vm);
    }

    [Test]
    public async Task GetById_Should_Return_404_For_None()
    {
        _mediator.Send(Arg.Any<GetFillerPresetByIdForApi>(), Arg.Any<CancellationToken>())
            .Returns(Option<FillerPresetFullResponseModel>.None);

        IActionResult result = await _controller.GetById(4, CancellationToken.None);

        var notFound = result.ShouldBeOfType<NotFoundObjectResult>();
        var problemDetails = notFound.Value.ShouldBeOfType<ProblemDetails>();
        problemDetails.Status.ShouldBe(404);
    }

    [Test]
    public async Task Create_Should_Return_201_With_Location_And_Body()
    {
        _mediator.Send(Arg.Any<CreateFillerPreset>(), Arg.Any<CancellationToken>())
            .Returns(Right<BaseError, CreateFillerPresetResult>(new CreateFillerPresetResult(7)));
        FillerPresetFullResponseModel vm = MakeVm(7);
        _mediator.Send(Arg.Any<GetFillerPresetByIdForApi>(), Arg.Any<CancellationToken>())
            .Returns(Option<FillerPresetFullResponseModel>.Some(vm));

        IActionResult result = await _controller.Create(MakeCreateRequest(), CancellationToken.None);

        var created = result.ShouldBeOfType<CreatedResult>();
        created.StatusCode.ShouldBe(201);
        created.Location.ShouldBe("/api/v1/filler-presets/7");
        created.Value.ShouldBe(vm);
    }

    [Test]
    public async Task Create_Should_Return_422_On_Validation_Error()
    {
        _mediator.Send(Arg.Any<CreateFillerPreset>(), Arg.Any<CancellationToken>())
            .Returns(Left<BaseError, CreateFillerPresetResult>(BaseError.New("bad")));

        IActionResult result = await _controller.Create(MakeCreateRequest(), CancellationToken.None);

        result.ShouldBeOfType<UnprocessableEntityObjectResult>();
    }

    [Test]
    public async Task Update_Should_Return_200_And_Map_Route_Id_To_Command()
    {
        _mediator.Send(Arg.Any<UpdateFillerPreset>(), Arg.Any<CancellationToken>())
            .Returns(Right<BaseError, Unit>(Unit.Default));
        FillerPresetFullResponseModel vm = MakeVm(8);
        _mediator.Send(Arg.Any<GetFillerPresetByIdForApi>(), Arg.Any<CancellationToken>())
            .Returns(Option<FillerPresetFullResponseModel>.Some(vm));

        IActionResult result = await _controller.Update(8, MakeUpdateRequest(), CancellationToken.None);

        result.ShouldBeOfType<OkObjectResult>().Value.ShouldBe(vm);
        await _mediator.Received(1).Send(
            Arg.Is<UpdateFillerPreset>(c => c.Id == 8),
            Arg.Any<CancellationToken>());
    }

    [Test]
    public async Task Update_Should_Return_422_For_Error()
    {
        _mediator.Send(Arg.Any<UpdateFillerPreset>(), Arg.Any<CancellationToken>())
            .Returns(Left<BaseError, Unit>(BaseError.New("nope")));

        IActionResult result = await _controller.Update(99, MakeUpdateRequest(), CancellationToken.None);

        result.ShouldBeOfType<UnprocessableEntityObjectResult>();
    }

    [Test]
    public async Task Delete_Should_Return_204_On_Success()
    {
        _mediator.Send(Arg.Any<DeleteFillerPreset>(), Arg.Any<CancellationToken>())
            .Returns(Right<BaseError, Unit>(Unit.Default));

        IActionResult result = await _controller.Delete(9, CancellationToken.None);

        result.ShouldBeOfType<NoContentResult>();
    }

    [Test]
    public async Task Delete_Should_Return_404_For_NotFoundError()
    {
        _mediator.Send(Arg.Any<DeleteFillerPreset>(), Arg.Any<CancellationToken>())
            .Returns(Left<BaseError, Unit>(new NotFoundError("missing")));

        IActionResult result = await _controller.Delete(9, CancellationToken.None);

        result.ShouldBeOfType<NotFoundObjectResult>();
    }

    private static void ShouldHaveActionRoute(string httpMethod, string route)
    {
        bool exists = typeof(FillerPresetController)
            .GetMethods(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly)
            .SelectMany(m => m.GetCustomAttributes<HttpMethodAttribute>(inherit: true))
            .Any(a => a.HttpMethods.Contains(httpMethod) && a.Template == route);

        exists.ShouldBeTrue($"Missing route {httpMethod} {route}");
    }

    private static FillerPresetFullResponseModel MakeVm(int id) =>
        new(
            id,
            "Intro",
            FillerKind.PreRoll,
            FillerMode.Count,
            null,
            3,
            null,
            true,
            CollectionType.Collection,
            5,
            null,
            null,
            null,
            null,
            null,
            false);

    private static CreateFillerPresetRequest MakeCreateRequest() =>
        new(
            "Intro",
            FillerKind.PreRoll,
            FillerMode.Count,
            null,
            3,
            null,
            true,
            CollectionType.Collection,
            5,
            null,
            null,
            null,
            null,
            null,
            false);

    private static UpdateFillerPresetRequest MakeUpdateRequest() =>
        new(
            "Intro",
            FillerKind.PreRoll,
            FillerMode.Count,
            null,
            3,
            null,
            true,
            CollectionType.Collection,
            5,
            null,
            null,
            null,
            null,
            null,
            false);
}
