#!/usr/bin/env bash
set -euo pipefail

PLUGIN_ROOT="$(cd "$(dirname "$0")/.." && pwd)"
DEPLOY_DIR="${1:?usage: build-deploy-folder.sh <deploy-dir> [claude-marketplace-dir] [site-source-dir]}"
CLAUDE_MARKETPLACE_DIR="${2:-}"
SITE_SOURCE_DIR="${3:-}"
PACKAGE_DIR="$PLUGIN_ROOT/output/packages"
VERSION="$(node -p "require('$PLUGIN_ROOT/package.json').version")"
BUILT_DATE="$(date +%Y-%m-%d)"

mkdir -p "$DEPLOY_DIR" "$PACKAGE_DIR"

"$PLUGIN_ROOT/scripts/build-packages.sh" "$PACKAGE_DIR" "$DEPLOY_DIR"
python3 "$PLUGIN_ROOT/scripts/package-standard-contract-test.py" "$PACKAGE_DIR"
python3 "$PLUGIN_ROOT/scripts/package-standard-contract-test.py" "$DEPLOY_DIR"

install -m 0644 "$PLUGIN_ROOT/README.md" "$DEPLOY_DIR/CARBON-README.md"
install -m 0644 "$PLUGIN_ROOT/COMMANDS.md" "$DEPLOY_DIR/COMMANDS.md"
install -m 0644 "$PLUGIN_ROOT/MCP_SERVER.md" "$DEPLOY_DIR/MCP_SERVER.md"
install -m 0644 "$PLUGIN_ROOT/NOTICE.md" "$DEPLOY_DIR/NOTICE.md"
install -m 0644 "$PLUGIN_ROOT/TESTING_GUIDE.md" "$DEPLOY_DIR/TESTING_GUIDE.md"

if [[ -n "$SITE_SOURCE_DIR" ]]; then
  site_assets=(
    index.html
    carbon-demo.html
    brand.html
    docs.html
    install-carbon.html
    install.html
    carbon-beta.html
    carbon-site.css
    carbon-site.js
    favicon.svg
    carbon-mark.svg
    carbon-horizontal-logo.png
    carbon-icon.png
    carbon-social-card.png
    carbon-social-card.svg
    carbon-architecture-overview.png
    carbon-architecture-overview-harness.svg
    carbon-coverage-overview.png
    carbon-beta-signup-apps-script.gs
    carbon-plugin-feedback-apps-script.gs
    carbon-sales-leads-apps-script.gs
    CARBON-BETA-SIGNUP-SETUP.md
  )
  for asset in "${site_assets[@]}"; do
    if [[ -f "$SITE_SOURCE_DIR/$asset" ]]; then
      install -m 0644 "$SITE_SOURCE_DIR/$asset" "$DEPLOY_DIR/$asset"
    fi
  done
fi

python3 "$PLUGIN_ROOT/scripts/harness-positioning-contract-test.py" "$DEPLOY_DIR"

if [[ -n "$CLAUDE_MARKETPLACE_DIR" ]]; then
  mkdir -p "$CLAUDE_MARKETPLACE_DIR"
  unzip -oq "$PACKAGE_DIR/carbon-claude-marketplace.zip" -d "$CLAUDE_MARKETPLACE_DIR"
fi

shopt -s nullglob
brand_targets=("$DEPLOY_DIR"/*.html "$DEPLOY_DIR"/*.md)
if ((${#brand_targets[@]})); then
  VERSION="$VERSION" BUILT_DATE="$BUILT_DATE" perl -0pi -e '
  s/CARBON \d+\.\d+\.\d+ beta/CARBON $ENV{VERSION} beta/g;
  s/Beta installer \d+\.\d+\.\d+/Beta installer $ENV{VERSION}/g;
  s/Beta \d+\.\d+\.\d+/Beta $ENV{VERSION}/g;
  s/CARBON \d+\.\d+\.\d+ runtime/CARBON $ENV{VERSION} runtime/g;
  s/Version \d+\.\d+\.\d+ packages/Version $ENV{VERSION} packages/g;
  s/\b(?:39|56|57|58|59|60) tools\b/40 local tools/g;
  s/\b(?:43|44|45|46|47) workflows\b/44 workflows/g;
  s/\b(?:43|44|45|46|47) prompts\b/44 prompts/g;
  s/\b(?:39|56|57|58|59|60)-tool\b/40-local-tool/g;
  s/\b(?:43|44|45|46|47)-workflow\b/44-workflow/g;
  s/\b(?:43|44|45|46|47)-prompt\b/44-prompt/g;
  s{<strong>(?:39|56|57|58|59|60)</strong><span>MCP capabilities</span>}{<strong>40</strong><span>local MCP capabilities</span>}g;
  s{<strong>(?:43|44|45|46|47)</strong><span>coding-agent workflows</span>}{<strong>44</strong><span>coding-agent workflows</span>}g;
  s{/carbon:carbon-}{/carbon-}g;
' "${brand_targets[@]}"
fi

{
  echo "CARBON $VERSION beta"
  echo "Built by testers.ai"
  echo "Built $BUILT_DATE"
} > "$DEPLOY_DIR/VERSION.txt"

artifacts=(
  carbon-openai.zip
  carbon-agent-plugin.zip
  carbon-cursor.zip
  carbon-claude.zip
  carbon-claude-marketplace.zip
  carbon-codex.zip
  carbon.plugin
  carbon-antigravity.plugin
  carbon-mcp.zip
)

(
  cd "$DEPLOY_DIR"
  shasum -a 256 "${artifacts[@]}" > carbon-checksums.txt
  for artifact in "${artifacts[@]}"; do
    shasum -a 256 "$artifact" > "$artifact.sha256"
  done
)

echo "Updated CARBON $VERSION deployment in $DEPLOY_DIR"
if [[ -n "$CLAUDE_MARKETPLACE_DIR" ]]; then
  echo "Updated Claude marketplace in $CLAUDE_MARKETPLACE_DIR"
fi
