plugins {
    id 'java'
    id 'application'
    id 'eclipse'
    id 'idea'
}

group = 'com.openosrs'
version = '1.0.0'
description = 'OpenOSRS Modernized Client - Agent-Friendly RuneScape Client'

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

application {
    mainClass = 'com.openosrs.client.ModernizedClient'
    applicationName = 'modernized-client'
}

repositories {
    mavenCentral()
    maven {
        url 'https://repo.runelite.net'
        content {
            includeGroupByRegex "net\\.runelite.*"
        }
    }
    maven {
        url 'https://raw.githubusercontent.com/open-osrs/hosting/master'
        content {
            includeGroupByRegex "com\\.openosrs.*"
        }
    }
}

dependencies {
    // Logging
    implementation 'org.slf4j:slf4j-api:2.0.9'
    implementation 'ch.qos.logback:logback-classic:1.4.11'

    // JSON processing
    implementation 'com.fasterxml.jackson.core:jackson-core:2.15.2'
    implementation 'com.fasterxml.jackson.core:jackson-databind:2.15.2'
    implementation 'com.fasterxml.jackson.core:jackson-annotations:2.15.2'

    // Utilities
    implementation 'com.google.guava:guava:32.1.2-jre'
    implementation 'org.apache.commons:commons-lang3:3.13.0'
    implementation 'commons-io:commons-io:2.13.0'

    // Networking
    implementation 'org.apache.httpcomponents.client5:httpclient5:5.2.1'
    implementation 'org.apache.httpcomponents.core5:httpcore5:5.2.2'

    // Concurrency
    implementation 'net.jcip:jcip-annotations:1.0'

    // Optional: RuneLite API compatibility (if needed)
    // implementation 'net.runelite:runelite-api:1.10.17'

    // Testing
    testImplementation 'org.junit.jupiter:junit-jupiter:5.10.0'
    testImplementation 'org.mockito:mockito-core:5.5.0'
    testImplementation 'org.mockito:mockito-junit-jupiter:5.5.0'
    testImplementation 'org.assertj:assertj-core:3.24.2'
}

tasks.withType(JavaCompile) {
    options.encoding = 'UTF-8'
    options.compilerArgs.addAll([
        '-Xlint:all',
        '-Xlint:-processing',
        '-Werror'
    ])
}

test {
    useJUnitPlatform()
    testLogging {
        events "passed", "skipped", "failed"
        exceptionFormat "full"
    }
}

jar {
    manifest {
        attributes(
            'Main-Class': 'com.openosrs.client.ModernizedClient',
            'Implementation-Title': 'OpenOSRS Modernized Client',
            'Implementation-Version': project.version,
            'Implementation-Vendor': 'OpenOSRS',
            'Built-Date': new Date(),
            'Built-By': System.getProperty('user.name'),
            'Built-JDK': System.getProperty('java.version')
        )
    }

    // Include dependencies in fat jar for easy distribution
    from {
        configurations.runtimeClasspath.collect { it.isDirectory() ? it : zipTree(it) }
    }

    duplicatesStrategy = DuplicatesStrategy.EXCLUDE

    exclude 'META-INF/*.SF'
    exclude 'META-INF/*.DSA'
    exclude 'META-INF/*.RSA'
}

// Task to create a distribution package
task createDistribution(type: Zip) {
    group = 'distribution'
    description = 'Creates a distribution package'

    archiveFileName = "modernized-client-${version}.zip"
    destinationDirectory = file("$buildDir/distributions")

    from jar
    from 'README.md'
    from 'LICENSE'

    into('scripts') {
        from fileTree('scripts')
        filePermissions {
            unix(0755)
        }
    }

    into('config') {
        from fileTree('config')
    }
}

// Development tasks
task runWithDebug(type: JavaExec) {
    group = 'development'
    description = 'Run the client with debug logging'

    classpath = sourceSets.main.runtimeClasspath
    mainClass = 'com.openosrs.client.ModernizedClient'

    systemProperty 'org.slf4j.simpleLogger.defaultLogLevel', 'DEBUG'
    systemProperty 'org.slf4j.simpleLogger.showDateTime', 'true'
    systemProperty 'org.slf4j.simpleLogger.dateTimeFormat', 'HH:mm:ss.SSS'

    args = ['--debug']
}

task generateApiDocs(type: Javadoc) {
    group = 'documentation'
    description = 'Generate API documentation'

    source = sourceSets.main.allJava
    classpath = sourceSets.main.runtimeClasspath

    options {
        title = "OpenOSRS Modernized Client API"
        author = true
        version = true
        use = true
        windowTitle = "OpenOSRS Modernized Client API"
        docTitle = "OpenOSRS Modernized Client API"

        links(
            'https://docs.oracle.com/en/java/javase/17/docs/api/',
            'https://www.slf4j.org/apidocs/',
            'https://google.github.io/guava/releases/32.1.2-jre/api/docs/'
        )

        addStringOption('Xdoclint:none', '-quiet')
    }

    destinationDir = file("$buildDir/docs/api")

    include '**/api/**'
    include '**/scripting/**'
    include '**/plugins/**'
}

// Quality assurance
task checkCodeStyle {
    group = 'verification'
    description = 'Check code style and conventions'

    doLast {
        println "Code style check would go here"
        // Could integrate with SpotBugs, PMD, or Checkstyle
    }
}

// Performance profiling
task profileStartup(type: JavaExec) {
    group = 'profiling'
    description = 'Profile client startup performance'

    classpath = sourceSets.main.runtimeClasspath
    mainClass = 'com.openosrs.client.ModernizedClient'

    jvmArgs = [
        '-XX:+PrintGCDetails',
        '-XX:+PrintGCTimeStamps',
        '-XX:+PrintGCApplicationStoppedTime',
        '-Xloggc:build/gc.log'
    ]

    systemProperty 'profile.startup', 'true'
}

// Example script runners
task runWoodcuttingExample(type: JavaExec) {
    group = 'examples'
    description = 'Run the woodcutting script example'

    classpath = sourceSets.main.runtimeClasspath
    mainClass = 'com.openosrs.client.ModernizedClient'
    args = ['--script', 'woodcutting', '--auto-start']
}

task runCombatExample(type: JavaExec) {
    group = 'examples'
    description = 'Run the combat training script example'

    classpath = sourceSets.main.runtimeClasspath
    mainClass = 'com.openosrs.client.ModernizedClient'
    args = ['--script', 'combat-training', '--auto-start']
}

task runLoginExample(type: JavaExec) {
    group = 'examples'
    description = 'Run the login system demonstration'

    classpath = sourceSets.main.runtimeClasspath
    mainClass = 'com.openosrs.client.examples.ExampleLoginAgent'

    // Enable debug logging for the example
    systemProperty 'org.slf4j.simpleLogger.defaultLogLevel', 'DEBUG'
    systemProperty 'org.slf4j.simpleLogger.showDateTime', 'true'
    systemProperty 'org.slf4j.simpleLogger.dateTimeFormat', 'HH:mm:ss.SSS'

    standardInput = System.in
}

// Clean up build artifacts
clean {
    delete 'logs'
    delete 'cache'
    delete 'profiles'
}

// IDE integration
eclipse {
    project {
        name = 'modernized-client'
        comment = 'OpenOSRS Modernized Client for AI Agents'
    }
}

idea {
    module {
        downloadJavadoc = true
        downloadSources = true
    }
}

// Wrapper configuration
wrapper {
    gradleVersion = '8.3'
    distributionType = Wrapper.DistributionType.ALL
}
