using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.Json;
using System.Threading.Tasks;
using MairuiSdk;

class SmokeAll {
  class Row { public string name { get; set; } public bool ok { get; set; } public bool empty { get; set; } public int n { get; set; } public string err { get; set; } }
  static async Task Main() {
    var lic = Environment.GetEnvironmentVariable("MAIRUI_LICENCE") ?? "EE9059F4-B601-4F0A-B64D-DEE2BD64822C";
    var api = new MairuiClient(lic);
    var results = new List<Row>();
    async Task Run(string name, Func<Task<object>> fn) {
      try {
        var data = await fn();
        int n = Size(data); bool empty = IsEmpty(data);
        results.Add(new Row { name=name, ok=true, empty=empty, n=n });
        Console.WriteLine($"{(empty?"EMPTY":"OK   ")} {name} n={n}");
      } catch (Exception e) {
        results.Add(new Row { name=name, ok=false, err=e.Message });
        Console.WriteLine($"FAIL {name}: {e.Message}");
      }
    }
    await Run("stock_list", async () => api.StockListAsync());
    await Run("new_stock_calendar", async () => api.NewStockCalendarAsync());
    await Run("sectors_list", async () => api.SectorsListAsync());
    await Run("primary_sectors_list", async () => api.PrimarySectorsListAsync());
    await Run("sector_detail", async () => api.SectorDetailAsync("GN玻璃"));
    await Run("stock_history", async () => api.StockHistoryAsync("000001.SZ", "d", "n", new TimeRangeQuery { St="20240101", Et="20241231", Lt=1 }));
    await Run("stock_latest", async () => api.StockLatestAsync("000001.SZ", "d", "n", lt: 1));
    await Run("stock_vip_history", async () => api.StockVipHistoryAsync("000001.SZ", "d", "n", new TimeRangeQuery { St="20240101", Et="20241231", Lt=1 }));
    await Run("stock_stopprice", async () => api.StockStoppriceAsync("000001.SZ", new TimeRangeQuery { Lt=1 }));
    await Run("stock_indicators", async () => api.StockIndicatorsAsync("000001.SZ", new TimeRangeQuery { Lt=1 }));
    await Run("stock_indicator", async () => api.StockIndicatorAsync("macd", "000001.SZ", "d", "n", new TimeRangeQuery { Lt=1 }));
    await Run("stock_macd", async () => api.StockMacdAsync("000001.SZ", "d", "n", new TimeRangeQuery { St="20240101", Et="20241231", Lt=1 }));
    await Run("stock_ma", async () => api.StockMaAsync("000001.SZ", "d", "n", new TimeRangeQuery { St="20240101", Et="20241231", Lt=1 }));
    await Run("stock_boll", async () => api.StockBollAsync("000001.SZ", "d", "n", new TimeRangeQuery { St="20240101", Et="20241231", Lt=1 }));
    await Run("stock_kdj", async () => api.StockKdjAsync("000001.SZ", "d", "n", new TimeRangeQuery { St="20240101", Et="20241231", Lt=1 }));
    await Run("stock_real_time", async () => api.StockRealTimeAsync("000001"));
    await Run("stock_real_five", async () => api.StockRealFiveAsync("000001"));
    await Run("stock_ssjy", async () => api.StockSsjyAsync("000001"));
    await Run("stock_ssjy_all", async () => api.StockSsjyAllAsync());
    await Run("stock_real_all", async () => api.StockRealAllAsync());
    await Run("stock_ssjy_more", async () => api.StockSsjyMoreAsync(new[] { "000001", "600519" }));
    await Run("stock_zbjy", async () => api.StockZbjyAsync("000001"));
    await Run("stock_transaction", async () => api.StockTransactionAsync("000001.SZ", new TimeRangeQuery { Lt=1 }));
    await Run("stock_financial", async () => api.StockFinancialAsync("balance", "000001.SZ", new TimeRangeQuery { Lt=1 }));
    await Run("stock_balance", async () => api.StockBalanceAsync("000001.SZ", new TimeRangeQuery { Lt=1 }));
    await Run("stock_income", async () => api.StockIncomeAsync("000001.SZ", new TimeRangeQuery { Lt=1 }));
    await Run("stock_cashflow", async () => api.StockCashflowAsync("000001.SZ", new TimeRangeQuery { Lt=1 }));
    await Run("stock_pershareindex", async () => api.StockPershareindexAsync("000001.SZ", new TimeRangeQuery { Lt=1 }));
    await Run("stock_capital", async () => api.StockCapitalAsync("000001.SZ", new TimeRangeQuery { Lt=1 }));
    await Run("stock_shareholder_count", async () => api.StockShareholderCountAsync("000001.SZ", new TimeRangeQuery { Lt=1 }));
    await Run("stock_top_holders", async () => api.StockTopHoldersAsync("000001.SZ", new TimeRangeQuery { Lt=1 }));
    await Run("stock_flow_holders", async () => api.StockFlowHoldersAsync("000001.SZ", new TimeRangeQuery { Lt=1 }));
    await Run("stock_instrument", async () => api.StockInstrumentAsync("000001.SZ"));
    await Run("company", async () => api.CompanyAsync("gsjj", "000001"));
    await Run("company_profile", async () => api.CompanyProfileAsync("000001"));
    await Run("company_indexes", async () => api.CompanyIndexesAsync("000001"));
    await Run("company_directors", async () => api.CompanyDirectorsAsync("000001"));
    await Run("company_executives", async () => api.CompanyExecutivesAsync("000001"));
    await Run("company_supervisors", async () => api.CompanySupervisorsAsync("000001"));
    await Run("company_top10_holders", async () => api.CompanyTop10HoldersAsync("000001"));
    await Run("company_top10_float_holders", async () => api.CompanyTop10FloatHoldersAsync("000001"));
    await Run("company_holder_change", async () => api.CompanyHolderChangeAsync("000001"));
    await Run("company_dividends", async () => api.CompanyDividendsAsync("000001"));
    await Run("company_seo", async () => api.CompanySeoAsync("000001"));
    await Run("company_unlock", async () => api.CompanyUnlockAsync("000001"));
    await Run("company_fund_holding", async () => api.CompanyFundHoldingAsync("000001"));
    await Run("company_earnings_forecast", async () => api.CompanyEarningsForecastAsync("000001"));
    await Run("company_financial_index", async () => api.CompanyFinancialIndexAsync("000001"));
    await Run("company_quarter_profit", async () => api.CompanyQuarterProfitAsync("000001"));
    await Run("company_quarter_cashflow", async () => api.CompanyQuarterCashflowAsync("000001"));
    await Run("stock_pool", async () => api.StockPoolAsync("ztgc", "2026-01-15"));
    await Run("limit_up_pool", async () => api.LimitUpPoolAsync("2026-01-15"));
    await Run("limit_down_pool", async () => api.LimitDownPoolAsync("2026-01-15"));
    await Run("strong_pool", async () => api.StrongPoolAsync("2026-01-15"));
    await Run("new_stock_pool", async () => api.NewStockPoolAsync("2026-01-15"));
    await Run("broken_board_pool", async () => api.BrokenBoardPoolAsync("2026-01-15"));
    await Run("concept_tree", async () => api.ConceptTreeAsync());
    await Run("concepts_of_stock", async () => api.ConceptsOfStockAsync("000001"));
    await Run("stocks_of_concept", async () => api.StocksOfConceptAsync("sw_mt"));
    await Run("stock_interactiveqa", async () => api.StockInteractiveqaAsync("000001.SZ", new TimeRangeQuery { Lt=1 }));
    await Run("stock_announcement", async () => api.StockAnnouncementAsync("000001.SZ", new TimeRangeQuery { Lt=1 }));
    await Run("index_list", async () => api.IndexListAsync());
    await Run("index_history", async () => api.IndexHistoryAsync("000001.SH", "d", new TimeRangeQuery { Lt=1 }));
    await Run("index_latest", async () => api.IndexLatestAsync("000001.SH", "d", lt: 1));
    await Run("index_indicator", async () => api.IndexIndicatorAsync("macd", "000001.SH", "d", new TimeRangeQuery { Lt=1 }));
    await Run("index_macd", async () => api.IndexMacdAsync("000001.SH", "d", new TimeRangeQuery { Lt=1 }));
    await Run("index_ma", async () => api.IndexMaAsync("000001.SH", "d", new TimeRangeQuery { Lt=1 }));
    await Run("index_boll", async () => api.IndexBollAsync("000001.SH", "d", new TimeRangeQuery { Lt=1 }));
    await Run("index_kdj", async () => api.IndexKdjAsync("000001.SH", "d", new TimeRangeQuery { Lt=1 }));
    await Run("index_real_time", async () => api.IndexRealTimeAsync("000001.SH"));
    await Run("bj_stock_list", async () => api.BjStockListAsync());
    await Run("bj_index_list", async () => api.BjIndexListAsync());
    await Run("bj_stock_real_time", async () => api.BjStockRealTimeAsync("920000.BJ"));
    await Run("bj_stock_real_five", async () => api.BjStockRealFiveAsync("920000.BJ"));
    await Run("bj_index_real_time", async () => api.BjIndexRealTimeAsync("899050"));
    await Run("bj_history", async () => api.BjHistoryAsync("920000.BJ", "d", "n", new TimeRangeQuery { Lt=1 }));
    await Run("bj_financial", async () => api.BjFinancialAsync("balance", "920000.BJ", new TimeRangeQuery { Lt=1 }));
    await Run("bj_balance", async () => api.BjBalanceAsync("920000.BJ", new TimeRangeQuery { Lt=1 }));
    await Run("bj_income", async () => api.BjIncomeAsync("920000.BJ", new TimeRangeQuery { Lt=1 }));
    await Run("bj_cashflow", async () => api.BjCashflowAsync("920000.BJ", new TimeRangeQuery { Lt=1 }));
    await Run("bj_pershareindex", async () => api.BjPershareindexAsync("920000.BJ", new TimeRangeQuery { Lt=1 }));
    await Run("bj_capital", async () => api.BjCapitalAsync("920000.BJ", new TimeRangeQuery { Lt=1 }));
    await Run("bj_shareholder_count", async () => api.BjShareholderCountAsync("920000.BJ", new TimeRangeQuery { Lt=1 }));
    await Run("bj_top_holders", async () => api.BjTopHoldersAsync("920000.BJ", new TimeRangeQuery { Lt=1 }));
    await Run("bj_flow_holders", async () => api.BjFlowHoldersAsync("920000.BJ", new TimeRangeQuery { Lt=1 }));
    await Run("fund_list", async () => api.FundListAsync());
    await Run("etf_list", async () => api.EtfListAsync());
    await Run("fund_real_time", async () => api.FundRealTimeAsync("510300"));
    await Run("star_stock_list", async () => api.StarStockListAsync());
    await Run("star_real_time", async () => api.StarRealTimeAsync("688001"));
    await Run("star_real_five", async () => api.StarRealFiveAsync("688001"));
    api.Dispose();
    var fail = results.Count(r => !r.ok);
    var empty = results.Count(r => r.ok && r.empty);
    var payload = new { total = results.Count, fail, empty, fails = results.Where(r => !r.ok).ToList() };
    Console.WriteLine(JsonSerializer.Serialize(payload));
    if (fail > 0) Environment.Exit(2);
  }
  static int Size(object data) {
    if (data == null) return 0;
    if (data is JsonElement je) {
      if (je.ValueKind == JsonValueKind.Array) return je.GetArrayLength();
      if (je.ValueKind == JsonValueKind.Object) { int c=0; foreach (var _ in je.EnumerateObject()) c++; return c; }
      return 1;
    }
    if (data is System.Collections.ICollection col) return col.Count;
    return 1;
  }
  static bool IsEmpty(object data) {
    if (data == null) return true;
    if (data is JsonElement je) {
      if (je.ValueKind == JsonValueKind.Array) return je.GetArrayLength()==0;
      if (je.ValueKind == JsonValueKind.Null || je.ValueKind == JsonValueKind.Undefined) return true;
      if (je.ValueKind == JsonValueKind.Object) { foreach (var _ in je.EnumerateObject()) return false; return true; }
      return false;
    }
    if (data is System.Collections.ICollection col) return col.Count==0;
    return false;
  }
}