import { readFileSync } from "node:fs";

import { describe, expect, it } from "vitest";

import { GenerationModelConfigurationCatalog, type ModelConfigurationView } from "../../apps/api/src/model-configuration.js";

describe("POSTV1-04 generation runtime wiring", () => {
  it("constructs, injects and closes the production generation submission service", () => {
    const main = readFileSync("apps/api/src/main.ts", "utf8");

    expect(main).toContain('import { GenerationSubmissionService } from "./generation-submission.js";');
    expect(main).toContain("let generations: GenerationSubmissionService | undefined;");
    expect(main).toContain("generations = new GenerationSubmissionService({");
    expect(main).toContain("models: new GenerationModelConfigurationCatalog(models),");
    expect(main).toContain("...(generations ? { generations } : {}),");
    expect(main).toContain("generations?.close();");
  });

  it("maps the current model configuration into the generation submission contract", () => {
    const configuration: ModelConfigurationView = {
      config_set_version: 7,
      configured_default_model_id: "gemini-3.1-flash-image-preview",
      recommended_model_id: "gemini-3.1-flash-image-preview",
      models: [{
        config_version: 3,
        contract_evidence_ref: "fixture-contract",
        contract_validation_status: "verified",
        credit_cost: 2,
        display_name: "Fixture model",
        enabled: true,
        error_mapping_profile: {},
        gateway_account_ref: "fixture-gateway",
        is_default: true,
        model_id: "gemini-3.1-flash-image-preview",
        prompt_max_length: 1_000,
        recommendation_priority: 1,
        reference_limits: { max_file_bytes: 10, max_files: 2, max_total_bytes: 20 },
        route_profile: {},
        runtime_availability: { available_for_new_jobs: true, checked_at: "2026-08-05T00:00:00.000Z", reason: "available" },
        safety_source: "provider",
        supported_ratios: ["3:4", "invalid"],
      }],
    };
    const catalog = new GenerationModelConfigurationCatalog({ read: () => configuration });

    expect(catalog.readModel("gemini-3.1-flash-image-preview")).toEqual({
      configSetVersion: 7,
      configVersion: 3,
      contractValidationStatus: "verified",
      creditCost: 2,
      enabled: true,
      modelId: "gemini-3.1-flash-image-preview",
      promptMaxLength: 1_000,
      referenceLimits: { maxFileBytes: 10, maxFiles: 2, maxTotalBytes: 20 },
      runtimeAvailability: { availableForNewJobs: true, reason: null },
      supportedRatios: ["3:4"],
    });
    expect(catalog.readModel("missing")).toBeUndefined();
  });

  it("maps model and contract blocks into generation error categories", () => {
    const configuration = {
      config_set_version: 1,
      configured_default_model_id: "gemini-3.1-flash-image-preview",
      recommended_model_id: null,
      models: [],
    } satisfies ModelConfigurationView;
    const catalog = new GenerationModelConfigurationCatalog({ read: () => configuration });
    const base = {
      config_version: 1,
      contract_evidence_ref: null,
      contract_validation_status: "verified" as const,
      credit_cost: 1,
      display_name: "Fixture model",
      enabled: true,
      error_mapping_profile: {},
      gateway_account_ref: "fixture-gateway",
      is_default: true,
      model_id: "gemini-3.1-flash-image-preview" as const,
      prompt_max_length: 1_000,
      recommendation_priority: 1,
      reference_limits: { max_file_bytes: 10, max_files: 2, max_total_bytes: 20 },
      route_profile: {},
      safety_source: "provider",
      supported_ratios: ["3:4"],
    };

    configuration.models = [{
      ...base,
      runtime_availability: { available_for_new_jobs: false, checked_at: "2026-08-05T00:00:00.000Z", reason: "contract_blocked" },
    }];
    expect(catalog.readModel(base.model_id)?.runtimeAvailability.reason).toBe("gateway_contract_invalid");

    configuration.models = [{
      ...base,
      runtime_availability: { available_for_new_jobs: false, checked_at: "2026-08-05T00:00:00.000Z", reason: "worker_degraded" },
    }];
    expect(catalog.readModel(base.model_id)?.runtimeAvailability.reason).toBe("model_disabled");
  });
});
