plugins {
    id 'base'
}

group = 'org.thoughtframe.runtime'

// Host/process knobs come in as -P properties (see runtime/scripts/publish.sh):
//   -Pregistry -Pimage -Ptag -PosImage -PosTag -Pplatform -PwithLibreOffice
def registry    = findProperty('registry')    ?: 'registry.thoughtframe.ai'
def imageName   = findProperty('image')       ?: 'thoughtframe-runtime'
def imageTag    = findProperty('tag')         ?: 'dev'
def osImageName = findProperty('osImage')     ?: 'thoughtframe-os'
def osImageTag  = findProperty('osTag')       ?: 'latest'
def platform    = findProperty('platform')
def withLibreOffice = findProperty('withLibreOffice')

def osDir = layout.projectDirectory.dir('thoughtframe-os')
def rtDir = layout.projectDirectory.dir('runtime')

// --- thoughtframe-os: base image (JDK / apt / python). Rebuilt rarely. ---
tasks.register('dockerOsBuild', Exec) {
    description = 'Build the thoughtframe-os base image (registry-tagged + local alias)'
    workingDir = osDir.asFile
    def args = ['docker', 'build',
                '-t', "${registry}/${osImageName}:${osImageTag}",
                '-t', "${osImageName}:${osImageTag}"]
    if (platform) {
        args += ['--platform', platform]
    }
    if (withLibreOffice != null) {
        args += ['--build-arg', "WITH_LIBREOFFICE=${withLibreOffice}"]
    }
    args += ['.']
    commandLine args
}

tasks.register('dockerOsPush', Exec) {
    description = 'Build and push the thoughtframe-os base image'
    dependsOn 'dockerOsBuild'
    commandLine 'docker', 'push', "${registry}/${osImageName}:${osImageTag}"
}

// --- thoughtframe-runtime: Jetty + the app WAR baked in. Rebuilt per release. ---
tasks.register('stageRuntime', Copy) {
    description = 'Bake the freshly built app WAR into the runtime build context'
    dependsOn ':thoughtframe:createWar'
    from(project(':thoughtframe').layout.projectDirectory.file('deploy/ROOT.war'))
    into rtDir
}

tasks.register('dockerBuild', Exec) {
    description = 'Build thoughtframe-runtime FROM thoughtframe-os (WAR baked in)'
    dependsOn 'stageRuntime'
    workingDir = rtDir.asFile
    def args = ['docker', 'build',
                '--build-arg', "OS_IMAGE=${registry}/${osImageName}:${osImageTag}",
                '-t', "${registry}/${imageName}:${imageTag}"]
    if (platform) {
        args += ['--platform', platform]
    }
    args += ['.']
    commandLine args
}

tasks.register('dockerPush', Exec) {
    description = 'Push thoughtframe-runtime to the registry (numbered tag + :latest)'
    dependsOn 'dockerBuild'
    commandLine 'bash', '-lc',
        "docker push ${registry}/${imageName}:${imageTag} && " +
        "docker tag ${registry}/${imageName}:${imageTag} ${registry}/${imageName}:latest && " +
        "docker push ${registry}/${imageName}:latest"
}

// --- local run (uses runtime/site.sh, falling back to site.sh.example defaults) ---
tasks.register('dockerUp', Exec) {
    description = 'docker compose up for the local runtime site'
    workingDir = rtDir.asFile
    commandLine 'docker', 'compose', 'up', '-d'
}

tasks.register('dockerDown', Exec) {
    description = 'docker compose down for the local runtime site'
    workingDir = rtDir.asFile
    commandLine 'docker', 'compose', 'down'
}

tasks.register('release') {
    dependsOn 'dockerPush'
}
