using ErsatzTV.Application.Television;
using ErsatzTV.Core.Api;
using ErsatzTV.Core.Api.Television;
using ErsatzTV.Core.Domain;
using ErsatzTV.Extensions;
using MediatR;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace ErsatzTV.Controllers.Api;

[ApiController]
public class SeasonsController(IMediator mediator) : ControllerBase
{
    [HttpGet("/api/v1/seasons/{id:int}", Name = "GetSeasonById")]
    [Tags("Television")]
    [EndpointSummary("Get a television season by id")]
    [EndpointGroupName("general")]
    [ProducesResponseType(typeof(SeasonDetailResponseModel), StatusCodes.Status200OK)]
    [ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status404NotFound)]
    public async Task<IActionResult> GetById(int id, CancellationToken cancellationToken)
    {
        Option<TelevisionSeasonViewModel> result =
            await mediator.Send(new GetTelevisionSeasonById(id), cancellationToken);
        return result.Map(Project).ToGetResult();
    }

    private static SeasonDetailResponseModel Project(TelevisionSeasonViewModel vm) =>
        new(
            vm.Id,
            vm.ShowId,
            vm.Title,
            string.IsNullOrWhiteSpace(vm.Year) ? null : vm.Year,
            vm.Name,
            ApiArtwork.Root(vm.Poster, ArtworkKind.Poster),
            ApiArtwork.Root(vm.FanArt, ArtworkKind.FanArt));
}
