Integrations/GitLab.psm1

# GitLab Integration Module for MiMo CLI
# Provides integration with GitLab API

function Get-GitLabProject {
    [CmdletBinding()]
    param(
        [Parameter(Mandatory=$true)]
        [string]$ProjectId,
        
        [string]$BaseUrl = "https://gitlab.com"
    )
    
    $url = "$BaseUrl/api/v4/projects/$ProjectId"
    try {
        $response = Invoke-RestMethod -Uri $url -Method Get
        return $response
    }
    catch {
        Write-Error "Failed to fetch GitLab project: $_"
        return $null
    }
}

function Get-GitLabIssues {
    [CmdletBinding()]
    param(
        [Parameter(Mandatory=$true)]
        [string]$ProjectId,
        
        [string]$State = "opened",
        [string]$BaseUrl = "https://gitlab.com"
    )
    
    $url = "$BaseUrl/api/v4/projects/$ProjectId/issues?state=$State"
    try {
        $response = Invoke-RestMethod -Uri $url -Method Get
        return $response
    }
    catch {
        Write-Error "Failed to fetch GitLab issues: $_"
        return $null
    }
}

function New-GitLabIssue {
    [CmdletBinding()]
    param(
        [Parameter(Mandatory=$true)]
        [string]$ProjectId,
        
        [Parameter(Mandatory=$true)]
        [string]$Title,
        
        [string]$Description = "",
        [string]$BaseUrl = "https://gitlab.com"
    )
    
    $url = "$BaseUrl/api/v4/projects/$ProjectId/issues"
    $body = @{
        title = $Title
        description = $Description
    } | ConvertTo-Json
    
    try {
        $response = Invoke-RestMethod -Uri $url -Method Post -Headers @{
            "Content-Type" = "application/json"
        } -Body $body
        return $response
    }
    catch {
        Write-Error "Failed to create GitLab issue: $_"
        return $null
    }
}

function Get-GitLabMergeRequests {
    [CmdletBinding()]
    param(
        [Parameter(Mandatory=$true)]
        [string]$ProjectId,
        
        [string]$State = "opened",
        [string]$BaseUrl = "https://gitlab.com"
    )
    
    $url = "$BaseUrl/api/v4/projects/$ProjectId/merge_requests?state=$State"
    try {
        $response = Invoke-RestMethod -Uri $url -Method Get
        return $response
    }
    catch {
        Write-Error "Failed to fetch GitLab merge requests: $_"
        return $null
    }
}

function Get-GitLabBranches {
    [CmdletBinding()]
    param(
        [Parameter(Mandatory=$true)]
        [string]$ProjectId,
        
        [string]$BaseUrl = "https://gitlab.com"
    )
    
    $url = "$BaseUrl/api/v4/projects/$ProjectId/repository/branches"
    try {
        $response = Invoke-RestMethod -Uri $url -Method Get
        return $response
    }
    catch {
        Write-Error "Failed to fetch GitLab branches: $_"
        return $null
    }
}

# Export functions
Export-ModuleMember -Function Get-GitLabProject, Get-GitLabIssues, New-GitLabIssue, Get-GitLabMergeRequests, Get-GitLabBranches