plugins {
    id 'java'
    id 'application'
    id 'jvm-test-suite'
    id 'gg.jte.gradle' version '3.1.15'
    id 'com.google.cloud.tools.jib' version '3.4.5'
}

application {
    mainClass = 'nu.marginalia.search.SearchMain'
    applicationName = 'search-service'
}

tasks.distZip.enabled = false

jte {
    sourceDirectory = file('resources/jte').toPath()
    targetDirectory = file('build/classes/jte-precompiled').toPath()
    generate()
}
java {
    toolchain {
        languageVersion.set(JavaLanguageVersion.of(rootProject.ext.jvmVersion))
    }
}

apply from: "$rootProject.projectDir/srcsets.gradle"
apply from: "$rootProject.projectDir/docker.gradle"

dependencies {
    implementation project(':code:common:db')
    implementation project(':code:common:model')
    implementation project(':code:common:service')
    implementation project(':code:common:config')
    implementation project(':code:index:query')

    implementation project(':code:libraries:easy-lsh')
    implementation project(':code:libraries:language-processing')
    implementation project(':code:libraries:braille-block-punch-cards')
    implementation project(':code:libraries:term-frequency-dict')

    implementation project(':code:functions:live-capture:api')
    implementation project(':code:functions:math:api')
    implementation project(':code:functions:favicon:api')
    implementation project(':code:functions:domain-info:api')
    implementation project(':code:functions:search-query:api')


    implementation project(':code:index:api')

    implementation project(':code:features-search:screenshots')
    implementation project(':code:features-search:random-websites')

    implementation libs.bundles.slf4j

    implementation libs.roaringbitmap
    implementation libs.prometheus
    implementation libs.notnull
    implementation libs.guava
    implementation dependencies.create(libs.guice.get()) {
        exclude group: 'com.google.guava'
    }
    implementation libs.handlebars
    implementation libs.opencsv
    implementation libs.trove
    implementation libs.jte

    libs.bundles.jooby.get().each {
        implementation dependencies.create(it) {
            // Jooby pulls in an incompatible slf4j version that breaks all logs
            exclude group: 'org.slf4j'
        }
    }

    implementation libs.fastutil
    implementation libs.bundles.gson
    implementation libs.bundles.mariadb
    implementation libs.bundles.nlp

    annotationProcessor libs.jooby.apt

    testImplementation libs.bundles.slf4j.test
    testImplementation libs.bundles.junit
    testImplementation libs.mockito

    testImplementation platform('org.testcontainers:testcontainers-bom:1.17.4')
    testImplementation libs.commons.codec
    testImplementation 'org.testcontainers:mariadb:1.17.4'
    testImplementation 'org.testcontainers:junit-jupiter:1.17.4'
    testImplementation project(':code:libraries:test-helpers')

    testImplementation dependencies.create(libs.spark.get())
}

task compileTailwind {
    def inputFile = file('tailwind/globals.css')
    def configFile = file('tailwind/tailwind.config.js')
    def outputFile = file('resources/static/css/style.css')
    def jteDir = file('resources/jte')

    inputs.file inputFile
    inputs.file configFile
    inputs.files fileTree(jteDir).include('**/*.jte')
    outputs.file outputFile

    doLast {
        exec {
            // If you're getting a build error like 'npm error could not determine executable to run'
            // pointing you here, you need to run  `npm install -D tailwindcss`
            workingDir projectDir
            if (System.getProperty('os.name').toLowerCase().contains('windows')) {
                commandLine 'cmd', '/c', 'npx', 'tailwindcss',
                        '-i', inputFile.toString(),
                        '-o', outputFile.toString(),
                        '-c', configFile.toString()
            } else {
                commandLine 'npx', 'tailwindcss',
                        '-i', inputFile.toString(),
                        '-o', outputFile.toString(),
                        '-c', configFile.toString()
            }
        }
    }
}

processResources.dependsOn(compileTailwind)

tasks.register('paperDoll', Test) {
    useJUnitPlatform {
        includeTags "paperdoll"
    }
    jvmArgs = [ '-DrunPaperDoll=true', '--enable-preview' ]
}
