#Requires -Version 7.0

<#
Run the Accountable Record release validation sequence.

WHY: This script runs the contract checks, generated-artifact refresh,
generated-artifact validation, Python tests, type checks, and pre-commit checks
used before tagging a release.
#>

Set-StrictMode -Version Latest
$ErrorActionPreference = "Stop"

Write-Host "Uv Syncing dependencies..."
uv sync --extra dev --extra docs --upgrade

Write-Host "Running pre-commit checks on all files..."
git add -A
uvx pre-commit run --all-files

Write-Host "Writing lock files..."
uv run accountable-record write-lock

Write-Host "Verifying lock files..."
uv run accountable-record verify-lock

Write-Host "Running all stages..."
uv run accountable-record run all

Write-Host "Validating the AR repo..."
uv run accountable-record --target ../accountable-record write-lock
uv run accountable-record --target ../accountable-record run all

Write-Host "Running tests..."
uv run python -m pytest

Write-Host "Running type checks..."
uv run python -m pyright

Write-Host "Running pre-commit checks..."
uvx pre-commit run --all-files

Write-Host "Building docs..."
uv run python -m zensical build

Write-Host "Running contract checks (should be all kept, 0 broken)"
uvx --with-editable . import-linter lint

Write-Host "Finding dead code..."
uvx --with-editable . vulture src/accountable_record

Write-Host "Complexity (check for any C ratings, should return nothing)"
uvx radon cc src/accountable_record -s -a -n C

Write-Host "All metrics"
# raw (totals)
uvx radon raw src/accountable_record -j | uv run python -c "import json, sys; data=json.load(sys.stdin); keys=('loc','lloc','sloc','comments','multi','blank','single_comments'); totals={k:sum(file[k] for file in data.values()) for k in keys}; print('\n'.join(f'{k.upper()}: {v}' for k,v in totals.items()))"

Write-Host "Building distribution artifacts and checking with twine..."
uv build
uvx twine check dist/*

Write-Host "Release validation completed successfully."
