namespace RioJoy.Core.Profiles;

/// <summary>
/// Top-level RIOJoy configuration: the profile library plus the global settings
/// that drive the three-state auto-switch (see docs/PLAN.md §Profiles). Persisted
/// as JSON by <see cref="ConfigStore"/>.
/// </summary>
public sealed class AppConfig
{
    /// <summary>Default RIO COM port when a profile doesn't specify one.</summary>
    public string DefaultRioComPort { get; set; } = "COM1";

    /// <summary>
    /// RIO link baud rate. 9600 = stock/wedge-patched firmware (native-game
    /// compatible). 31250 = the validated FastRIO chip
    /// (rio-firmware/RIOv4_2_patched_31250v2.bin) — needs an FTDI-class
    /// adapter; classic 16550 UARTs cannot make this rate.
    /// </summary>
    public int RioBaudRate { get; set; } = 9600;

    /// <summary>
    /// Analog poll interval in milliseconds (legacy cadence 55 → ~18 Hz).
    /// The FastRIO chip sustains ~20-25 ms (~30+ Hz effective; bench
    /// 2026-07-19). Leave at 55 for stock-speed boards.
    /// </summary>
    public int AnalogPollMs { get; set; } = 55;

    /// <summary>Default plasma COM port when a profile doesn't specify one.</summary>
    public string? DefaultPlasmaComPort { get; set; } = "COM2";

    /// <summary>
    /// Executable names of the native games to yield to. When one of these is the
    /// foreground app, RIOJoy releases the COM port and goes dormant. Matched
    /// case-insensitively, with or without ".exe".
    /// </summary>
    public List<string> NativeGameExecutables { get; set; } = new();

    /// <summary>The per-game profile library.</summary>
    public List<RioProfile> Profiles { get; set; } = new();

    /// <summary>
    /// Optional profile to activate when nothing else matches (desktop/idle). Null
    /// means stay idle / release the port.
    /// </summary>
    public string? NeutralProfileName { get; set; }

    /// <summary>
    /// Path to the cockpit overlay template (<c>regions.json</c>) used to generate
    /// per-profile wallpapers (Phase 7). Null disables wallpaper generation. The
    /// template's base image is resolved relative to this file's folder.
    /// </summary>
    public string? OverlayTemplatePath { get; set; }

    /// <summary>Find a profile by name (case-insensitive), or null.</summary>
    public RioProfile? FindProfile(string? name) =>
        name is null
            ? null
            : Profiles.FirstOrDefault(p => string.Equals(p.Name, name, StringComparison.OrdinalIgnoreCase));
}
