import { access, readFile } from "node:fs/promises";
import { dirname, resolve } from "node:path";
import { fileURLToPath } from "node:url";

const root = resolve(dirname(fileURLToPath(import.meta.url)), "..");
const parse = async (path) => JSON.parse(await readFile(resolve(root, path), "utf8"));

const registry = await parse("registry/registry.json");
const sources = await parse("registry/sources.json");
const components = await parse("registry/components.json");
const candidates = await parse("registry/candidates.json");
const icons = await parse("registry/icons.json");
const pages = await parse("registry/pages.json");
const applicationManifestSchema = await parse("registry/schemas/application-manifest-v0.1.schema.json");
const designProfileSchema = await parse("registry/schemas/design-profile-v0.1.schema.json");
const mapSceneFixtureSchema = await parse("registry/schemas/map-scene-fixture-v0.1.schema.json");
const mapLodPolicySchema = await parse("registry/schemas/map-lod-policy-v0.1.schema.json");

const failures = [];
const requireValue = (condition, message) => {
  if (!condition) failures.push(message);
};

requireValue(registry.schemaVersion === "1.0.0", "registry schemaVersion must be 1.0.0");
requireValue(registry.libraryVersion, "registry libraryVersion is required");
requireValue(Array.isArray(registry.packages) && registry.packages.length >= 4, "registry must list all packages");
requireValue(Array.isArray(sources.sources) && sources.sources.length >= 6, "source audit is incomplete");
requireValue(Array.isArray(components.components) && components.components.length >= 10, "component registry is incomplete");
requireValue(Array.isArray(candidates.candidates) && candidates.candidates.length >= 10, "candidate registry is incomplete");
requireValue(Array.isArray(icons.groups) && icons.groups.length >= 5, "icon registry is incomplete");
requireValue(pages.schemaVersion === "0.1.0", "page registry schemaVersion must be 0.1.0");
requireValue(Array.isArray(pages.templates) && pages.templates.length >= 1, "page registry must contain at least one template");
requireValue(registry.applicationManifestSchema === "schemas/application-manifest-v0.1.schema.json", "application manifest schema must be registered");
requireValue(registry.designProfileSchema === "schemas/design-profile-v0.1.schema.json", "design profile schema must be registered");
requireValue(registry.mapSceneFixtureSchema === "schemas/map-scene-fixture-v0.1.schema.json", "map scene fixture schema must be registered");
requireValue(registry.mapLodPolicySchema === "schemas/map-lod-policy-v0.1.schema.json", "map LOD policy schema must be registered");
requireValue(applicationManifestSchema.properties?.schemaVersion?.const === "0.1.0", "application manifest schema version must be 0.1.0");
requireValue(designProfileSchema.properties?.schemaVersion?.const === "0.1.0", "design profile schema version must be 0.1.0");
requireValue(mapSceneFixtureSchema.properties?.schemaVersion?.const === "0.1.0", "map scene fixture schema version must be 0.1.0");
requireValue(mapLodPolicySchema.properties?.schemaVersion?.const === "0.1.0", "map LOD policy schema version must be 0.1.0");

const ids = components.components.map((component) => component.id);
requireValue(new Set(ids).size === ids.length, "component ids must be unique");
const candidateIds = candidates.candidates.map((component) => component.id);
requireValue(new Set(candidateIds).size === candidateIds.length, "candidate ids must be unique");
requireValue(candidateIds.every((id) => !ids.includes(id)), "a component cannot be both baseline and candidate");
const iconNames = icons.groups.flatMap((group) => group.names ?? []);
requireValue(iconNames.length >= 35, "icon registry must contain the audited baseline set");
requireValue(new Set(iconNames).size === iconNames.length, "icon names must be unique across groups");
const pageTemplateKeys = pages.templates.map((template) => `${template.id}@${template.version}`);
requireValue(new Set(pageTemplateKeys).size === pageTemplateKeys.length, "page template id/version pairs must be unique");

for (const template of pages.templates) {
  requireValue(template.schemaVersion === "0.1.0", `unsupported page template schema: ${template.id ?? "unknown"}`);
  requireValue(template.id && template.version && template.title && template.page, `page template entry is incomplete: ${template.id ?? "unknown"}`);
  requireValue(Array.isArray(template.features), `page template features are missing: ${template.id ?? "unknown"}`);
  requireValue(Array.isArray(template.slots), `page template slots are missing: ${template.id ?? "unknown"}`);
  requireValue(Array.isArray(template.actions), `page template actions are missing: ${template.id ?? "unknown"}`);

  if (template.contracts) {
    requireValue(typeof template.contracts.fixtureSchema === "string", `page template fixture schema is missing: ${template.id ?? "unknown"}`);
    requireValue(Array.isArray(template.contracts.acceptanceFixtures) && template.contracts.acceptanceFixtures.length >= 1, `page template acceptance fixtures are missing: ${template.id ?? "unknown"}`);

    for (const fixturePath of template.contracts.acceptanceFixtures ?? []) {
      let fixture;
      try {
        fixture = await parse(fixturePath);
      } catch {
        failures.push(`missing or invalid acceptance fixture: ${fixturePath}`);
        continue;
      }

      requireValue(fixture.schemaVersion === "0.1.0", `unsupported fixture schema: ${fixturePath}`);
      requireValue(fixture.template?.id === template.id && fixture.template?.version === template.version, `fixture template mismatch: ${fixturePath}`);
      requireValue(["ready", "loading", "empty", "stale", "error", "offline"].includes(fixture.state), `unsupported fixture state: ${fixturePath}`);
      requireValue(Array.isArray(fixture.capabilities), `fixture capabilities are missing: ${fixturePath}`);
      requireValue(Array.isArray(fixture.styleProfiles), `fixture style profiles are missing: ${fixturePath}`);
      requireValue(Array.isArray(fixture.acceptance) && fixture.acceptance.length >= 1, `fixture acceptance checks are missing: ${fixturePath}`);

      const collections = ["layers", "places", "movingObjects", "stations", "routes", "tracks", "zones"];
      requireValue(collections.every((name) => Array.isArray(fixture.scene?.[name])), `fixture scene collections are incomplete: ${fixturePath}`);

      const entities = collections.flatMap((name) => fixture.scene?.[name] ?? []);
      const entityIds = entities.map((entity) => entity.id);
      requireValue(entityIds.every(Boolean), `fixture entity id is missing: ${fixturePath}`);
      requireValue(new Set(entityIds).size === entityIds.length, `fixture entity ids must be unique: ${fixturePath}`);

      const styleIds = fixture.styleProfiles?.map((style) => style.id) ?? [];
      requireValue(styleIds.every(Boolean), `fixture style profile id is missing: ${fixturePath}`);
      requireValue(new Set(styleIds).size === styleIds.length, `fixture style profile ids must be unique: ${fixturePath}`);

      const referencedStyleIds = entities.flatMap((entity) => [entity.styleProfileId, entity.pinStyleProfileId, entity.label?.styleProfileId]).filter(Boolean);
      requireValue(referencedStyleIds.every((id) => styleIds.includes(id)), `fixture references an unknown style profile: ${fixturePath}`);

      const selectedEntityId = fixture.selection?.entityId;
      requireValue(selectedEntityId === null || entityIds.includes(selectedEntityId), `fixture selection references an unknown entity: ${fixturePath}`);

      for (const layer of fixture.scene?.layers ?? []) {
        if (!layer.lodLevels) continue;
        const levels = [...layer.lodLevels].sort((a, b) => a.minRange - b.minRange);
        requireValue(levels.every((level) => level.maxRange > level.minRange), `fixture LOD range is invalid: ${fixturePath}#${layer.id}`);
        requireValue(levels.every((level, index) => index === 0 || level.minRange === levels[index - 1].maxRange), `fixture LOD ranges must be contiguous: ${fixturePath}#${layer.id}`);
      }
    }

    if (template.contracts.lodPolicies) {
      let policySet;
      try {
        policySet = await parse(template.contracts.lodPolicies);
      } catch {
        failures.push(`missing or invalid LOD policy set: ${template.contracts.lodPolicies}`);
      }

      if (policySet) {
        requireValue(policySet.schemaVersion === "0.1.0", `unsupported LOD policy schema: ${template.contracts.lodPolicies}`);
        requireValue(policySet.template?.id === template.id && policySet.template?.version === template.version, `LOD policy template mismatch: ${template.contracts.lodPolicies}`);
        requireValue(policySet.metric === "camera-range-meters", `LOD policy metric must be provider-neutral camera range: ${template.contracts.lodPolicies}`);
        requireValue(policySet.intervalRule === "min-inclusive-max-exclusive", `LOD interval rule is unsupported: ${template.contracts.lodPolicies}`);
        requireValue(Array.isArray(policySet.policies) && policySet.policies.length >= 1, `LOD policies are missing: ${template.contracts.lodPolicies}`);

        const policyIds = policySet.policies?.map((policy) => policy.id) ?? [];
        requireValue(new Set(policyIds).size === policyIds.length, `LOD policy ids must be unique: ${template.contracts.lodPolicies}`);

        for (const policy of policySet.policies ?? []) {
          const bands = [...(policy.bands ?? [])].sort((a, b) => a.minRange - b.minRange);
          requireValue(bands.length >= 1, `LOD policy bands are missing: ${policy.id}`);
          requireValue(new Set(bands.map((band) => band.id)).size === bands.length, `LOD band ids must be unique: ${policy.id}`);
          requireValue(bands.every((band) => band.maxRange > band.minRange), `LOD range is invalid: ${policy.id}`);
          requireValue(bands.every((band, index) => index === 0 || band.minRange === bands[index - 1].maxRange), `LOD ranges must be contiguous: ${policy.id}`);
          requireValue(typeof policy.hysteresisRatio === "number" && policy.hysteresisRatio >= 0 && policy.hysteresisRatio <= 0.5, `LOD hysteresis ratio is invalid: ${policy.id}`);
        }
      }
    }
  }
}

for (const component of components.components) {
  requireValue(component.id && component.status && component.summary, `component entry is incomplete: ${component.id ?? "unknown"}`);
}

for (const pkg of registry.packages) {
  try {
    await access(resolve(root, pkg.path, "package.json"));
  } catch {
    failures.push(`missing package manifest: ${pkg.path}/package.json`);
  }
}

for (const path of Object.values(registry.documentation)) {
  try {
    await access(resolve(root, "registry", path));
  } catch {
    failures.push(`missing documentation target: ${path}`);
  }
}

if (failures.length > 0) {
  console.error(failures.map((failure) => `- ${failure}`).join("\n"));
  process.exit(1);
}

console.log(`Registry valid: ${components.components.length} components, ${iconNames.length} icons, ${pages.templates.length} page templates, ${candidates.candidates.length} candidates, ${sources.sources.length} audited sources.`);
