# Gitea CLI Commands
#
# User-facing CLI commands for Gitea integration
#
# Version: 1.0.0

use api_client.nu *
use service.nu *
use workspace_git.nu *
use locking.nu *
use extension_publish.nu *

# Gitea service status
export def "gitea status" [] -> nothing {
    let status = get-gitea-status

    print "Gitea Status:"
    print $"  Mode: ($status.mode)"

    if $status.mode == "remote" {
        print $"  URL: ($status.url)"
        print $"  Accessible: ($status.accessible)"
    } else {
        print $"  Deployment: ($status.deployment)"
        print $"  Running: ($status.running)"
        print $"  Port: ($status.port)"
        print $"  URL: ($status.url)"

        if $status.deployment == "docker" {
            print $"  Container: ($status.container_name)"
        } else {
            print $"  Binary: ($status.binary_path)"
        }
    }

    # Check health
    let healthy = check-gitea-health
    print $"  Health: (if $healthy { "✓ OK" } else { "❌ Unavailable" })"
}

# Start Gitea service
export def "gitea start" [] -> nothing {
    start-gitea
}

# Stop Gitea service
export def "gitea stop" [
    --remove (-r)  # Remove container (Docker only)
] -> nothing {
    if $remove {
        stop-gitea-docker --remove
    } else {
        stop-gitea
    }
}

# Restart Gitea service
export def "gitea restart" [] -> nothing {
    restart-gitea
}

# Show Gitea logs
export def "gitea logs" [
    --lines (-n): int = 100
    --follow (-f)
] -> nothing {
    get-gitea-logs --lines $lines --follow=$follow
}

# Install Gitea binary
export def "gitea install" [
    version?: string = "latest"
    --install-dir: string = "/usr/local/bin"
] -> nothing {
    install-gitea $version --install-dir $install_dir
}

# Repository commands

# Create repository
export def "gitea repo create" [
    name: string
    --org (-o): string = ""
    --description (-d): string = ""
    --private (-p): bool = false
] -> nothing {
    let config = get-gitea-config

    let organization = if ($org | is-empty) {
        $config.repositories.organization
    } else {
        $org
    }

    let repo = create-repository $organization $name $description $private

    print $"✓ Repository created: ($repo.full_name)"
    print $"   URL: ($repo.html_url)"
}

# List repositories
export def "gitea repo list" [
    --org (-o): string = ""
] -> nothing {
    let repos = if ($org | is-empty) {
        list-user-repositories
    } else {
        list-repositories $org
    }

    $repos | select name full_name private description html_url | table
}

# Delete repository
export def "gitea repo delete" [
    name: string
    --org (-o): string = ""
    --force (-f)
] -> nothing {
    let config = get-gitea-config

    let organization = if ($org | is-empty) {
        $config.repositories.organization
    } else {
        $org
    }

    delete-repository $organization $name --force=$force
}

# Extension commands

# Publish extension
export def "gitea extension publish" [
    extension_path: string
    version: string
    --release-notes (-m): string = ""
    --draft (-d): bool = false
    --prerelease (-p): bool = false
] -> nothing {
    let metadata = publish-extension-to-gitea $extension_path $version \
        --release-notes $release_notes \
        --draft=$draft \
        --prerelease=$prerelease

    print ""
    print "Extension published:"
    print $"  Name: ($metadata.extension_name)"
    print $"  Type: ($metadata.extension_type)"
    print $"  Version: ($metadata.version)"
    print $"  Release URL: ($metadata.release_url)"
}

# List published extensions
export def "gitea extension list" [
    --type (-t): string = ""
] -> nothing {
    let extensions = if ($type | is-empty) {
        list-gitea-extensions
    } else {
        list-gitea-extensions --extension-type $type
    }

    $extensions | select extension_name extension_type version published_at url | table
}

# Download extension
export def "gitea extension download" [
    name: string
    version: string
    --destination (-d): string = "./extensions"
] -> nothing {
    download-gitea-extension $name $version $destination
}

# Show extension info
export def "gitea extension info" [
    name: string
    version: string
] -> nothing {
    let metadata = get-gitea-extension-metadata $name $version

    print "Extension Information:"
    print $"  Name: ($metadata.extension_name)"
    print $"  Type: ($metadata.extension_type)"
    print $"  Version: ($metadata.version)"
    print $"  Tag: ($metadata.tag_name)"
    print $"  Published: ($metadata.published_at)"
    print $"  Draft: ($metadata.draft)"
    print $"  Prerelease: ($metadata.prerelease)"
    print $"  URL: ($metadata.url)"
    print ""
    print "Release Notes:"
    print $metadata.body
}

# Lock commands

# Acquire workspace lock
export def "gitea lock acquire" [
    workspace: string
    type: string  # read, write, deploy
    --operation (-o): string = ""
    --expiry (-e): string = ""
] -> nothing {
    let lock = acquire-workspace-lock $workspace $type $operation $expiry

    print $"✓ Lock acquired for workspace: ($workspace)"
    print $"  Lock ID: ($lock.lock_id)"
    print $"  Type: ($lock.lock_type)"
    print $"  User: ($lock.user)"
}

# Release workspace lock
export def "gitea lock release" [
    workspace: string
    lock_id: int
] -> nothing {
    release-workspace-lock $workspace $lock_id
}

# List workspace locks
export def "gitea lock list" [
    workspace?: string
] -> nothing {
    let locks = if ($workspace | is-empty) {
        list-all-locks
    } else {
        list-workspace-locks $workspace
    }

    if ($locks | length) == 0 {
        print "No active locks"
    } else {
        $locks | select number workspace lock_type user created_at url | table
    }
}

# Show lock info
export def "gitea lock info" [
    workspace: string
    lock_id: int
] -> nothing {
    let lock = get-lock-info $workspace $lock_id

    print "Lock Information:"
    print $"  Lock ID: ($lock.lock_id)"
    print $"  Workspace: ($lock.workspace)"
    print $"  Type: ($lock.lock_type)"
    print $"  User: ($lock.user)"
    print $"  State: ($lock.state)"
    print $"  Created: ($lock.created_at)"
    print $"  Updated: ($lock.updated_at)"
    print $"  URL: ($lock.url)"
    print ""
    print "Details:"
    print $lock.body
}

# Force release lock
export def "gitea lock force-release" [
    workspace: string
    lock_id: int
    --reason (-r): string = "Forced unlock"
] -> nothing {
    force-release-lock $workspace $lock_id --reason $reason
}

# Cleanup expired locks
export def "gitea lock cleanup" [] -> nothing {
    let expired = cleanup-expired-locks

    if ($expired | length) == 0 {
        print "No expired locks found"
    } else {
        print $"Cleaned up ($expired | length) expired locks"
    }
}

# User/Auth commands

# Validate token
export def "gitea auth validate" [] -> nothing {
    let valid = validate-token

    if $valid {
        print "✓ Token is valid"

        let user = get-current-user
        print $"  User: ($user.login)"
        print $"  Email: ($user.email)"
    } else {
        print "❌ Token is invalid"
    }
}

# Show current user
export def "gitea user" [] -> nothing {
    let user = get-current-user

    print "Current User:"
    print $"  Username: ($user.login)"
    print $"  Email: ($user.email)"
    print $"  Full Name: ($user.full_name)"
    print $"  ID: ($user.id)"
}

# Organization commands

# Create organization
export def "gitea org create" [
    name: string
    --description (-d): string = ""
    --visibility (-v): string = "private"
] -> nothing {
    let org = create-organization $name $description $visibility

    print $"✓ Organization created: ($org.username)"
    print $"   URL: ($org.website)"
}

# List organizations
export def "gitea org list" [] -> nothing {
    let orgs = list-organizations

    $orgs | select username description | table
}

# Gitea help
export def "gitea help" [] -> nothing {
    print "Gitea Integration Commands"
    print ""
    print "Service:"
    print "  gitea status              - Show Gitea service status"
    print "  gitea start               - Start Gitea service"
    print "  gitea stop                - Stop Gitea service"
    print "  gitea restart             - Restart Gitea service"
    print "  gitea logs                - Show Gitea logs"
    print "  gitea install [version]   - Install Gitea binary"
    print ""
    print "Repositories:"
    print "  gitea repo create <name>  - Create repository"
    print "  gitea repo list           - List repositories"
    print "  gitea repo delete <name>  - Delete repository"
    print ""
    print "Extensions:"
    print "  gitea extension publish <path> <version>  - Publish extension"
    print "  gitea extension list                      - List published extensions"
    print "  gitea extension download <name> <version> - Download extension"
    print "  gitea extension info <name> <version>     - Show extension info"
    print ""
    print "Workspace Locking:"
    print "  gitea lock acquire <workspace> <type>  - Acquire lock"
    print "  gitea lock release <workspace> <id>    - Release lock"
    print "  gitea lock list [workspace]            - List active locks"
    print "  gitea lock info <workspace> <id>       - Show lock details"
    print "  gitea lock force-release <workspace>   - Force release lock"
    print "  gitea lock cleanup                     - Cleanup expired locks"
    print ""
    print "Authentication:"
    print "  gitea auth validate  - Validate auth token"
    print "  gitea user           - Show current user"
    print ""
    print "Organizations:"
    print "  gitea org create <name>  - Create organization"
    print "  gitea org list           - List organizations"
}
