let s = import "../schema.ncl" in

# Mode: new_website
# Creates a new static website project using the evol-rustelo framework.
# Adds ontology, kogral, syntaxis, infra provisioning, DNS, and ecosystem announcement.
#
# Required params:
#   {project_name}     — site identifier
#   {project_dir}      — absolute path for the new site repository
#   {ontoref_dir}  — absolute path to the ontoref checkout
#   {domain}           — DNS domain to configure (e.g. "example.com")
#   {infra_env}        — target infra environment ("staging" | "production")
{
  id      = "new_website",
  trigger = "Create a new evol-rustelo static website in the ecosystem",

  preconditions = [
    "git is available in PATH",
    "nickel is available in PATH",
    "cargo is available in PATH (evol-rustelo scaffold uses cargo generate)",
    "{project_dir} parent directory exists and is writable",
    "kogral CLI available (init_kogral continues on failure)",
    "syntaxis CLI available (create_syntaxis_project continues on failure)",
    "nats CLI available (best-effort steps continue on failure)",
    "provisioning system reachable (apply_infra continues on failure)",
    "{ontoref_dir}/templates/ontology/ exists",
  ],

  steps = [
    {
      id         = "init_repo",
      action     = "initialize_git_repository",
      actor      = 'Both,
      cmd        = "git -C {project_dir} init && git -C {project_dir} commit --allow-empty -m 'chore: initial commit'",
      depends_on = [],
      on_error   = { strategy = 'Stop },
    },
    {
      id         = "copy_ontology_template",
      action     = "scaffold_ontology_directory",
      actor      = 'Agent,
      cmd        = "cp -r {ontoref_dir}/templates/ontology {project_dir}/.ontology",
      depends_on = [{ step = "init_repo", kind = 'OnSuccess }],
      on_error   = { strategy = 'Stop },
    },
    {
      id         = "init_kogral",
      action     = "initialize_kogral_graph",
      actor      = 'Agent,
      cmd        = "kogral init --name {project_name} --dir {project_dir}/.kogral",
      depends_on = [{ step = "init_repo", kind = 'OnSuccess }],
      on_error   = { strategy = 'Continue },
    },
    {
      id         = "create_syntaxis_project",
      action     = "register_project_in_syntaxis",
      actor      = 'Agent,
      cmd        = "syntaxis project create --name {project_name} --path {project_dir}",
      depends_on = [{ step = "init_repo", kind = 'OnSuccess }],
      on_error   = { strategy = 'Continue },
    },
    {
      id         = "scaffold_rustelo",
      action     = "generate_evol_rustelo_project",
      actor      = 'Agent,
      cmd        = "cargo generate --git https://repo.jesusperez.pro/jesus/evol-rustelo --name {project_name} --destination {project_dir}",
      depends_on = [{ step = "init_repo", kind = 'OnSuccess }],
      on_error   = { strategy = 'Stop },
      note       = "Generates the evol-rustelo site scaffold with Nickel content schema",
    },
    {
      id         = "apply_infra",
      action     = "request_infrastructure_provisioning",
      actor      = 'Agent,
      cmd        = "nats pub ecosystem.provisioning.scaffold '{\"project_name\": \"{project_name}\", \"stack\": \"evol-rustelo\", \"env\": \"{infra_env}\"}'",
      depends_on = [{ step = "scaffold_rustelo", kind = 'OnSuccess }],
      on_error   = { strategy = 'Continue },
      note       = "Provisioning service responds on ecosystem.provisioning.ready with Nickel infra NCL path",
    },
    {
      id         = "configure_dns",
      action     = "point_domain_to_infra",
      actor      = 'Human,
      depends_on = [{ step = "apply_infra", kind = 'OnSuccess }],
      on_error   = { strategy = 'Stop },
      note       = "Human task: configure {domain} DNS to point to provisioned infrastructure",
      verify     = "dig +short {domain} | grep -v '^$'",
    },
    {
      id     = "publish_ecosystem_event",
      action = "announce_website_created",
      actor  = 'Agent,
      cmd    = "nats pub ecosystem.project.created '{\"project_name\": \"{project_name}\", \"domain\": \"{domain}\", \"type\": \"website\"}'",
      depends_on = [
        { step = "configure_dns",           kind = 'OnSuccess },
        { step = "copy_ontology_template",  kind = 'OnSuccess },
        { step = "init_kogral",             kind = 'Always },
        { step = "create_syntaxis_project", kind = 'Always },
      ],
      on_error = { strategy = 'Continue },
    },
  ],

  postconditions = [
    "{project_dir} is an initialized git repository with evol-rustelo scaffold",
    "{project_dir}/.ontology/ populated from templates",
    "infra provisioned for {infra_env} environment",
    "{domain} DNS configured",
    "ecosystem.project.created published",
  ],
} | (s.Mode String)
