import experimental from "@/data/experimental_data.json";
import kpi from "@/data/dashboard_kpis.json";
import kinetics from "@/data/kinetic_parameters.json";
import curves from "@/data/model_curves.json";
import catalyst from "@/data/catalyst_characterization.json";
import reactor from "@/data/reactor_configuration.json";

export function validateData(): string[] {
  const errors: string[] = [];
  if (experimental.length !== 40) errors.push(`experimental rows ${experimental.length} != 40`);
  const fittedVocs = ["n-hexane", "n-pentane", "benzene", "toluene"];
  for (const v of fittedVocs) {
    const n = experimental.filter((r) => r.pollutant === v).length;
    if (n !== 10) errors.push(`${v}: ${n} rows != 10`);
  }
  if (kpi.completed_experiments !== kpi.fitted_observations + 1)
    errors.push("completed != fitted+1");
  const pct = (kpi.completed_experiments / kpi.planned_experiments) * 100;
  if (Math.abs(pct - kpi.plan_completion_pct) > 0.01)
    errors.push("completion pct mismatch");
  const kineticNames = kinetics.map((k) => k.pollutant).sort();
  if (JSON.stringify(kineticNames) !== JSON.stringify([...fittedVocs].sort()))
    errors.push("kinetic pollutant mismatch");
  if (kinetics.some((k) => k.pollutant === "butanone"))
    errors.push("butanone must not have kinetic parameters");
  if (
    catalyst.crystalline_phase.anatase_pct + catalyst.crystalline_phase.rutile_pct !==
    100
  )
    errors.push("anatase+rutile != 100");
  for (const p of curves.points) {
    for (const key of [
      "n_hexane_conversion_pct",
      "n_pentane_conversion_pct",
      "benzene_conversion_pct",
      "toluene_conversion_pct",
    ]) {
      if (!(key in p)) errors.push(`curve point missing ${key}`);
    }
  }
  const publicText = JSON.stringify(experimental) + JSON.stringify(kinetics);
  if (publicText.toLowerCase().includes("cyclohexane"))
    errors.push("cyclohexane must not appear in public data");
  if (reactor.airflow_l_min !== kpi.airflow_l_min) errors.push("Q mismatch");
  if (reactor.solids_flux_kg_m2_s !== kpi.solids_flux_kg_m2_s)
    errors.push("Gs mismatch");
  return errors;
}
