using System;

namespace TestersAI;

public sealed class Config
{
    public string? ApiKey { get; set; }
    public string BaseUrl { get; set; } = "https://api.testers.ai";
    public bool Enabled { get; set; } = true;
    public int TimeoutMs { get; set; } = 15_000;
    public int MaxRetries { get; set; } = 3;
    public int MaxRetryWaitMs { get; set; } = 5_000;
    public bool Strict { get; set; } = false;
    public string Sink { get; set; } = "framework"; // comma-separated: framework,disk,return
    public string SinkDir { get; set; } = "./testersai-results";
    public string LogFormat { get; set; } = "json"; // json|junit|tap|text
    public bool Quiet { get; set; } = false;        // suppress SDK-level log output

    public static Config FromEnv()
    {
        var c = new Config { ApiKey = Environment.GetEnvironmentVariable("TESTERSAI_API_KEY") };
        c.BaseUrl = Environment.GetEnvironmentVariable("TESTERSAI_BASE_URL") ?? c.BaseUrl;
        c.Enabled = Bool("TESTERSAI_ENABLED", c.Enabled);
        c.TimeoutMs = Int("TESTERSAI_TIMEOUT_MS", c.TimeoutMs);
        c.MaxRetries = Int("TESTERSAI_MAX_RETRIES", c.MaxRetries);
        c.MaxRetryWaitMs = Int("TESTERSAI_MAX_RETRY_WAIT_MS", c.MaxRetryWaitMs);
        c.Strict = Bool("TESTERSAI_STRICT", c.Strict);
        c.Sink = Environment.GetEnvironmentVariable("TESTERSAI_SINK") ?? c.Sink;
        c.SinkDir = Environment.GetEnvironmentVariable("TESTERSAI_SINK_DIR") ?? c.SinkDir;
        c.LogFormat = Environment.GetEnvironmentVariable("TESTERSAI_LOG_FORMAT") ?? c.LogFormat;
        c.Quiet = Bool("TESTERSAI_QUIET", c.Quiet);
        return c;
    }

    static int Int(string k, int d) => int.TryParse(Environment.GetEnvironmentVariable(k), out var v) ? v : d;
    static bool Bool(string k, bool d)
    {
        var v = Environment.GetEnvironmentVariable(k);
        return v is null ? d : v.Equals("1") || v.Equals("true", StringComparison.OrdinalIgnoreCase)
                              || v.Equals("yes", StringComparison.OrdinalIgnoreCase)
                              || v.Equals("on", StringComparison.OrdinalIgnoreCase);
    }
}
