modules/frontend/Write-Frontend.ps1

param(
    [string]$ProjectName,
    [string]$OrmMode,
    [string]$OutputPath,
    [string]$FrontendMode,
    [string[]]$Entities,
    [bool]$I18n = $false,
    [string]$Language = "tr",
    [string]$Platform = "Web"
)

$ModulesDir = Split-Path -Parent $MyInvocation.MyCommand.Path

# 1. Monorepo Shared Package & Mobile generation if selected
if ($Platform -match "Mobile|Both") {
    $sharedParams = @{
        ProjectName  = $ProjectName
        Platform     = $Platform
        OutputPath   = $OutputPath
        Entities     = $Entities
        Language     = $Language
    }
    
    # Write shared package
    & "$ModulesDir\Write-Shared-Package.ps1" @sharedParams

    # Write Mobile (React Native / Expo) components
    $mobileParams = @{
        ProjectName  = $ProjectName
        OutputPath   = $OutputPath
        Platform     = $Platform
        Entities     = $Entities
        Language     = $Language
    }
    & "$ModulesDir\Write-Frontend-RN-Config.ps1" @mobileParams
    & "$ModulesDir\Write-Frontend-RN-Navigation.ps1" @mobileParams
    & "$ModulesDir\Write-Frontend-RN-Feature.ps1" @mobileParams
    & "$ModulesDir\Write-Frontend-RN-Auth.ps1" @mobileParams
}

# 2. Web Frontend generation if selected
if ($Platform -match "Web|Both") {
    $reactParams = @{
        ProjectName  = $ProjectName
        OrmMode      = $OrmMode
        OutputPath   = $OutputPath
        FrontendMode = $FrontendMode
        Entities     = $Entities
        I18n         = $I18n
        Language     = $Language
    }
    
    switch ($FrontendMode) {
        { $_ -in "ReactTS","ReactJS" } {
            & "$ModulesDir\Write-Frontend-React-Config.ps1" @reactParams
            & "$ModulesDir\Write-Frontend-React-Components.ps1" @reactParams
            & "$ModulesDir\Write-Frontend-React-Pages.ps1" @reactParams
            & "$ModulesDir\Write-Frontend-EntityPages.ps1" @reactParams
            & "$ModulesDir\Write-Frontend-I18n.ps1" @reactParams
        }
        "NextJS" {
            & "$ModulesDir\Write-Frontend-NextJs-Config.ps1" @reactParams
            & "$ModulesDir\Write-Frontend-NextJs-Components.ps1" @reactParams
            & "$ModulesDir\Write-Frontend-NextJs-Pages.ps1" @reactParams
        }
        default {
            throw "Bilinmeyen FrontendMode: $FrontendMode"
        }
    }

    # If Both mode, rename the output "frontend" folder to "apps/web"
    if ($Platform -eq "Both") {
        $oldPath = Join-Path $OutputPath "frontend"
        $newPath = Join-Path $OutputPath "apps\web"
        if (Test-Path $oldPath) {
            $appsDir = Join-Path $OutputPath "apps"
            if (-not (Test-Path $appsDir)) { New-Item -ItemType Directory -Path $appsDir -Force | Out-Null }
            Move-Item -Path $oldPath -Destination $newPath -Force
        }
    }
}