using ErsatzTV.Application.Emby;
using ErsatzTV.Application.Jellyfin;
using ErsatzTV.Core.Emby;
using ErsatzTV.Core.Jellyfin;

namespace ErsatzTV.Controllers.Api.Requests;

// ApiKey blank/omitted = retain the existing key (design #202 secure connection contract, finding 1)
// — the caller passes the source's current key so ToCommand can substitute it when the incoming
// value is blank. A non-blank value sets a new key. Required on first connect (no existing key),
// which the controller/handler enforces before dispatch.
public record SaveRemoteConnectionRequest(string Address, string ApiKey)
{
    public SaveJellyfinSecrets ToJellyfinCommand(string existingApiKey) =>
        new(
            new JellyfinSecrets
            {
                Address = Address,
                ApiKey = string.IsNullOrWhiteSpace(ApiKey) ? existingApiKey : ApiKey
            });

    public SaveEmbySecrets ToEmbyCommand(string existingApiKey) =>
        new(
            new EmbySecrets
            {
                Address = Address,
                ApiKey = string.IsNullOrWhiteSpace(ApiKey) ? existingApiKey : ApiKey
            });
}
