def call(Map config = [:]) {
    def packageName = config.get('name', 'flutter-app')
    def apiVersion = config.get('api', '36')
    def dockerImage = "jenkins/build-flutter-android:${apiVersion}"

    // URLs configurables depuis le Jenkinsfile si besoin
    def debugApiBaseUrl = config.get('debugApiBaseUrl', 'http://192.168.1.37:8000/api/v1')
    def releaseApiBaseUrl = config.get('releaseApiBaseUrl', 'https://laundry-dev.abpcode.fr//api/v1')

    pipeline {
        agent any

        environment {
            PUB_CACHE = "${WORKSPACE}/.pub-cache"
            GRADLE_USER_HOME = "${WORKSPACE}/.gradle"
        }

        stages {
            stage('Prepare docker') {
                steps {
                    script {
                        writeFile file: 'Dockerfile', text: libraryResource("dockerfiles/flutter/android/${apiVersion}/Dockerfile")
                        docker.build(dockerImage, "--pull=false .")
                    }
                }
            }

            stage('Checkout') {
                steps {
                    cleanWs()
                    checkout scm
                }
            }

            stage('Flutter doctor') {
                steps {
                    script {
                        docker.image(dockerImage).inside('-u root') {
                            sh 'git config --global --add safe.directory "$WORKSPACE" || true'
                            sh 'flutter --version'
                            sh 'flutter doctor -v'
                        }
                    }
                }
            }

            stage('Dependencies') {
                steps {
                    script {
                        docker.image(dockerImage).inside('-u root') {
                            sh 'git config --global --add safe.directory "$WORKSPACE" || true'
                            sh 'flutter pub get'
                        }
                    }
                }
            }

            stage('Analyze') {
                steps {
                    script {
                        docker.image(dockerImage).inside('-u root') {
                            sh 'git config --global --add safe.directory "$WORKSPACE" || true'
                            sh 'flutter analyze'
                        }
                    }
                }
            }

            stage('Test') {
                steps {
                    script {
                        docker.image(dockerImage).inside('-u root') {
                            sh 'git config --global --add safe.directory "$WORKSPACE" || true'
                            sh 'flutter test'
                        }
                    }
                }
            }

            stage('Build Android') {
                steps {
                    script {
                        docker.image(dockerImage).inside('-u root') {
                            sh 'git config --global --add safe.directory "$WORKSPACE" || true'

                            if (env.TAG_NAME) {
                                sh """
                                    flutter build apk --release \
                                      --dart-define=API_BASE_URL=${releaseApiBaseUrl}
                                """
                            } else {
                                sh """
                                    flutter build apk --debug \
                                      --dart-define=API_BASE_URL=${debugApiBaseUrl}
                                """
                            }
                        }
                    }
                }
            }

            stage('Publish') {
                steps {
                    script {
                        if (env.TAG_NAME) {
                            env.deployment = 'prod'
                            env.version = "${env.TAG_NAME}-${env.BUILD_NUMBER}"
                            sh "cp build/app/outputs/flutter-apk/app-release.apk ./${packageName}-${env.deployment}-${env.version}.apk"
                        } else {
                            env.deployment = 'debug'
                            env.version = "${env.BUILD_NUMBER}"
                            sh "cp build/app/outputs/flutter-apk/app-debug.apk ./${packageName}-${env.deployment}-${env.version}.apk"
                        }

                        archiveArtifacts artifacts: "${packageName}-${env.deployment}-${env.version}.apk", fingerprint: true
                    }
                }
            }
        }
    }
}
