using System;
using System.Text.Json;
using System.Threading.Tasks;
using MairuiSdk;
using MairuiSdk.Exceptions;

namespace Quickstart
{
    class Program
    {
        static async Task Main(string[] args)
        {
            // 也可替换为真实证书: YOUR-LICENCE-UUID
            var licence = Environment.GetEnvironmentVariable("MAIRUI_LICENCE");
            if (string.IsNullOrWhiteSpace(licence))
            {
                Console.Error.WriteLine("请设置环境变量 MAIRUI_LICENCE=你的证书UUID");
                Environment.Exit(1);
            }

            using var api = new MairuiClient(licence.Trim());
            try
            {
                Console.WriteLine("== 股票列表 ==");
                var list = await api.StockListAsync();
                Console.WriteLine(JsonSerializer.Serialize(list));

                var q = new TimeRangeQuery { St = "20240101", Et = "20241231", Lt = 5 };
                Console.WriteLine("\n== 平安银行 日线 2024（lt=5）==");
                var bars = await api.StockHistoryAsync("000001.SZ", "d", "n", q);
                var barsJson = JsonSerializer.Serialize(bars);
                Console.WriteLine(barsJson.Length > 800 ? barsJson.Substring(0, 800) : barsJson);

                Console.WriteLine("\n== 实时行情 000001 ==");
                var rt = await api.StockRealTimeAsync("000001");
                var rtJson = JsonSerializer.Serialize(rt);
                Console.WriteLine(rtJson.Length > 400 ? rtJson.Substring(0, 400) : rtJson);
            }
            catch (MairuiApiException ex)
            {
                Console.Error.WriteLine("API 错误: " + ex.Message);
                Environment.Exit(2);
            }
        }
    }
}
