plugins {
    id 'java'
    id 'org.springframework.boot' version '3.2.0'
    id 'io.spring.dependency-management' version '1.1.4'
}

group = 'com.cfdeployer'
version = '1.0.0'
sourceCompatibility = '21'

configurations {
    compileOnly {
        extendsFrom annotationProcessor
    }
}

repositories {
    mavenCentral()
}

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-web'
    implementation 'org.springframework.boot:spring-boot-starter-validation'

    compileOnly 'org.projectlombok:lombok'
    annotationProcessor 'org.projectlombok:lombok'

    testImplementation 'org.springframework.boot:spring-boot-starter-test'
}

tasks.named('test') {
    useJUnitPlatform()
}

// Task to download CF CLI binaries
task downloadCfCli {
    group = 'build'
    description = 'Downloads CF CLI binaries for Linux, macOS, and Windows'

    doLast {
        def cfCliVersion = '8.7.10'
        def resourcesDir = file("$projectDir/src/main/resources/cf-cli")
        resourcesDir.mkdirs()

        // Download for all platforms (useful for local development on different OSes)
        // For production Tanzu deployment, only Linux binary will be used
        def downloads = [
            [os: 'linux', url: "https://packages.cloudfoundry.org/stable?release=linux64-binary&version=${cfCliVersion}&source=github-rel", ext: 'tgz', executable: 'cf'],
            [os: 'macos', url: "https://packages.cloudfoundry.org/stable?release=macosx64-binary&version=${cfCliVersion}&source=github-rel", ext: 'tgz', executable: 'cf'],
            [os: 'windows', url: "https://packages.cloudfoundry.org/stable?release=windows64-exe&version=${cfCliVersion}&source=github-rel", ext: 'zip', executable: 'cf.exe']
        ]

        downloads.each { download ->
            def osDir = file("${resourcesDir}/${download.os}")
            osDir.mkdirs()

            def archiveFile = file("${osDir}/cf-cli.${download.ext}")

            if (!archiveFile.exists()) {
                println "Downloading CF CLI for ${download.os}..."

                ant.get(src: download.url, dest: archiveFile, verbose: true)

                println "Extracting CF CLI for ${download.os}..."
                if (download.ext == 'tgz') {
                    copy {
                        from tarTree(resources.gzip(archiveFile))
                        into osDir
                    }
                } else if (download.ext == 'zip') {
                    copy {
                        from zipTree(archiveFile)
                        into osDir
                    }
                }

                archiveFile.delete()
                println "CF CLI for ${download.os} downloaded and extracted successfully"
            } else {
                println "CF CLI for ${download.os} already exists, skipping download"
            }
        }
    }
}

// Task to download only Linux CF CLI (for production Tanzu builds)
task downloadCfCliLinuxOnly {
    group = 'build'
    description = 'Downloads CF CLI binary for Linux only (production builds)'

    doLast {
        def cfCliVersion = '8.7.10'
        def resourcesDir = file("$projectDir/src/main/resources/cf-cli")
        resourcesDir.mkdirs()

        def download = [os: 'linux', url: "https://packages.cloudfoundry.org/stable?release=linux64-binary&version=${cfCliVersion}&source=github-rel", ext: 'tgz', executable: 'cf']

        def osDir = file("${resourcesDir}/${download.os}")
        osDir.mkdirs()

        def archiveFile = file("${osDir}/cf-cli.${download.ext}")

        if (!archiveFile.exists()) {
            println "Downloading CF CLI for Linux..."
            ant.get(src: download.url, dest: archiveFile, verbose: true)

            println "Extracting CF CLI for Linux..."
            copy {
                from tarTree(resources.gzip(archiveFile))
                into osDir
            }

            archiveFile.delete()
            println "CF CLI for Linux downloaded and extracted successfully"
        } else {
            println "CF CLI for Linux already exists, skipping download"
        }
    }
}

// Ensure CF CLI is downloaded before building
// Use downloadCfCli for local development (all platforms)
// Use downloadCfCliLinuxOnly for production Tanzu builds (Linux only)
processResources.dependsOn downloadCfCli
