plugins {
    id 'base'
    id "com.github.node-gradle.node" version "7.1.0"
}

group 'top.nxxy335.commentaiautopilot.ui'

// Use system pnpm directly — avoids Windows exit code 268435659
// caused by Gradle Worker Daemon / node-gradle downloading pnpm on Windows
node {
    download = false
}

// Skip built-in pnpm tasks (they fail on Windows), replace with Exec-based tasks
tasks.named('pnpmSetup').configure { enabled = false }
tasks.named('pnpmInstall').configure { enabled = false }

// Cross-platform: use 'cmd /c' on Windows, direct 'pnpm' on Linux/macOS
def isWindows = System.properties['os.name'].toLowerCase().contains('windows')
def pnpmCmd = isWindows ? ['cmd', '/c', 'pnpm'] : ['pnpm']

tasks.register('uiInstall', Exec) {
    group = 'build'
    description = 'Install UI dependencies using system pnpm'
    workingDir layout.projectDirectory
    commandLine(pnpmCmd + ['install'])
}

tasks.register('uiBuild', Exec) {
    group = 'build'
    description = 'Build the UI project using pnpm'
    workingDir layout.projectDirectory
    commandLine(pnpmCmd + ['run', 'build'])
    dependsOn uiInstall
    inputs.dir(layout.projectDirectory.dir('src'))
    inputs.files(fileTree(
        dir: layout.projectDirectory,
        includes: ['*.cjs', '*.ts', '*.js', '*.json', '*.yaml']))
    outputs.dir(layout.buildDirectory.dir('dist'))
}

tasks.register('uiCheck', Exec) {
    group = 'verification'
    description = 'Run unit tests for the UI project using pnpm'
    workingDir layout.projectDirectory
    commandLine(pnpmCmd + ['run', 'test:unit'])
    dependsOn uiInstall
}

tasks.named('check') {
    dependsOn tasks.named('uiCheck')
}

tasks.named('assemble') {
    dependsOn tasks.named('uiBuild')
}
