// ci-generator/android-jobs.ts — Android CI job builders for Gradle + Play Store (SPEC-325)
// -- Shared helpers -----------------------------------------------------------
function checkoutStep() {
    return { name: 'Checkout code', uses: 'actions/checkout@v4' };
}
function javaSetupStep() {
    return {
        name: 'Setup JDK',
        uses: 'actions/setup-java@v4',
        with: { 'java-version': '17', distribution: 'temurin' },
    };
}
function gradleCacheStep() {
    return {
        name: 'Cache Gradle packages',
        uses: 'actions/cache@v4',
        with: {
            path: '~/.gradle/caches\n~/.gradle/wrapper',
            key: "${{ runner.os }}-gradle-${{ hashFiles('**/*.gradle*', '**/gradle-wrapper.properties') }}",
            'restore-keys': '${{ runner.os }}-gradle-',
        },
    };
}
function gradlePermissionsStep() {
    return {
        name: 'Grant Gradle execute permission',
        run: 'chmod +x gradlew',
    };
}
/** Common env vars required for Play Store keystore signing. */
function keystoreEnv() {
    return {
        KEYSTORE_FILE: '${{ secrets.KEYSTORE_FILE }}',
        KEYSTORE_PASSWORD: '${{ secrets.KEYSTORE_PASSWORD }}',
        KEY_ALIAS: '${{ secrets.KEY_ALIAS }}',
        KEY_PASSWORD: '${{ secrets.KEY_PASSWORD }}',
    };
}
function resolveModule(config) {
    return config.scheme ?? 'app';
}
// -- Job builders -------------------------------------------------------------
/**
 * Build a Gradle test job that runs unit tests on ubuntu-latest.
 * Uses JDK 17 (Temurin) and caches Gradle dependencies.
 */
export function buildGradleTestJob(config) {
    const module = resolveModule(config);
    const steps = [
        checkoutStep(),
        javaSetupStep(),
        gradleCacheStep(),
        gradlePermissionsStep(),
        {
            name: 'Run unit tests',
            run: `./gradlew :${module}:testDebugUnitTest`,
        },
        {
            name: 'Upload test reports',
            uses: 'actions/upload-artifact@v4',
            with: {
                name: 'test-results',
                path: `${module}/build/reports/tests/`,
                'if-no-files-found': 'warn',
            },
        },
    ];
    return {
        name: 'Android — Gradle Unit Tests',
        runsOn: 'ubuntu-latest',
        steps,
    };
}
/**
 * Build an APK/AAB signing job using the project keystore.
 * Decodes the base64-encoded KEYSTORE_FILE secret and runs assembleRelease.
 */
export function buildPlayStoreSigningJob(config) {
    const module = resolveModule(config);
    const steps = [
        checkoutStep(),
        javaSetupStep(),
        gradleCacheStep(),
        gradlePermissionsStep(),
        {
            name: 'Decode keystore',
            run: 'echo "$KEYSTORE_FILE" | base64 --decode > keystore.jks',
            env: keystoreEnv(),
        },
        {
            name: 'Build signed release AAB',
            run: [
                `./gradlew :${module}:bundleRelease \\`,
                `  -Pandroid.injected.signing.store.file=$(pwd)/keystore.jks \\`,
                `  -Pandroid.injected.signing.store.password="$KEYSTORE_PASSWORD" \\`,
                `  -Pandroid.injected.signing.key.alias="$KEY_ALIAS" \\`,
                `  -Pandroid.injected.signing.key.password="$KEY_PASSWORD"`,
            ].join('\n'),
            env: keystoreEnv(),
        },
        {
            name: 'Upload signed AAB',
            uses: 'actions/upload-artifact@v4',
            with: {
                name: 'release-aab',
                path: `${module}/build/outputs/bundle/release/*.aab`,
            },
        },
    ];
    return {
        name: 'Android — Sign Release AAB',
        runsOn: 'ubuntu-latest',
        steps,
        env: keystoreEnv(),
    };
}
/**
 * Build a Play Console deploy job that uploads the signed AAB via Fastlane Supply.
 * Depends on the signing job artifact. Requires PLAY_STORE_JSON_KEY secret.
 */
export function buildPlayConsoleDeployJob(config) {
    const module = resolveModule(config);
    const packageName = config.bundleId ?? 'com.example.app';
    const steps = [
        checkoutStep(),
        {
            name: 'Download signed AAB',
            uses: 'actions/download-artifact@v4',
            with: { name: 'release-aab', path: './release' },
        },
        {
            name: 'Setup Ruby for Fastlane',
            uses: 'ruby/setup-ruby@v1',
            with: { 'ruby-version': 'bundler-cache: true' },
        },
        {
            name: 'Install Fastlane',
            run: 'gem install fastlane --no-document',
        },
        {
            name: 'Decode Play Store JSON key',
            run: 'echo "$PLAY_STORE_JSON_KEY" > play-store-key.json',
            env: {
                PLAY_STORE_JSON_KEY: '${{ secrets.PLAY_STORE_JSON_KEY }}',
            },
        },
        {
            name: 'Upload to Play Console (internal track)',
            run: [
                'fastlane supply \\',
                `  --aab ./release/${module}/build/outputs/bundle/release/*.aab \\`,
                `  --package_name "${packageName}" \\`,
                '  --track internal \\',
                '  --json_key ./play-store-key.json',
            ].join('\n'),
        },
    ];
    return {
        name: 'Android — Play Console Deploy',
        runsOn: 'ubuntu-latest',
        needs: ['android-sign'],
        steps,
        env: {
            PLAY_STORE_JSON_KEY: '${{ secrets.PLAY_STORE_JSON_KEY }}',
        },
    };
}
//# sourceMappingURL=android-jobs.js.map