using System.Linq;
using ErsatzTV.Application.MediaItems;
using ErsatzTV.Core.Api.MediaItems;
using MediatR;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace ErsatzTV.Controllers.Api;

[ApiController]
public class LanguagesController(IMediator mediator) : ControllerBase
{
    [HttpGet("/api/v1/languages", Name = "GetLanguages")]
    [Tags("Languages")]
    [EndpointSummary("Get all available language codes")]
    [EndpointGroupName("general")]
    [ProducesResponseType(typeof(List<LanguageCodeResponseModel>), StatusCodes.Status200OK)]
    public async Task<List<LanguageCodeResponseModel>> GetLanguages(CancellationToken cancellationToken)
    {
        List<LanguageCodeViewModel> languages = await mediator.Send(new GetAllLanguageCodes(), cancellationToken);
        return languages
            .Select(l => new LanguageCodeResponseModel(l.ThreeLetterISOLanguageName, l.EnglishName))
            .ToList();
    }
}
