using MBN_STOCK_WEBVIEW.Core.Playout.Scenes;

namespace MBN_STOCK_WEBVIEW.Playout.Configuration;

public sealed record LegacyParityStartupCompositionResolution(
    LegacySceneCueCompositionOptions Composition,
    string WarningMessage)
{
    public bool HasWarning => WarningMessage.Length > 0;
}

/// <summary>
/// Resolves the startup-only MainForm background behavior used by LegacyParityApp.
/// An explicit local file remains authoritative; only an absent file enables the
/// validated legacy Cuts\video\기본.vrv fallback.
/// </summary>
public static class LegacyParityStartupCompositionResolver
{
    public const string MissingLegacyDefaultWarning =
        "신뢰 Cuts 경로에서 video\\기본.vrv를 확인할 수 없어 배경없음으로 시작했습니다.";

    public static LegacyParityStartupCompositionResolution Resolve(
        PlayoutOptions options,
        bool explicitLocalConfigurationExists)
    {
        ArgumentNullException.ThrowIfNull(options);
        var configured = PlayoutSceneCompositionFactory.Create(options);
        if (explicitLocalConfigurationExists ||
            configured.BackgroundKind != LegacySceneBackgroundKind.None)
        {
            return new LegacyParityStartupCompositionResolution(configured, string.Empty);
        }

        try
        {
            return new LegacyParityStartupCompositionResolution(
                PlayoutSceneCompositionFactory.CreateLegacyStartupDefault(
                    options,
                    configured.FadeDuration),
                string.Empty);
        }
        catch (PlayoutConfigurationException)
        {
            // Missing/invalid external media is not an application-startup failure.
            // Keep the already validated None composition and expose one bounded,
            // path-free warning through the native state projection.
            return new LegacyParityStartupCompositionResolution(
                configured,
                MissingLegacyDefaultWarning);
        }
    }
}
