using System.Text.Json;
using TornadoAce_CJOnStyle.Services;

var workspace = AutomationWorkspace.CreateDemo();
Require(workspace.CueSheets.Count == 3, "Expected 3 cue sheet records.");
Require(workspace.ErpSchedules.Count == 3, "Expected 3 ERP schedule records.");
Require(workspace.ActiveTemplate.Mappings.Count == 5, "Expected 5 field mappings.");

var engine = new PocAutomationEngine(workspace);
var result = engine.RunFullPoc(0, workspace.ActiveTemplate);

Require(result.Selection.Record.CueSheetId == "CJ-20260817-001", "Unexpected selected cue sheet.");
Require(result.Schedule?.ProgramCode == "PGM-CHOI-0900", "ERP schedule was not matched.");
Require(result.GeneratedScene.VisibleTextObjects.Count == 5, "Scene snapshot did not include all visible text objects.");
Require(result.MappingValidations.All(validation => validation.Status == "통과"), "Mapping profile validation failed.");
Require(result.AdapterChecks.Count >= 3, "Tornado adapter checks were not produced.");
Require(result.InspectionSummary.Results.Count == workspace.CueSheets.Count, "Inspection did not cover all cue sheets.");
Require(result.InspectionSummary.ReviewCount == 2, "Expected 2 inspection review candidates.");
Require(result.LlmReviewRequests.Count == 2, "Expected 2 LLM review candidates.");

var logPath = new PocRunLogWriter().Write(result);
Require(File.Exists(logPath), "PoC audit log was not written.");

using var logDocument = JsonDocument.Parse(File.ReadAllText(logPath));
var root = logDocument.RootElement;
Require(root.GetProperty("cueSheetId").GetString() == result.Selection.Record.CueSheetId, "Log cue sheet id mismatch.");
Require(root.GetProperty("llmRequestCount").GetInt32() == result.LlmReviewRequests.Count, "Log LLM request count mismatch.");

Console.WriteLine("PoC smoke verification passed.");
Console.WriteLine(logPath);

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