#!/usr/bin/env nu
# Fix layout_conventions.md violations
# Part 2: Rename product docs to lowercase-kebab-case
# Handles case-insensitive filesystems (macOS APFS)

def main [] {
    print "🔧 Renaming product docs to lowercase-kebab-case..."
    print ""

    # Product docs to rename
    let renames = [
        # docs/src/
        {old: "docs/src/PROVISIONING.md", new: "docs/src/provisioning.md"},

        # ADRs
        {old: "docs/src/architecture/adr/ADR-001-project-structure.md", new: "docs/src/architecture/adr/adr-001-project-structure.md"},
        {old: "docs/src/architecture/adr/ADR-002-distribution-strategy.md", new: "docs/src/architecture/adr/adr-002-distribution-strategy.md"},
        {old: "docs/src/architecture/adr/ADR-003-workspace-isolation.md", new: "docs/src/architecture/adr/adr-003-workspace-isolation.md"},
        {old: "docs/src/architecture/adr/ADR-004-hybrid-architecture.md", new: "docs/src/architecture/adr/adr-004-hybrid-architecture.md"},
        {old: "docs/src/architecture/adr/ADR-005-extension-framework.md", new: "docs/src/architecture/adr/adr-005-extension-framework.md"},
        {old: "docs/src/architecture/adr/ADR-006-provisioning-cli-refactoring.md", new: "docs/src/architecture/adr/adr-006-provisioning-cli-refactoring.md"},
        {old: "docs/src/architecture/adr/ADR-007-kms-simplification.md", new: "docs/src/architecture/adr/adr-007-kms-simplification.md"},
        {old: "docs/src/architecture/adr/ADR-008-cedar-authorization.md", new: "docs/src/architecture/adr/adr-008-cedar-authorization.md"},
        {old: "docs/src/architecture/adr/ADR-009-security-system-complete.md", new: "docs/src/architecture/adr/adr-009-security-system-complete.md"},
        {old: "docs/src/architecture/orchestrator_info.md", new: "docs/src/architecture/orchestrator-info.md"},

        # Control Center UI
        {old: "platform/crates/control-center-ui/AUTH_SYSTEM.md", new: "platform/crates/control-center-ui/auth-system.md"},
        {old: "platform/crates/control-center-ui/REFERENCE.md", new: "platform/crates/control-center-ui/reference.md"},
        {old: "platform/crates/control-center-ui/UPSTREAM_DEPENDENCY_ISSUE.md", new: "platform/crates/control-center-ui/upstream-dependency-issue.md"},

        # Extension Registry
        {old: "platform/crates/extension-registry/API.md", new: "platform/crates/extension-registry/api.md"},

        # Control Center
        {old: "platform/crates/control-center/docs/SECURITY_CONSIDERATIONS.md", new: "platform/crates/control-center/docs/security-considerations.md"},

        # Orchestrator
        {old: "platform/crates/orchestrator/docs/DNS_INTEGRATION.md", new: "platform/crates/orchestrator/docs/dns-integration.md"},
        {old: "platform/crates/orchestrator/docs/EXTENSION_LOADING.md", new: "platform/crates/orchestrator/docs/extension-loading.md"},
        {old: "platform/crates/orchestrator/docs/OCI_INTEGRATION.md", new: "platform/crates/orchestrator/docs/oci-integration.md"},
        {old: "platform/crates/orchestrator/docs/SERVICE_ORCHESTRATION.md", new: "platform/crates/orchestrator/docs/service-orchestration.md"},
        {old: "platform/crates/orchestrator/docs/SSH_KEY_MANAGEMENT.md", new: "platform/crates/orchestrator/docs/ssh-key-management.md"},
        {old: "platform/crates/orchestrator/docs/STORAGE_BACKENDS.md", new: "platform/crates/orchestrator/docs/storage-backends.md"},
        {old: "platform/crates/orchestrator/wrks/README_TESTING.md", new: "platform/crates/orchestrator/wrks/readme-testing.md"},

        # Test docs
        {old: "tests/integration/docs/ORBSTACK_SETUP.md", new: "tests/integration/docs/orbstack-setup.md"},
        {old: "tests/integration/docs/TESTING_GUIDE.md", new: "tests/integration/docs/testing-guide.md"},
        {old: "tests/integration/docs/TEST_COVERAGE.md", new: "tests/integration/docs/test-coverage.md"},

        # Extensions
        {old: "extensions/providers/REFERENCE.md", new: "extensions/providers/reference.md"},
        {old: "extensions/clusters/REFERENCE.md", new: "extensions/clusters/reference.md"},
        {old: "extensions/providers/aws/kcl/docs/aws_prov.md", new: "extensions/providers/aws/kcl/docs/aws-prov.md"},
    ]

    print "📝 Renaming product documentation files..."
    for rename in $renames {
        if ($rename.old | path exists) {
            # Use temp file for case-insensitive filesystems
            let temp = $"($rename.old).tmp"
            print $"  - ($rename.old) → ($rename.new)"
            mv $rename.old $temp
            mv $temp $rename.new
        } else {
            print $"  ⚠️  File not found: ($rename.old)"
        }
    }

    print "\n✅ Product docs renamed to lowercase-kebab-case"
    print "\n⚡ Next: Update SUMMARY.md and fix internal links"
}
