using System.Text.Json;
using Yanbor.DataAI.Etl.Spark;

var runtime = DataAiSparkRuntimeLayout.Discover();
Require(runtime.LibraryJars.Count == 4, "Expected four DataAI library JARs.");
Require(runtime.LibraryJars.All(File.Exists), "A DataAI library JAR is missing.");
Require(File.Exists(runtime.CliJar), "The DataAI CLI JAR is missing.");
Require(
    DataAiFunctionCatalog.All.Any(item => item.Capability == "Matrix balancing"),
    "The function catalog does not include matrix balancing.");

var configuration = new DataAiQualityJobConfiguration
{
    SourceTable = "evaluation.orders",
    CleanTable = "evaluation.orders_clean",
    RejectedTable = "evaluation.orders_rejected",
    ProfileTable = "evaluation.orders_profiles",
    FindingsTable = "evaluation.orders_findings",
    Normalize = true,
    RecordKeyColumns = ["order_id"],
    Rules =
    [
        DataAiRuleSpec.Required("customer-required", "customer_id"),
        DataAiRuleSpec.Between("amount-range", "amount", 0, 100000)
    ],
    MinimumQualityScore = 90
};

using JsonDocument json = JsonDocument.Parse(configuration.ToJson());
Require(json.RootElement.GetProperty("sourceTable").GetString() == "evaluation.orders",
    "NuGet configuration JSON is invalid.");
var command = DataAiSparkSubmitBuilder.BuildQualityJob(
    "evaluation-job.json",
    runtime,
    new DataAiSparkSubmitOptions { Master = "local[2]" });
Require(command.Arguments.Contains("com.dataai.etl.spark.cli.DataAiJob"),
    "The generated command does not contain the DataAI CLI main class.");

Console.WriteLine(
    $"DATAAI_NUGET_SMOKE_TEST_PASS functions={DataAiFunctionCatalog.All.Count} jars={runtime.LibraryJars.Count}");

static void Require(bool condition, string message)
{
    if (!condition)
    {
        throw new InvalidOperationException(message);
    }
}
