///|
/// Checks the structural invariants an OOXML (xlsx) package must satisfy
/// for Microsoft Excel to open it without a repair prompt, returning the
/// list of problems found (empty when the package is well-formed). These
/// are the package-level rules the OOXML schema validator does not fully
/// cover and that are the common causes of Excel's "we found a problem"
/// dialog:
///
///  - the archive is a readable zip
///  - `[Content_Types].xml` and the root `_rels/.rels` exist
///  - every part is covered by a Default (by extension) or an Override
///    content type
///  - every relationship target (except external ones) resolves to a
///    part that exists in the package
///  - no duplicate part names, and part names are well-formed
///  - the core workbook parts are present
///
/// This is a fast, dependency-free complement to the Microsoft OpenXML
/// SDK validator: it runs entirely in MoonBit, so it can be asserted on
/// every workbook a test generates.
pub fn validate_ooxml_package(bytes : BytesView) -> Array[String] {
  let problems : Array[String] = []
  let archive = @zip.read(bytes) catch {
    err => {
      problems.push("archive is not a readable zip: \{repr(err)}")
      return problems
    }
  }
  let parts : Map[String, Int] = {}
  for entry in archive.entries() {
    let name = entry.name()
    parts[name] = parts.get(name).unwrap_or(0) + 1
    if name.contains("\\") {
      problems.push("part name uses a backslash: \{name}")
    }
    if name.has_prefix("/") {
      problems.push("part name has a leading slash: \{name}")
    }
  }
  for name, count in parts {
    if count > 1 {
      problems.push("duplicate part: \{name} (\{count} entries)")
    }
  }

  // required core parts
  for
    required in [
      "[Content_Types].xml", "_rels/.rels", "xl/workbook.xml", "xl/_rels/workbook.xml.rels",
    ] {
    if !parts.contains(required) {
      problems.push("missing required part: \{required}")
    }
  }

  // content-type coverage
  match archive.get("[Content_Types].xml") {
    Some(data) => {
      let ct = decode_package_part(data)
      let (defaults, overrides) = parse_content_types(ct)
      for entry in archive.entries() {
        let name = entry.name()
        if name == "[Content_Types].xml" {
          continue
        }
        let has_override = overrides.contains("/" + name)
        let has_default = match part_extension(name) {
          Some(ext) => defaults.contains(ext.to_lower())
          None => false
        }
        if !has_override && !has_default {
          problems.push("part has no declared content type: \{name}")
        }
      }
    }
    None => ()
  }

  // relationship target integrity
  for entry in archive.entries() {
    let name = entry.name()
    if !name.has_suffix(".rels") {
      continue
    }
    let base = rels_base_dir(name)
    let rels_xml = decode_package_part(entry.data())
    for rel in parse_rel_targets(rels_xml) {
      let (target, external) = rel
      if external {
        continue
      }
      let resolved = resolve_part_path(base, target)
      if resolved != "" && !parts.contains(resolved) {
        problems.push(
          "relationship in \{name} points to a missing part: \{target} -> \{resolved}",
        )
      }
    }
  }
  problems
}

///|
/// Decodes a package part as UTF-8, tolerating a leading BOM. Used only
/// for the ASCII-structured package plumbing (content types, rels).
fn decode_package_part(data : Bytes) -> String {
  @encoding/utf8.decode(data) catch {
    _ => ""
  }
}

///|
/// Returns the lowercase-ready file extension of a part name, or `None`.
fn part_extension(name : String) -> String? {
  match name.rev_find(".") {
    Some(dot) =>
      match name.rev_find("/") {
        Some(slash) =>
          if slash > dot {
            None
          } else {
            Some(name[dot + 1:].to_owned())
          }
        None => Some(name[dot + 1:].to_owned())
      }
    None => None
  }
}

///|
/// Parses `[Content_Types].xml` into the set of Default extensions
/// (lowercased) and the set of Override part names.
fn parse_content_types(xml : String) -> (Map[String, Bool], Map[String, Bool]) {
  let defaults : Map[String, Bool] = {}
  let overrides : Map[String, Bool] = {}
  let mut first = true
  for chunk in xml.split("") {
      Some(pos) => pos
      None => continue
    }
    let ext = attr_value(text[:end], "Extension") catch { _ => None }
    match ext {
      Some(ext) => defaults[ext.to_lower()] = true
      None => ()
    }
  }
  first = true
  for chunk in xml.split("") {
      Some(pos) => pos
      None => continue
    }
    let part = attr_value(text[:end], "PartName") catch { _ => None }
    match part {
      Some(part) => overrides[part] = true
      None => ()
    }
  }
  (defaults, overrides)
}

///|
/// Parses the relationship targets of a `.rels` part, returning each
/// (target, is_external) pair.
fn parse_rel_targets(xml : String) -> Array[(String, Bool)] {
  let result : Array[(String, Bool)] = []
  let mut first = true
  for chunk in xml.split("") {
      Some(pos) => pos
      None => continue
    }
    let tag = text[:end]
    let target_opt = attr_value(tag, "Target") catch { _ => None }
    let target = match target_opt {
      Some(value) => value
      None => continue
    }
    let mode_opt = attr_value(tag, "TargetMode") catch { _ => None }
    let external = match mode_opt {
      Some(mode) => mode == "External"
      None => false
    }
    result.push((target, external))
  }
  result
}

///|
/// The base directory a relationship target in `/_rels/.rels`
/// resolves against: the directory containing the described part. For
/// the root `_rels/.rels` this is the package root ("").
fn rels_base_dir(rels_part : String) -> String {
  match rels_part.rev_find("_rels/") {
    Some(pos) => rels_part[:pos].to_owned()
    None => ""
  }
}

///|
/// Resolves a relationship target against a base directory, applying a
/// leading-slash absolute form and `.`/`..` segments. Returns "" when
/// the path escapes the package root (which the caller treats as
/// unresolvable rather than a definite error).
fn resolve_part_path(base : String, target : String) -> String {
  let combined = if target.has_prefix("/") {
    target[1:].to_owned()
  } else {
    base + target
  }
  let segments : Array[String] = []
  for segment in combined.split("/") {
    let seg = segment.to_owned()
    if seg == "" || seg == "." {
      continue
    }
    if seg == ".." {
      if segments.length() == 0 {
        return ""
      }
      segments.remove(segments.length() - 1) |> ignore
      continue
    }
    segments.push(seg)
  }
  segments.join("/")
}