default_platform(:ios)

package = load_json(json_path: "package.json")

platform :ios do
    before_all do
        get_certificates(
            output_path: "certificates/"
        )
        get_provisioning_profile(
            output_path: "certificates/",
            filename: "provisioning.mobileprovision",
            fail_on_name_taken: false,
        )
        update_code_signing_settings(
            use_automatic_signing: true,
            path: "ios/Fintunes.xcodeproj"
        )
        increment_version_number(
            version_number: package["version"],
            xcodeproj: "ios/Fintunes.xcodeproj",
        );
        increment_build_number(
            xcodeproj: "ios/Fintunes.xcodeproj",
        )
    end

    lane :release do
        build_app(
            scheme: "Fintunes",
            output_directory: "build",
            workspace: "ios/Fintunes.xcworkspace",
            export_method: "app-store",
        )
        upload_to_app_store
    end
        
    lane :beta do 
        build_app(
            scheme: "Fintunes",
            output_directory: "build",
            workspace: "ios/Fintunes.xcworkspace",
            export_method: "app-store",
        )
        upload_to_testflight()
    end

    lane :build do
        build_app(
            scheme: "Fintunes",
            output_directory: "build",
            workspace: "ios/Fintunes.xcworkspace",
            export_method: "app-store",
        )
    end
    
    after_all do
        build_number = get_build_number(
            xcodeproj: "ios/Fintunes.xcodeproj",
        )
        Dir.chdir("..") do
            sh(
                "npx", "react-native", "bundle", 
                "--dev", "false",
                "--platform", "ios",
                "--entry-file", "index.js",
                "--bundle-output", "build/index.ios.bundle",
                "--sourcemap-output", "build/index.ios.bundle.map"
            )
        end

        sentry_create_release(
            version: package["version"],
            app_identifier: 'nl.moeilijkedingen.jellyfinaudioplayer',
            finalize: true
        )
        sentry_upload_dsym(
            dsym_path: "build/Fintunes.app.dSYM.zip",
            info_plist: "ios/Fintunes/Info.plist",
        )
        sentry_upload_file(
            version: package["version"],
            app_identifier: 'nl.moeilijkedingen.jellyfinaudioplayer',
            build: build_number,
            file: 'build/index.ios.bundle',
        )
        sentry_upload_sourcemap(
            version: package["version"],
            app_identifier: 'nl.moeilijkedingen.jellyfinaudioplayer',
            build: build_number,
            sourcemap: 'build/index.ios.bundle.map',
            rewrite: true
        )
    end

    lane :screenshots do
        capture_screenshots
        # upload_to_app_store
    end
end

platform :android do
    desc "Generate beta build"
    lane :beta do
        android_set_version_name(
            version_name: package["version"],
            gradle_file: "android/app/build.gradle"
        )
        android_set_version_code(    
            gradle_file: "android/app/build.gradle"
        )
        gradle(
            task: "assemble",
            build_type: "Release",
            project_dir: "android"
        )
        gradle(
            task: "bundle",
            build_type: "Release",
            project_dir: "android"
        )
        upload_to_play_store(
            track: "beta",
            skip_upload_apk: true
        )
    end

    lane :release do
        gradle(
            task: "bundle",
            build_type: "Release",
            project_dir: "android"
        )
        upload_to_play_store(
            skip_upload_apk: true
        )
    end
end
