import de.undercouch.gradle.tasks.download.Download
import org.gradle.crypto.checksum.Checksum

plugins {
    id 'org.springframework.boot'
    id 'io.spring.dependency-management'
    id "com.gorylenko.gradle-git-properties"
    id "checkstyle"
    id 'java'
    id 'idea'
    id 'jacoco'
    id "de.undercouch.download"
    id "io.freefair.lombok"
    id 'org.gradle.crypto.checksum'
    id 'org.springdoc.openapi-gradle-plugin'
}

group = 'run.halo.app'
compileJava.options.encoding = 'UTF-8'
compileTestJava.options.encoding = 'UTF-8'

java {
    sourceCompatibility = JavaVersion.VERSION_17
    targetCompatibility = JavaVersion.VERSION_17
}

idea {
    module {
        resourceDirs += file("../ui/build/dist/")
    }
}

checkstyle {
    toolVersion = "9.3"
    showViolations = false
    ignoreFailures = false
}

repositories {
    mavenCentral()

    maven { url 'https://repo.spring.io/milestone' }
    maven { url 'https://s01.oss.sonatype.org/content/repositories/snapshots' }
}


configurations {
    compileOnly {
        extendsFrom annotationProcessor
    }
}

springBoot {
    buildInfo {
        properties {
            artifact = 'halo'
            name = 'halo'
        }
    }
}

bootJar {
    archiveBaseName = 'halo'
    manifest {
        attributes 'Implementation-Title': 'Halo Application',
                'Implementation-Vendor': 'Halo OSS Team'
    }
}

tasks.named('jar') {
    enabled = false
}

dependencies {
    implementation project(':api')

    annotationProcessor "org.springframework.boot:spring-boot-configuration-processor"
    annotationProcessor "org.springframework:spring-context-indexer"

    testImplementation 'org.springframework.boot:spring-boot-starter-test'
    testImplementation 'org.springframework.security:spring-security-test'
    testImplementation 'io.projectreactor:reactor-test'
}

tasks.register('createChecksums', Checksum) {
    dependsOn tasks.named('bootJar')
    inputFiles.setFrom(layout.buildDirectory.files('libs'))
    outputDirectory = layout.buildDirectory.dir("libs")
    checksumAlgorithm = Checksum.Algorithm.SHA256
}

tasks.named('processResources', ProcessResources) {
    from project(':ui').layout.buildDirectory.dir('dist')
    into layout.buildDirectory.dir('resources/main')
    configure {
        mustRunAfter project(':ui').tasks.named('build')
    }
}

tasks.named('build') {
    dependsOn tasks.named('createChecksums')
}

tasks.named('test', Test) {
    useJUnitPlatform()
    finalizedBy jacocoTestReport
}

tasks.named('jacocoTestReport', JacocoReport) {
    reports {
        xml.required = true
        html.required = false
    }
}

ext.presetPluginUrls = [
        'https://github.com/halo-dev/plugin-comment-widget/releases/download/v2.3.1/plugin-comment-widget-2.3.1.jar'  : 'plugin-comment-widget.jar',
        'https://github.com/halo-dev/plugin-search-widget/releases/download/v1.4.0/plugin-search-widget-1.4.0.jar'    : 'plugin-search-widget.jar',
        'https://github.com/halo-dev/plugin-sitemap/releases/download/v1.1.1/plugin-sitemap-1.1.1.jar'                : 'plugin-sitemap.jar',
        'https://github.com/halo-dev/plugin-feed/releases/download/v1.2.2/plugin-feed-1.2.2.jar'                      : 'plugin-feed.jar',

        // Currently, plugin-app-store is not open source, so we need to download it from the official website.
        // Please see https://github.com/halo-dev/plugin-app-store/issues/55
        // https://www.halo.run/store/apps/app-VYJbF
        'https://www.halo.run/store/apps/app-VYJbF/releases/download/app-release-SRWfv/assets/app-release-SRWfv-VLtMA': 'appstore.jar',
]

tasks.register('downloadPluginPresets', Download) {
    doFirst {
        delete 'src/main/resources/presets/plugins'
    }

    src presetPluginUrls.keySet()

    dest 'src/main/resources/presets/plugins'

    eachFile { f ->
        f.name = presetPluginUrls[f.sourceURL.toString()]
    }
}

openApi {
    outputDir = file("$rootDir/api-docs/openapi/v3_0")
    groupedApiMappings = [
            'http://localhost:8091/v3/api-docs/apis_aggregated.api_v1alpha1': 'aggregated.json',
            'http://localhost:8091/v3/api-docs/apis_public.api_v1alpha1'    : 'apis_public.api_v1alpha1.json',
            'http://localhost:8091/v3/api-docs/apis_console.api_v1alpha1'   : 'apis_console.api_v1alpha1.json',
            'http://localhost:8091/v3/api-docs/apis_uc.api_v1alpha1'        : 'apis_uc.api_v1alpha1.json',
            'http://localhost:8091/v3/api-docs/apis_extension.api_v1alpha1' : 'apis_extension.api_v1alpha1.json',
    ]
    customBootRun {
        args = ['--server.port=8091',
                '--spring.profiles.active=doc',
                "--halo.work-dir=${layout.buildDirectory.get()}/tmp/workdir-for-generating-apidocs"]
    }
}

tasks.named('forkedSpringBootRun') {
    dependsOn ':api:jar'
}

tasks.named('generateOpenApiDocs') {
    outputs.upToDateWhen {
        false
    }
}
