/* global window */
// Shared data - derived from M1 spec + existing rac-claude codebase

const DEFAULT_GAP = {
  activeIngredient: "Mesotrione",
  applicationRate: "0.188",
  applicationRateUnit: "lb ai/A",
  phi: "7",
  numberOfApps: "2",
  applicationMethod: "Foliar broadcast",
  formulationType: "SC (suspension concentrate)",
  geographicRegions: ["I", "II", "V", "X"],
  targetPest: "Annual broadleaf weeds, grasses",
  growthStage: "V2–V6",
};

const APPLICATION_METHODS = [
  "Foliar broadcast", "Foliar banded", "Soil broadcast",
  "Soil banded", "Seed treatment", "Chemigation", "Aerial", "Other",
];

const FORMULATION_TYPES = [
  "SC (suspension concentrate)", "EC (emulsifiable concentrate)",
  "WDG (water-dispersible granule)", "WP (wettable powder)",
  "CS (capsule suspension)", "SL (soluble liquid)", "Other",
];

const REGIONS = [
  { id: "I",    name: "Northeast",          required: true,  states: "CT, MA, ME, NH, NJ, NY, PA, RI, VT" },
  { id: "II",   name: "North Central",      required: true,  states: "IL, IN, MI, MN, OH, WI" },
  { id: "III",  name: "Northern Plains",    required: false, states: "IA, KS, MO, NE, ND, SD" },
  { id: "IV",   name: "Delta",              required: false, states: "AR, LA, MS" },
  { id: "V",    name: "South Central",      required: true,  states: "AL, GA, KY, NC, SC, TN, VA" },
  { id: "VI",   name: "Southeast / Florida",required: false, states: "FL" },
  { id: "VII",  name: "Southwest",          required: false, states: "AZ, NM, OK, TX" },
  { id: "VIII", name: "Mountain",           required: false, states: "CO, ID, MT, NV, UT, WY" },
  { id: "IX",   name: "Northwest",          required: false, states: "OR, WA" },
  { id: "X",    name: "Pacific / California",required: true, states: "CA" },
  { id: "XI",   name: "Hawaii",             required: false, states: "HI" },
  { id: "XII",  name: "Puerto Rico / VI",   required: false, states: "PR, VI" },
  { id: "XIII", name: "Alaska",             required: false, states: "AK" },
];

const CROP_GROUPS = [
  { id: 1,  name: "Root and Tuber Vegetables", crop: "Potato",        active: false },
  { id: 2,  name: "Bulb Vegetables",           crop: "Onion",         active: false },
  { id: 3,  name: "Leafy Vegetables",          crop: "Lettuce",       active: false },
  { id: 4,  name: "Brassica Vegetables",       crop: "Broccoli",      active: false },
  { id: 5,  name: "Fruiting Vegetables",       crop: "Tomato",        active: false },
  { id: 6,  name: "Cucurbit Vegetables",       crop: "Cucumber",      active: false },
  { id: 7,  name: "Citrus Fruits",             crop: "Orange",        active: false },
  { id: 8,  name: "Pome Fruits",               crop: "Apple",         active: false },
  { id: 9,  name: "Stone Fruits",              crop: "Peach",         active: false },
  { id: 10, name: "Berries & Small Fruits",    crop: "Strawberry",    active: false },
  { id: 11, name: "Tropical & Subtropical",    crop: "Avocado",       active: false },
  { id: 12, name: "Tree Nuts",                 crop: "Almond",        active: false },
  { id: 13, name: "Oilseed Crops",             crop: "Soybean",       active: false },
  { id: 14, name: "Legume Vegetables",         crop: "Snap Bean",     active: false },
  { id: 15, name: "Cereal Grains",             crop: "Sweet Corn",    active: true  },
  { id: 16, name: "Foliage of Cereal Grains",  crop: "Corn stover",   active: false },
  { id: 17, name: "Grass Forage & Hay",        crop: "Bermudagrass",  active: false },
  { id: 18, name: "Non-Grass Animal Feeds",    crop: "Alfalfa",       active: false },
  { id: 19, name: "Herbs & Spices",            crop: "Basil",         active: false },
  { id: 20, name: "Cotton & Fibers",           crop: "Cotton",        active: false },
];

const TREE_NODES = [
  { id: "N0", level: "entry",      check: null,           question: "Is the assessed crop Fresh Sweet Corn?",                              yes: "N1",   no: "LOOP" },
  { id: "N1", level: "individual", check: "number",       question: "Does the NUMBER of trials qualify as USEPA Individual Crop?",         yes: "N2",   no: "FAIL_A" },
  { id: "N2", level: "individual", check: "distribution", question: "Does the DISTRIBUTION qualify as USEPA Individual Crop?",             yes: "N3",   no: "FAIL_B" },
  { id: "N3", level: "group",      check: "number",       question: "Does the NUMBER of trials qualify as USEPA Crop Group?",              yes: "N4",   no: "N7" },
  { id: "N4", level: "group",      check: "distribution", question: "Does the DISTRIBUTION qualify as USEPA Crop Group?",                  yes: "N5",   no: "N7" },
  { id: "N5", level: "subgroup",   check: "number",       question: "Does the NUMBER of trials qualify as USEPA Crop Subgroup?",           yes: "N6",   no: "FAIL_C" },
  { id: "N6", level: "subgroup",   check: "distribution", question: "Does the DISTRIBUTION qualify as USEPA Crop Subgroup?",               yes: "PASS_A", no: "FAIL_D" },
  { id: "N7", level: "subgroup",   check: "number",       question: "Does the NUMBER of trials qualify as USEPA Crop Subgroup (fallback)?",yes: "N8",   no: "FAIL_E" },
  { id: "N8", level: "subgroup",   check: "distribution", question: "Does the DISTRIBUTION qualify as USEPA Crop Subgroup (fallback)?",    yes: "PASS_B", no: "FAIL_F" },
];

const DEMO_PATHS = {
  pass: {
    outcome: "pass",
    nodes: ["N0","N1","N2","N3","N4","N5","N6","PASS_1"],
    failingRule: null,
  },
  fail: {
    outcome: "fail",
    nodes: ["N0","N1","N2","N3","N4","N7","N8","FAIL_8"],
    failingNode: "N8",
    failingRule: {
      nodeId: "N8",
      ruleText: "Does the distribution of successful trials qualify as a USEPA Crop Subgroup (fallback)?",
      explanation: "Trials present in 2 of 4 required USEPA regions (Regions II and V only). No trial data in Region I (Northeast) or Region X (Pacific/California).",
    },
  },
};

const REPORT_DATA = {
  pass: {
    submissionRef: "DEMO-001",
    totalTrials: 8,
    trialCounts: [
      { criterion: "Individual Crop (Sweet Corn)", required: "≥ 5",  submitted: "8", result: "PASS" },
      { criterion: "Crop Group 15 (Cereal Grains)", required: "≥ 12", submitted: "8", result: "NOT_REQUIRED",
        note: "Group-level count is informational. Petition is for an individual-crop tolerance.",
        citation: "Per 40 CFR §180.41(c) and OPPTS 860.1500 §III.B, group-level minimums apply only when seeking a Crop Group tolerance. For an individual-crop petition, the binding count is the Individual Crop minimum (≥ 5 trials)." },
      { criterion: "Crop Subgroup 15-A",            required: "≥ 8",  submitted: "8", result: "NOT_REQUIRED",
        note: "Subgroup count is informational. Petition is for an individual-crop tolerance.",
        citation: "OPPTS 860.1500 §III.B treats subgroup-level counts as advisory unless a Crop Subgroup tolerance is being requested. The binding trial-count check on this petition is the Individual Crop minimum (≥ 5)." },
    ],
    distribution: [
      { region: "I",  name: "Northeast",          present: 2, states: "NY, PA",     result: "PASS" },
      { region: "II", name: "North Central",      present: 2, states: "OH, IL",     result: "PASS" },
      { region: "V",  name: "South Central",      present: 2, states: "GA, NC",     result: "PASS" },
      { region: "X",  name: "Pacific / California",present: 2, states: "CA, CA",    result: "PASS" },
    ],
    overallResult: "PASS",
  },
  fail: {
    submissionRef: "DEMO-002",
    totalTrials: 6,
    trialCounts: [
      { criterion: "Individual Crop (Sweet Corn)", required: "≥ 5",  submitted: "6", result: "PASS" },
      { criterion: "Crop Group 15 (Cereal Grains)", required: "≥ 12", submitted: "6", result: "NOT_REQUIRED",
        note: "Group-level count is informational. Petition is for an individual-crop tolerance.",
        citation: "Per 40 CFR §180.41(c) and OPPTS 860.1500 §III.B, group-level minimums apply only when seeking a Crop Group tolerance. For an individual-crop petition, the binding count is the Individual Crop minimum (≥ 5 trials)." },
      { criterion: "Crop Subgroup 15-A",            required: "≥ 8",  submitted: "6", result: "NOT_REQUIRED",
        note: "Subgroup count is informational. Petition is for an individual-crop tolerance.",
        citation: "OPPTS 860.1500 §III.B treats subgroup-level counts as advisory unless a Crop Subgroup tolerance is being requested. The binding trial-count check on this petition is the Individual Crop minimum (≥ 5)." },
    ],
    distribution: [
      { region: "I",  name: "Northeast",          present: 0, states: "-",                result: "FAIL" },
      { region: "II", name: "North Central",      present: 4, states: "OH, IL, IN, MN",   result: "PASS" },
      { region: "V",  name: "South Central",      present: 2, states: "GA, NC",           result: "PASS" },
      { region: "X",  name: "Pacific / California",present: 0, states: "-",               result: "FAIL" },
    ],
    overallResult: "FAIL",
  },
};

const SAMPLE_TRIALS = {
  pass: [
    { id: "T-001", state: "NY", county: "Suffolk",    region: "I",  crop: "Sweet Corn", variety: "Jubilee",          rate: 0.188, apps: 2, phi: 7, harvest: "2024-08-12", residue: 0.04 },
    { id: "T-002", state: "PA", county: "Lancaster",  region: "I",  crop: "Sweet Corn", variety: "Silver Queen",     rate: 0.188, apps: 2, phi: 7, harvest: "2024-08-19", residue: 0.02 },
    { id: "T-003", state: "OH", county: "Franklin",   region: "II", crop: "Sweet Corn", variety: "Incredible",       rate: 0.188, apps: 2, phi: 7, harvest: "2024-08-22", residue: 0.05 },
    { id: "T-004", state: "IL", county: "Champaign",  region: "II", crop: "Sweet Corn", variety: "Jubilee",          rate: 0.188, apps: 2, phi: 7, harvest: "2024-08-27", residue: 0.03 },
    { id: "T-005", state: "GA", county: "Tift",       region: "V",  crop: "Sweet Corn", variety: "Peaches & Cream",  rate: 0.188, apps: 2, phi: 7, harvest: "2024-07-30", residue: 0.06 },
    { id: "T-006", state: "NC", county: "Wake",       region: "V",  crop: "Sweet Corn", variety: "Silver Queen",     rate: 0.188, apps: 2, phi: 7, harvest: "2024-08-05", residue: 0.04 },
    { id: "T-007", state: "CA", county: "Fresno",     region: "X",  crop: "Sweet Corn", variety: "Jubilee",          rate: 0.188, apps: 2, phi: 7, harvest: "2024-09-04", residue: 0.02 },
    { id: "T-008", state: "CA", county: "Monterey",   region: "X",  crop: "Sweet Corn", variety: "Incredible",       rate: 0.188, apps: 2, phi: 7, harvest: "2024-09-11", residue: 0.03 },
  ],
  fail: [
    { id: "T-101", state: "OH", county: "Franklin",   region: "II", crop: "Sweet Corn", variety: "Jubilee",          rate: 0.188, apps: 2, phi: 7, harvest: "2024-08-21", residue: 0.05 },
    { id: "T-102", state: "IL", county: "Champaign",  region: "II", crop: "Sweet Corn", variety: "Silver Queen",     rate: 0.188, apps: 2, phi: 7, harvest: "2024-08-26", residue: 0.04 },
    { id: "T-103", state: "IN", county: "Tippecanoe", region: "II", crop: "Sweet Corn", variety: "Incredible",       rate: 0.188, apps: 2, phi: 7, harvest: "2024-08-29", residue: 0.03 },
    { id: "T-104", state: "MN", county: "Blue Earth", region: "II", crop: "Sweet Corn", variety: "Jubilee",          rate: 0.188, apps: 2, phi: 7, harvest: "2024-09-02", residue: 0.06 },
    { id: "T-105", state: "GA", county: "Tift",       region: "V",  crop: "Sweet Corn", variety: "Peaches & Cream",  rate: 0.188, apps: 2, phi: 7, harvest: "2024-07-29", residue: 0.04 },
    { id: "T-106", state: "NC", county: "Wake",       region: "V",  crop: "Sweet Corn", variety: "Silver Queen",     rate: 0.188, apps: 2, phi: 7, harvest: "2024-08-04", residue: 0.05 },
  ],
};

const DEMO_SUBMISSIONS = [
  {
    id: "DEMO-001", company: "AgriDemo Holdings", contact: "Dr. M. Hartmann", submittedAt: "Apr 28, 2026 · 14:22",
    activeSubstance: "Mesotrione", crop: "Sweet Corn",
    submissionType: "New tolerance petition - Group 15", regNumber: "DEMO-REG-001",
    trialCount: "PASS", distribution: "PASS", overallResult: "PASS",
    failingRule: null, status: "Pending Review", scenario: "pass",
    demoFile: "sweet_corn_trials_PASS.csv", trials: 8, regions: 4,
  },
  {
    id: "DEMO-002", company: "GreenField Sciences", contact: "L. Okafor", submittedAt: "Apr 30, 2026 · 09:08",
    activeSubstance: "Mesotrione", crop: "Sweet Corn",
    submissionType: "New tolerance petition - Group 15", regNumber: "DEMO-REG-002",
    trialCount: "PASS", distribution: "FAIL", overallResult: "FAIL",
    failingRule: "N4 - Distribution does not qualify at USEPA Crop Group level. Trials present in 2 of 4 required regions.",
    status: "Pending Review", scenario: "fail",
    demoFile: "sweet_corn_trials_FAIL.csv", trials: 6, regions: 2,
  },
  // historical, greyed-out rows for richness
  {
    id: "REG-2025-114", company: "NorthStar AgroTech", contact: "-", submittedAt: "Mar 02, 2026",
    activeSubstance: "S-metolachlor", crop: "Sweet Corn",
    submissionType: "Tolerance modification - Group 15", regNumber: "-",
    trialCount: "PASS", distribution: "PASS", overallResult: "PASS",
    failingRule: null, status: "Approved", scenario: "archive",
    demoFile: "-", trials: 12, regions: 4,
  },
  {
    id: "REG-2025-098", company: "Meridian CropCare", contact: "-", submittedAt: "Feb 11, 2026",
    activeSubstance: "Atrazine", crop: "Sweet Corn",
    submissionType: "New tolerance petition - Group 15", regNumber: "-",
    trialCount: "FAIL", distribution: "-", overallResult: "FAIL",
    failingRule: "N1 - Insufficient trial count at Individual Crop level.",
    status: "Rejected", scenario: "archive",
    demoFile: "-", trials: 3, regions: 2,
  },
];

const VALIDATION_CHECKS = [
  { id: 1, label: "File format valid (.csv)",          detail: "11 columns detected, RFC 4180 compliant", result: "PASS" },
  { id: 2, label: "Required columns present",          detail: "All 11 required columns matched on row 1", result: "PASS" },
  { id: 3, label: "Data types correct",                detail: "No type mismatches across data rows",      result: "PASS" },
  { id: 4, label: "No missing required values",        detail: "All cells populated",                       result: "PASS" },
  { id: 5, label: "Trial identifiers unique",          detail: "No duplicate trial_id values",              result: "PASS" },
  { id: 6, label: "Row count matches declared trials", detail: "Header declared trials = data rows",        result: "PASS" },
  { id: 7, label: "Region codes valid",                detail: "All usepa_region values within I-X",        result: "PASS" },
  { id: 8, label: "Date format valid (ISO 8601)",      detail: "All harvest_date values YYYY-MM-DD",        result: "PASS" },
];

const CSV_COLUMNS = [
  "trial_id", "state", "county", "usepa_region", "crop", "variety",
  "application_rate_lb_ai_per_a", "number_of_applications",
  "phi_days", "harvest_date", "residue_ppm",
];

Object.assign(window, {
  DEFAULT_GAP, APPLICATION_METHODS, FORMULATION_TYPES, REGIONS, CROP_GROUPS,
  TREE_NODES, DEMO_PATHS, REPORT_DATA, SAMPLE_TRIALS, DEMO_SUBMISSIONS,
  VALIDATION_CHECKS, CSV_COLUMNS,
});
