default_platform(:mac)

platform :mac do
  desc "Create the LunaSea Keychain"
  lane :keychain_create do
    create_keychain(
      name: ENV["MATCH_KEYCHAIN_NAME"],
      password: ENV["MATCH_KEYCHAIN_PASSWORD"],
      default_keychain: is_ci,
      unlock: true,
      timeout: 3600,
      lock_when_sleeps: false
    )
  end

  desc "Delete the LunaSea Keychain"
  lane :keychain_delete do
    delete_keychain(name: ENV["MATCH_KEYCHAIN_NAME"])
  end
  
  desc "Setup the Keychain"
  lane :keychain_setup do
    # Development
    match(
      type: "development",
      readonly: is_ci,
      keychain_name: ENV["MATCH_KEYCHAIN_NAME"],
      keychain_password: ENV["MATCH_KEYCHAIN_PASSWORD"],
    )

    # App Store
    match(
      type: "appstore",
      additional_cert_types: "mac_installer_distribution",
      readonly: is_ci,
      keychain_name: ENV["MATCH_KEYCHAIN_NAME"],
      keychain_password: ENV["MATCH_KEYCHAIN_PASSWORD"],
    )

    # Direct
    match(
      type: "developer_id",
      additional_cert_types: "developer_id_installer",
      readonly: is_ci,
      keychain_name: ENV["MATCH_KEYCHAIN_NAME"],
      keychain_password: ENV["MATCH_KEYCHAIN_PASSWORD"],
    )
  end

  desc "Connect to App Store Connect"
  lane :connect_appstore_connect do
    app_store_connect_api_key(
      key_id: ENV["APPLE_STORE_CONNECT_KEY_ID"],
      issuer_id: ENV["APPLE_STORE_CONNECT_ISSUER_ID"],
      key_filepath: ENV["APPLE_STORE_CONNECT_KEY_FILEPATH"],
    )
  end

  desc "Build App Package for Direct Deployment"
  lane :build_app_package do |options|
    keychain_create
    keychain_setup
    connect_appstore_connect

    sh(
      "flutter", "build", "macos",
      "--release",
      "--build-number=#{options[:build_number]}",
    )
    build_mac_app(
      scheme: "Runner",
      workspace: "Runner.xcworkspace",
      installer_cert_name: ENV["MACOS_INSTALLER_CERT_DIRECT"],
      export_method: "developer-id",
      export_options: {
        provisioningProfiles: {
          "app.lunasea.lunasea" => "match Direct app.lunasea.lunasea macos",
        }
      }
    )

    notarize(
      package: "LunaSea.app",
      use_notarytool: true,
    )
    sh("mkdir", "-p", "../../output")
    sh("mv", "../LunaSea.app", "../../output/LunaSea.app")
    Dir.chdir("../../output") do
      sh("zip", "-r", "-y", "lunasea-macos-amd64.zip", "LunaSea.app")
      sh("rm", "-rf", "LunaSea.app")
    end

    keychain_delete
  end

  desc "Build App Package for App Store"
  lane :build_app_store do |options|
    keychain_create
    keychain_setup
    connect_appstore_connect

    sh(
      "flutter", "build", "macos",
      "--release",
      "--build-number=#{options[:build_number]}",
    )
    build_mac_app(
      scheme: "Runner",
      workspace: "Runner.xcworkspace",
      installer_cert_name: ENV["MACOS_INSTALLER_CERT_APP_STORE"],
      export_method: "app-store",
      export_options: {
        provisioningProfiles: {
          "app.lunasea.lunasea" => "match AppStore app.lunasea.lunasea macos"
        }
      },
    )
    sh("mkdir", "-p", "../../output")
    sh("mv", "../LunaSea.pkg", "../../output/lunasea-macos-amd64.pkg")

    keychain_delete
  end

  desc "Build Disk Image for Direct Deployment"
  lane :build_disk_image do |options|
    keychain_create
    keychain_setup
    connect_appstore_connect

    sh(
      "flutter", "build", "macos",
      "--release",
      "--build-number=#{options[:build_number]}",
    )
    build_mac_app(
      scheme: "Runner",
      workspace: "Runner.xcworkspace",
      installer_cert_name: ENV["MACOS_INSTALLER_CERT_DIRECT"],
      export_method: "developer-id",
      export_options: {
        provisioningProfiles: {
          "app.lunasea.lunasea" => "match Direct app.lunasea.lunasea macos",
        }
      }
    )
    
    Dir.chdir("../") do
      sh("create-dmg", "LunaSea.app", "--overwrite")
    end
    notarize(
      package: "LunaSea #{options[:build_version]}.dmg",
      bundle_id: "app.lunasea.lunasea.DiskImage",
      use_notarytool: true,
    )
    sh("mv", "../LunaSea #{options[:build_version]}.dmg", "../../output/lunasea-macos-amd64.dmg")

    keychain_delete
  end

  desc "Deploy to App Store Connect"
  lane :deploy_appstore do |options|
    connect_appstore_connect

    upload_to_testflight(
      changelog: File.read("./changelog.txt"),
      distribute_external: true,
      groups: options[:groups],
      pkg: options[:pkg],
      notify_external_testers: true,
    )
  end
end
