Integrations/GitHub.psm1

# GitHub Integration Module for MiMo CLI - Enhanced Version
# Provides integration with GitHub API

function Get-GitHubRepository {
    [CmdletBinding()]
    param(
        [Parameter(Mandatory=$true)]
        [string]$Owner,
        
        [Parameter(Mandatory=$true)]
        [string]$Repo
    )
    
    $url = "https://api.github.com/repos/$Owner/$Repo"
    try {
        $response = Invoke-RestMethod -Uri $url -Method Get -Headers @{
            "Accept" = "application/vnd.github.v3+json"
        }
        return $response
    }
    catch {
        Write-Error "Failed to fetch repository: $_"
        return $null
    }
}

function Get-GitHubIssues {
    [CmdletBinding()]
    param(
        [Parameter(Mandatory=$true)]
        [string]$Owner,
        
        [Parameter(Mandatory=$true)]
        [string]$Repo,
        
        [string]$State = "open",
        [int]$PerPage = 30
    )
    
    $url = "https://api.github.com/repos/$Owner/$Repo/issues?state=$State&per_page=$PerPage"
    try {
        $response = Invoke-RestMethod -Uri $url -Method Get -Headers @{
            "Accept" = "application/vnd.github.v3+json"
        }
        return $response
    }
    catch {
        Write-Error "Failed to fetch issues: $_"
        return $null
    }
}

function New-GitHubIssue {
    [CmdletBinding()]
    param(
        [Parameter(Mandatory=$true)]
        [string]$Owner,
        
        [Parameter(Mandatory=$true)]
        [string]$Repo,
        
        [Parameter(Mandatory=$true)]
        [string]$Title,
        
        [string]$Body = "",
        [string[]]$Labels = @(),
        [string[]]$Assignees = @()
    )
    
    $url = "https://api.github.com/repos/$Owner/$Repo/issues"
    $body = @{
        title = $Title
        body = $Body
        labels = $Labels
        assignees = $Assignees
    } | ConvertTo-Json
    
    try {
        $response = Invoke-RestMethod -Uri $url -Method Post -Headers @{
            "Accept" = "application/vnd.github.v3+json"
            "Content-Type" = "application/json"
        } -Body $body
        return $response
    }
    catch {
        Write-Error "Failed to create issue: $_"
        return $null
    }
}

function Get-GitHubBranches {
    [CmdletBinding()]
    param(
        [Parameter(Mandatory=$true)]
        [string]$Owner,
        
        [Parameter(Mandatory=$true)]
        [string]$Repo
    )
    
    $url = "https://api.github.com/repos/$Owner/$Repo/branches"
    try {
        $response = Invoke-RestMethod -Uri $url -Method Get -Headers @{
            "Accept" = "application/vnd.github.v3+json"
        }
        return $response
    }
    catch {
        Write-Error "Failed to fetch branches: $_"
        return $null
    }
}

function Get-GitHubContributors {
    [CmdletBinding()]
    param(
        [Parameter(Mandatory=$true)]
        [string]$Owner,
        
        [Parameter(Mandatory=$true)]
        [string]$Repo
    )
    
    $url = "https://api.github.com/repos/$Owner/$Repo/contributors"
    try {
        $response = Invoke-RestMethod -Uri $url -Method Get -Headers @{
            "Accept" = "application/vnd.github.v3+json"
        }
        return $response
    }
    catch {
        Write-Error "Failed to fetch contributors: $_"
        return $null
    }
}

function Get-GitHubCommits {
    [CmdletBinding()]
    param(
        [Parameter(Mandatory=$true)]
        [string]$Owner,
        
        [Parameter(Mandatory=$true)]
        [string]$Repo,
        
        [int]$PerPage = 30
    )
    
    $url = "https://api.github.com/repos/$Owner/$Repo/commits?per_page=$PerPage"
    try {
        $response = Invoke-RestMethod -Uri $url -Method Get -Headers @{
            "Accept" = "application/vnd.github.v3+json"
        }
        return $response
    }
    catch {
        Write-Error "Failed to fetch commits: $_"
        return $null
    }
}

function Get-GitHubContents {
    [CmdletBinding()]
    param(
        [Parameter(Mandatory=$true)]
        [string]$Owner,
        
        [Parameter(Mandatory=$true)]
        [string]$Repo,
        
        [string]$Path = "",
        [string]$Ref = "main"
    )
    
    $url = "https://api.github.com/repos/$Owner/$Repo/contents/$Path"
    if ($Ref) {
        $url += "?ref=$Ref"
    }
    
    try {
        $response = Invoke-RestMethod -Uri $url -Method Get -Headers @{
            "Accept" = "application/vnd.github.v3+json"
        }
        return $response
    }
    catch {
        Write-Error "Failed to fetch contents: $_"
        return $null
    }
}

function New-GitHubPullRequest {
    [CmdletBinding()]
    param(
        [Parameter(Mandatory=$true)]
        [string]$Owner,
        
        [Parameter(Mandatory=$true)]
        [string]$Repo,
        
        [Parameter(Mandatory=$true)]
        [string]$Title,
        
        [Parameter(Mandatory=$true)]
        [string]$Head,
        
        [Parameter(Mandatory=$true)]
        [string]$Base,
        
        [string]$Body = ""
    )
    
    $url = "https://api.github.com/repos/$Owner/$Repo/pulls"
    $body = @{
        title = $Title
        head = $Head
        base = $Base
        body = $Body
    } | ConvertTo-Json
    
    try {
        $response = Invoke-RestMethod -Uri $url -Method Post -Headers @{
            "Accept" = "application/vnd.github.v3+json"
            "Content-Type" = "application/json"
        } -Body $body
        return $response
    }
    catch {
        Write-Error "Failed to create pull request: $_"
        return $null
    }
}

function Get-GitHubPullRequests {
    [CmdletBinding()]
    param(
        [Parameter(Mandatory=$true)]
        [string]$Owner,
        
        [Parameter(Mandatory=$true)]
        [string]$Repo,
        
        [string]$State = "open"
    )
    
    $url = "https://api.github.com/repos/$Owner/$Repo/pulls?state=$State"
    try {
        $response = Invoke-RestMethod -Uri $url -Method Get -Headers @{
            "Accept" = "application/vnd.github.v3+json"
        }
        return $response
    }
    catch {
        Write-Error "Failed to fetch pull requests: $_"
        return $null
    }
}

# Export functions
Export-ModuleMember -Function Get-GitHubRepository, Get-GitHubIssues, New-GitHubIssue, Get-GitHubBranches, Get-GitHubContributors, Get-GitHubCommits, Get-GitHubContents, New-GitHubPullRequest, Get-GitHubPullRequests