#!/usr/bin/env python3
"""Remove flagged schemes from SchemeSeeder.php and print remaining count."""
import re
from pathlib import Path

path = Path(r"E:\Startup\vedica\database\seeders\SchemeSeeder.php")
text = path.read_text(encoding="utf-8")

# Remove these (2 FAIL + 5 discrete WARN). Keep savings (WARN was interest wording only).
REMOVE = {
    "PMEGP",
    "Stand-Up India",
    "SISFS",
    "AHIDF",
    "DAY-NULM",
    "NSAP",
    "PMKVY",
}

# Parse array items that start with [ and contain 'short' => '...'
# Find the return [ ... ]; block of verifiedSchemes
start = text.index("return [")
end = text.rindex("];", start)
header = text[: start + len("return [")]
footer = text[end:]  # starts with ];
body = text[start + len("return [") : end]

# Split top-level scheme arrays by tracking brackets; skip // comments
items = []
i = 0
n = len(body)
while i < n:
    while i < n and body[i] in " \t\r\n,":
        i += 1
    if i >= n:
        break
    # skip line comments
    if body.startswith("//", i):
        nl = body.find("\n", i)
        i = n if nl < 0 else nl + 1
        continue
    if body[i] != "[":
        raise SystemExit(f"Expected '[' at {i}: {body[i:i+40]!r}")
    depth = 0
    j = i
    in_str = False
    str_ch = ""
    escape = False
    while j < n:
        ch = body[j]
        if in_str:
            if escape:
                escape = False
            elif ch == "\\":
                escape = True
            elif ch == str_ch:
                in_str = False
        else:
            if ch in ("'", '"'):
                in_str = True
                str_ch = ch
            elif ch == "[":
                depth += 1
            elif ch == "]":
                depth -= 1
                if depth == 0:
                    j += 1
                    items.append(body[i:j])
                    i = j
                    break
        j += 1
    else:
        raise SystemExit("Unbalanced brackets")

kept = []
removed = []
for item in items:
    m = re.search(r"'short'\s*=>\s*'([^']+)'", item)
    if not m:
        raise SystemExit(f"No short in item: {item[:80]}")
    short = m.group(1)
    if short in REMOVE:
        removed.append(short)
    else:
        kept.append(item)

missing = REMOVE - set(removed)
if missing:
    raise SystemExit(f"Not found for removal: {missing}")

# Keep batch comments out; rebuild clean list
new_body = "\n            " + ",\n            ".join(kept) + ",\n        "
new_text = header + new_body + footer

# Update comment
new_text = new_text.replace(
    " * Verified Central schemes only (Batches 1–10).",
    " * Verified Central schemes only (Batches 1–10; WARN/FAIL temporarily removed).",
)

path.write_text(new_text, encoding="utf-8")
print(f"Removed ({len(removed)}): {', '.join(removed)}")
print(f"Kept: {len(kept)}")
