# DBAOps.Pipeline.psm1 - Minimal Working Version
$Script:Config = @{
    CentralServer = "AZDBAOPSSQL01"
    Database = "DBAOpsRepository"
}

function Get-DeploymentHistory {
    [CmdletBinding()]
    param(
        [int]$Top = 20
    )
    
    $query = "SELECT TOP $Top * FROM pipeline.DeploymentHistory ORDER BY StartTime DESC"
    
    try {
        return Invoke-DbaQuery -SqlInstance $Script:Config.CentralServer -Database $Script:Config.Database -Query $query
    } catch {
        Write-Warning "Failed to get deployment history: $_"
        return $null
    }
}

function New-Deployment {
    [CmdletBinding()]
    param(
        [Parameter(Mandatory)][string]$SqlInstance,
        [Parameter(Mandatory)][string]$Database,
        [Parameter(Mandatory)][string]$ScriptPath,
        [string]$Environment = "DEV",
        [string]$TicketNumber,
        [string]$DeployedBy = $env:USERNAME
    )
    
    $deploymentId = [guid]::NewGuid().ToString()
    
    $query = @"
INSERT INTO pipeline.DeploymentHistory 
(DeploymentId, ScriptPath, SqlInstance, DatabaseName, Environment, Status, StartTime, DeployedBy, TicketNumber)
VALUES 
('$deploymentId', '$ScriptPath', '$SqlInstance', '$Database', '$Environment', 'Started', GETDATE(), '$DeployedBy', '$TicketNumber')
"@
    
    try {
        Invoke-DbaQuery -SqlInstance $Script:Config.CentralServer -Database $Script:Config.Database -Query $query
        Write-Host "Deployment started: $deploymentId" -ForegroundColor Green
        return $deploymentId
    } catch {
        Write-Warning "Failed to create deployment: $_"
        return $null
    }
}

Export-ModuleMember -Function Get-DeploymentHistory, New-Deployment
