///|
/// Recognize the versioned profile documents published by the Data Package
/// standard. URLs belonging to custom profiles or JSON Schema drafts retain
/// their existing handling. Query strings and fragments do not change version.
fn standard_profile_version(value : Json) -> String? {
  guard prop(value, "$schema") is String(url) else { return None }
  let path = url.split_once("#").map(pair => pair.0).unwrap_or(url[:])
  let path = path.split_once("?").map(pair => pair.0).unwrap_or(path)
  guard path.split("/").collect()
    is [scheme, "", host, "profiles", version, kind] &&
    ["https:", "http:"].contains(scheme.to_owned().to_lower()) &&
    host.to_owned().to_lower() == "datapackage.org" &&
    [
      "datapackage.json", "dataresource.json", "tableschema.json", "tabledialect.json",
    ].contains(kind.to_owned()) &&
    version
    .split(".")
    .all(part => !part.is_empty() && part.iter().all(ascii_digit)) else {
    return None
  }
  Some(version.to_owned())
}

///|
fn unsupported_standard(
  value : Json,
  owner : String,
  field : String,
) -> Diagnostic? {
  guard standard_profile_version(value) is Some(version) && version != "1.0" else {
    return None
  }
  Some(
    issue(
      "unsupported",
      owner,
      0,
      field,
      "Declared Frictionless standard version " +
      version +
      " is unsupported; this validator implements v1.0 rules",
    ),
  )
}

///|
/// Inspect only descriptor objects. A data row or extension value may contain a
/// property named $schema and is not itself a Frictionless descriptor.
fn unsupported_package_standard(value : Json) -> Diagnostic? {
  if unsupported_standard(value, "", "$schema") is Some(error) {
    return Some(error)
  }
  if prop(value, "resources") is Array(resources) {
    for resource in resources {
      let owner = json_text(prop(resource, "name")).unwrap_or("")
      if unsupported_standard(resource, owner, "$schema") is Some(error) {
        return Some(error)
      }
      for field in ["schema", "dialect"] {
        if unsupported_standard(
            prop(resource, field),
            owner,
            field + ".$schema",
          )
          is Some(error) {
          return Some(error)
        }
      }
    }
  }
  None
}

///|
fn custom_profile(value : Json, resource : Bool) -> String? {
  guard prop(value, "profile") is String(identifier) else { return None }
  let built_in = if resource {
    identifier == "data-resource" || identifier == "tabular-data-resource"
  } else {
    identifier == "data-package" || identifier == "tabular-data-package"
  }
  if built_in {
    None
  } else {
    Some(identifier)
  }
}

///|
/// Return the custom profiles declared by this Data Package and its resources.
/// The strings are profile identifiers or fully qualified schema URLs.
pub fn DataPackage::custom_profiles(self : DataPackage) -> Array[String] {
  let profiles : Map[String, Unit] = Map([])
  if custom_profile(self.original, false) is Some(id) {
    profiles[id] = ()
  }
  for resource in self.resources {
    if custom_profile(resource.metadata, true) is Some(id) {
      profiles[id] = ()
    }
  }
  profiles.keys().collect()
}

///|
fn profile_schema_at(document : Json, url : String) -> Json {
  guard document is Object(fields) && valid_uri(url) else { return document }
  let draft = match fields.get("$schema") {
    Some(String(uri)) => @moonschema.Draft::of(uri[:]).unwrap_or(Draft4)
    _ => Draft4
  }
  let keyword = if draft == Draft4 { "id" } else { "$id" }
  guard fields.get(keyword) is None else { return document }
  let schema = Map([])
  for key, value in fields {
    schema[key] = value
  }
  schema[keyword] = Json::string(url)
  Json::object(schema)
}

///|
/// Validate the original package and resource descriptors against supplied
/// custom JSON Schema documents. Keys are registry identifiers or schema URLs.
/// External references resolve only from the supplied documents. Unavailable
/// profile schemas and invalid descriptors produce scoped diagnostics.
pub fn DataPackage::validate_profiles(
  self : DataPackage,
  schemas : Map[String, Json],
) -> Array[Diagnostic] {
  let errors = []
  let compiled : Map[String, @moonschema.Schema] = Map([])
  for
    (resource, descriptor) in [("", self.original)] +
    self.resources.map(item => (item.name, item.metadata)) {
    guard custom_profile(descriptor, resource != "") is Some(id) else {
      continue
    }
    guard id != "" && (valid_uri(id) || valid_resource_name(id)) else {
      errors.push(
        issue(
          "descriptor", resource, 0, "profile", "Invalid profile identifier",
        ),
      )
      continue
    }
    guard schemas.get(id) is Some(Object(_) | True | False as document) else {
      errors.push(
        issue(
          "unsupported",
          resource,
          0,
          "profile",
          "No JSON Schema supplied for profile " + id,
        ),
      )
      continue
    }
    let schema = match compiled.get(id) {
      Some(schema) => schema
      None => {
        // v1 registry schemas use draft-04. Explicit $schema declarations on
        // custom documents override this default.
        let schema = @moonschema.Schema::new(
          profile_schema_at(document, id),
          draft=Draft4,
          remotes=schemas,
        ) catch {
          error => {
            errors.push(
              issue(
                "descriptor",
                resource,
                0,
                "profile",
                "Cannot load profile " + id + ": " + error.to_string(),
              ),
            )
            continue
          }
        }
        compiled[id] = schema
        schema
      }
    }
    for fault in schema.faults(descriptor) {
      errors.push(
        issue(
          "descriptor",
          resource,
          0,
          "profile" + fault.at,
          id + ": " + fault.message,
        ),
      )
    }
  }
  errors
}