using ErsatzTV.Application.MediaSources;
using ErsatzTV.Core.Api.MediaSources;
using MediatR;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace ErsatzTV.Controllers.Api;

[ApiController]
public class MediaSourcesController(IMediator mediator) : ControllerBase
{
    [HttpGet("/api/v1/media-sources", Name = "GetMediaSources")]
    [Tags("Media Sources")]
    [EndpointSummary("Get all media sources with their libraries")]
    [EndpointGroupName("general")]
    [ProducesResponseType(typeof(List<MediaSourceResponseModel>), StatusCodes.Status200OK)]
    public async Task<List<MediaSourceResponseModel>> GetAll(CancellationToken cancellationToken) =>
        await mediator.Send(new GetAllMediaSourcesForApi(), cancellationToken);

    [HttpGet("/api/v1/media-sources/collections-scan-status", Name = "GetCollectionsScanStatus")]
    [Tags("Media Sources")]
    [EndpointSummary("Get active external-collections scan status")]
    [EndpointDescription(
        "Returns the media-source families (plex/jellyfin/emby) whose external-collections scan lock is " +
        "currently held. Collections locks are family-global, so an entry means every source of that family " +
        "is scanning. Empty when nothing is scanning. The SPA polls this to reconcile the collections scan " +
        "buttons against a live active set, matching the library-scan UX.")]
    [EndpointGroupName("general")]
    [ProducesResponseType(typeof(List<CollectionsScanStatusResponseModel>), StatusCodes.Status200OK)]
    public async Task<List<CollectionsScanStatusResponseModel>> GetCollectionsScanStatus(
        CancellationToken cancellationToken) =>
        await mediator.Send(new GetCollectionsScanStatus(), cancellationToken);
}
