using VoiceCat.App.Forms;

namespace VoiceCat.App;

internal static class Program
{
    [STAThread]
    private static void Main()
    {
        // Diagnostic-logging-only for now (manual debugging session) — every exception that
        // would otherwise be silently caught by WinForms' default message-loop handling (or
        // crash with no visible cause) gets printed to stdout/stderr first.
        Application.ThreadException += (_, e) =>
            Console.Error.WriteLine($"[UNHANDLED ThreadException] {e.Exception}");
        AppDomain.CurrentDomain.UnhandledException += (_, e) =>
            Console.Error.WriteLine($"[UNHANDLED AppDomain exception] {e.ExceptionObject}");

        Console.WriteLine("VoiceCat.App starting...");
        ApplicationConfiguration.Initialize();

        using var connectDialog = new ConnectDialog();
        Console.WriteLine("Showing ConnectDialog...");
        var result = connectDialog.ShowDialog();
        Console.WriteLine($"ConnectDialog closed with DialogResult={result}, ConnectedClient={(connectDialog.ConnectedClient is null ? "null" : "set")}");
        if (result != DialogResult.OK || connectDialog.ConnectedClient is null)
        {
            Console.WriteLine("Exiting (cancelled or no connected client).");
            return;
        }

        Console.WriteLine("Launching MainForm...");
        Application.Run(new MainForm(connectDialog.ConnectedClient, connectDialog.SelfUserId,
            connectDialog.Nickname, connectDialog.ServerName));
        Console.WriteLine("MainForm closed. Exiting.");
    }
}
