// hako_source_checker.hako — HakoSourceCheckerBox
// Purpose: Lint/structure checks for .hako sources (Phase 21.3)
// Rules (MVP):
//  HC001: Forbid top-level assignment inside static box (before any method)
//  HC002: Forbid include "..." lines (using+alias only)
//  HC003: Using must be quoted (using "pkg.name" as Alias)
//  HC004: Encourage JsonFragBox helpers for JSON scans (warn when substring/indexOf used with seg/inst_json)

using selfhost.shared.common.string_helpers as Str
using selfhost.shared.json.utils.json_frag as JsonFragBox

static box HakoSourceCheckerBox {
  // Public: check a file path. Returns 0 on success; >0 on issues.
  check_file(path) {
    local f = new FileBox()
    if f.open(path) == 0 { print("[lint/error] cannot open: " + path); return 2 }
    local text = f.read(); f.close()
    return me.check_source(text, path)
  }

  // Public: check raw source
  check_source(text, path) {
    local issues = new ArrayBox()
    me._rule_include_forbidden(text, path, issues)
    me._rule_using_quoted(text, path, issues)
    me._rule_static_top_assign(text, path, issues)
    me._rule_jsonfrag_usage(text, path, issues)
    local n = issues.size()
    if n > 0 {
      local i=0; while i<n { print(issues.get(i)); i=i+1 }
      return n
    }
    return 0
  }

  // HC002: include is forbidden
  _rule_include_forbidden(text, path, out) {
    local lines = text.split("\n")
    local i=0; while i<lines.size() { local ln=lines.get(i); local trimmed=me._ltrim(ln); if trimmed.indexOf("include \"") == 0 { out.push("[HC002] include is forbidden (use using+alias): " + path + ":" + me._itoa(i+1)) } i=i+1 }
  }

  // HC003: using must be quoted
  _rule_using_quoted(text, path, out) {
    local lines = text.split("\n")
    local i=0; while i<lines.size() { local ln=lines.get(i); local t=me._ltrim(ln); if t.indexOf("using ") == 0 { if t.indexOf("using \"") != 0 { out.push("[HC003] using must be quoted: " + path + ":" + me._itoa(i+1)) } } i=i+1 }
  }

  // HC001: static box top-level assignment (before any method) is forbidden
  _rule_static_top_assign(text, path, out) {
    local n = text.length(); local line = 1
    local in_static = 0; local brace = 0; local in_method = 0
    local i=0; while i<n { local c = text.substring(i, i+1)
      // crude line counting
      if c == "\n" { line = line + 1 }
      // detect "static box"
      if in_static == 0 {
        if me._match_kw(text, i, "static box ") { in_static = 1; in_method = 0 }
      }
      if in_static == 1 {
        // method start
        if in_method == 0 && me._match_kw(text, i, "method ") { in_method = 1 }
        // brace tracking
        if c == "{" { brace = brace + 1 }
        if c == "}" {
          brace = brace - 1
          if brace <= 0 { in_static = 0; in_method = 0 }
        }
        // assignment at column start (rough heuristic): letter at i and next '=' later
        if in_method == 0 {
          // find line start segment
          local lstart = me._line_start(text, i)
          local head = text.substring(lstart, i+1)
          // only check at the first non-space of the line
          if me._is_line_head(text, i) == 1 {
            // identifier = ... is suspicious
            if me._is_ident_start(c) == 1 {
              // scan next few chars for '=' (up to EOL)
              local seen_eq = 0
              local off=0; while off<n { local j = i + 1 + off; if j>=n { break }; local cj=text.substring(j,j+1); if cj=="\n" { break }; if cj=="=" { seen_eq=1; break }; off=off+1 }
              if seen_eq == 1 {
                out.push("[HC001] top-level assignment in static box (use lazy init in method): " + path + ":" + me._itoa(line))
              }
            }
          }
        }
      i=i+1 }
  }

  // HC004: encourage JsonFragBox for JSON scans
  _rule_jsonfrag_usage(text, path, out) {
    // If the file manipulates mir_call/inst_json/seg and uses indexOf/substring heavily, warn.
    local suspicious = 0
    if text.indexOf("\"mir_call\"") >= 0 || text.indexOf("inst_json") >= 0 || text.indexOf(" seg") >= 0 {
      if text.indexOf(".indexOf(") >= 0 || text.indexOf(".substring(") >= 0 { suspicious = 1 }
    }
    if suspicious == 1 && text.indexOf("JsonFragBox.") < 0 {
      out.push("[HC004] JSON scan likely brittle; prefer JsonFragBox helpers: " + path)
    }
  }

  // helpers
  _ltrim(s) { return me._ltrim_chars(s, " \t") }
  _ltrim_chars(s, cs) {
    local n = s.length()
    local head = 0
    local i=0; while i<n { local ch=s.substring(i,i+1); if ch!=" " && ch!="\t" { head=i; break }; if i==n-1 { head=n }; i=i+1 }
    return s.substring(head)
  }
  _itoa(n) { local v=0+n; if v==0 { return "0" } local out=""; local digits="0123456789"; local tmp=""; while v>0 { local d=v%10; tmp=digits.substring(d,d+1)+tmp; v=v/10 } out=tmp; return out }
  _match_kw(s, i, kw) {
    local k = kw.length()
    if i + k > s.length() { return 0 }
    if s.substring(i, i+k) == kw { return 1 }
    return 0
  }
  _is_ident_start(c) {
    // ASCII alpha or _
    if c >= "A" && c <= "Z" { return 1 }
    if c >= "a" && c <= "z" { return 1 }
    if c == "_" { return 1 }
    return 0
  }
  _is_line_head(s, i) {
    // true if all chars before i on same line are spaces/tabs
    local r=0; while r<=i { if i==0 { return 1 }; local j=i - 1 - r; local cj=s.substring(j,j+1); if cj=="\n" { return 1 }; if cj!=" " && cj!="\t" { return 0 }; if j==0 { return 1 }; r=r+1 }
    return 1
  }
  _line_start(s, i) {
    local r=0; while r<=i { local j=i-r; if j==0 { return 0 }; local cj=s.substring(j-1,j); if cj=="\n" { return j }; r=r+1 }
    return 0
  }
}

static box HakoSourceCheckerMain { method main(args) {
  if args == null || args.size() < 1 {
    print("[lint/error] require at least one path argument")
    return 2
  }
  local fail = 0
  local i=0; while i<args.size() { local p=args.get(i); local rc=HakoSourceCheckerBox.check_file(p); if rc!=0 { fail=fail+1 }; i=i+1 }
  return fail
} }
