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

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

function New-SchemaMigration {
    [CmdletBinding()]
    param(
        [Parameter(Mandatory)][string]$SqlInstance,
        [Parameter(Mandatory)][string]$Database,
        [Parameter(Mandatory)][string]$Version,
        [Parameter(Mandatory)][string]$ScriptName,
        [string]$AppliedBy = $env:USERNAME
    )
    
    $query = @"
INSERT INTO schemactl.MigrationHistory 
(SqlInstance, DatabaseName, Version, ScriptName, AppliedAt, AppliedBy, Status)
VALUES 
('$SqlInstance', '$Database', '$Version', '$ScriptName', GETDATE(), '$AppliedBy', 'Success')
"@
    
    try {
        Invoke-DbaQuery -SqlInstance $Script:Config.CentralServer -Database $Script:Config.Database -Query $query
        Write-Host "Migration recorded: $Version - $ScriptName" -ForegroundColor Green
    } catch {
        Write-Warning "Failed to record migration: $_"
    }
}

Export-ModuleMember -Function Get-MigrationHistory, New-SchemaMigration
