LeadForge.psm1

# LeadForge — Email-to-Opportunity Pipeline Module
# Dot-sources all pipeline functions and exports public API

$ErrorActionPreference = 'Stop'

# Dot-source all function files
$functionPaths = @(
    "$PSScriptRoot\tool\Analysis\functions\Repair-JsonResponse.ps1"
    "$PSScriptRoot\tool\Automation\functions\Get-OwnerDomains.ps1"
    "$PSScriptRoot\tool\Automation\functions\Invoke-GatherEmails.ps1"
    "$PSScriptRoot\tool\Analysis\functions\Invoke-AnalyseEmails.ps1"
    "$PSScriptRoot\tool\Enrichment\functions\Invoke-EnrichContacts.ps1"
    "$PSScriptRoot\tool\Analysis\functions\Invoke-ResearchContacts.ps1"
    "$PSScriptRoot\tool\Analysis\functions\Invoke-TriageContacts.ps1"
    "$PSScriptRoot\tool\Analysis\functions\Invoke-DeepResearch.ps1"
    "$PSScriptRoot\tool\Analysis\functions\Invoke-ScoreOpportunities.ps1"
    "$PSScriptRoot\tool\Automation\functions\Export-HtmlReport.ps1"
    "$PSScriptRoot\tool\Automation\functions\Export-OpportunityResults.ps1"
)

foreach ($path in $functionPaths) {
    if (Test-Path $path) {
        . $path
    }
}

# Dot-source public functions
$Public = @(Get-ChildItem -Path "$PSScriptRoot\Public\*.ps1" -ErrorAction SilentlyContinue)
foreach ($file in $Public) {
    . $file.FullName
}

Export-ModuleMember -Function @(
    # Pipeline entry point
    'Invoke-LeadForge'
    # Setup wizard
    'Initialize-LeadForge'
    # Individual stages (advanced use)
    'Invoke-GatherEmails'
    'Invoke-AnalyseEmails'
    'Invoke-EnrichContacts'
    'Invoke-ResearchContacts'
    'Invoke-TriageContacts'
    'Invoke-DeepResearch'
    'Invoke-ScoreOpportunities'
    'Export-OpportunityResults'
) -Alias @('lf')

# --- Aliases ---
New-Alias -Name 'lf' -Value 'Invoke-LeadForge' -Force -Scope Global

# --- Tab Completion ---
# Profile parameter: complete from profiles/ directory
Register-ArgumentCompleter -CommandName 'Invoke-LeadForge' -ParameterName 'Profile' -ScriptBlock {
    param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameters)
    $profileDir = Join-Path $PSScriptRoot 'profiles'
    if (Test-Path $profileDir) {
        Get-ChildItem $profileDir -Filter '*.json' |
            Where-Object { $_.BaseName -like "$wordToComplete*" } |
            ForEach-Object {
                [System.Management.Automation.CompletionResult]::new(
                    $_.BaseName, $_.BaseName, 'ParameterValue', "Profile: $($_.BaseName)"
                )
            }
    }
}

# Steps parameter: complete valid stage names
Register-ArgumentCompleter -CommandName 'Invoke-LeadForge' -ParameterName 'Steps' -ScriptBlock {
    param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameters)
    @('gather', 'analyse', 'enrich', 'research-pass1', 'triage', 'research-pass2', 'score', 'export') |
        Where-Object { $_ -like "$wordToComplete*" } |
        ForEach-Object {
            [System.Management.Automation.CompletionResult]::new($_, $_, 'ParameterValue', "Stage: $_")
        }
}