require "byebug"

# temp fix for NoMethodError: undefined method `last_comment'
# remove when fixed in Rake 11.x
# TODO - updating rspec should also fix this
module TempFixForRakeLastComment
  def last_comment
    last_description
  end
end
Rake::Application.send :include, TempFixForRakeLastComment
### end of temfix

Dir.glob("tasks/*.rake").each { |r| load r }

desc "Lints Gherkin and Ruby. Use this for local linting!"
task lint: ["gherkin:lint", "rubocop"]

namespace :ci do
  desc "Used by cucumber-tests-legacy-runthrough job in Jenkins"
  task all: [
    :init,
    "cucumber:full_legacy_run",
    "cucumber:store_all_logs"
  ]

  desc "Used by legacy-workstation-E2E"
  task legacy_workstation: [:init, "cucumber:legacy_workstation"]

  desc "Used by legacy-oa-E2E"
  task legacy_oa: [:init, "cucumber:legacy_oa"]

  desc "Used by frontend-distribution-cucumber-tests job in Jenkins"
  task frontend_distribution: [:init, "frontend:distribution"]

  desc "Used by frontend-distribution-oa-components-cucumber-tests job in Jenkins"
  task frontend_distribution_oa_components: [:init, "frontend:distribution_oa_components"]

  desc "Used by frontend-royalties-e2e-cucumber-tests-dc job in Jenkins"
  task frontend_royalties_docker_compose: [:init, "frontend:royalties_docker_compose"]

  desc "Used by frontend-royalties-e2e-cucumber-tests-qa job in Jenkins"
  task frontend_royalties_qa: [:init, "frontend:royalties_qa"]

  desc "Used by frontend-accounting-cucumber-tests job in Jenkins"
  task frontend_accounting: [:init, "frontend:accounting"]

  desc "Used by frontend-sales-sheets-cucumber-tests job in Jenkins"
  task frontend_sales_sheets: [:init, "frontend:sales_sheets"]

  desc "Used by frontend-masters-registry-cucumber-tests job in Jenkins"
  task frontend_masters_registry: [:init, "frontend:masters_registry"]

  desc "Used by frontend-reporting-cucumber-tests job in Jenkins"
  task frontend_reporting: [:init, "frontend:reporting"]

  desc "Used by frontend-video-cucumber-tests job in Jenkins"
  task frontend_video: [:init, "frontend:video"]

  desc "Used by frontend-pricing-cucumber-tests job in Jenkins"
  task frontend_pricing: [:init, "frontend:pricing"]

  desc "Used by frontend-conflict-manager-cucumber-tests job in Jenkins"
  task frontend_conflict_manager: [:init, "frontend:conflict_manager"]

  desc "Used by ows-accounting-cucumber-tests job in Jenkins"
  task ows_accounting: [:init, "ows:accounting"]

  desc "Used by frontend-audio-player-cucumber-tests job in Jenkins"
  task frontend_audio_player: [:init, "frontend:audio_player"]

  desc "Used by frontend-workstation-e2e-cucumber-tests job in Jenkins"
  task frontend_react_workstation: [:init, "frontend:react_workstation"]

  desc "Used by frontend-homepage-e2e-cucumber-tests job in Jenkins"
  task frontend_react_homepage: [:init, "frontend:react_homepage"]

  desc "Used by frontend-catalog-e2e-cucumber-tests job in Jenkins"
  task frontend_react_catalog: [:init, "frontend:react_catalog"]

  desc "Used by frontend-source-of-streams-e2e-cucumber-tests job in Jenkins"
  task frontend_react_engagement: [:init, "frontend:react_engagement"]

  desc "Used by frontend-feature-fm-e2e-cucumber-tests job in Jenkins"
  task frontend_react_feature_fm: [:init, "frontend:react_feature_fm"]

  desc "Used by frontend-audience-page-e2e-cucumber-tests job in Jenkins"
  task frontend_react_audience: [:init, "frontend:react_audience"]

  desc "Used by frontend-comparison-page-e2e-cucumber-tests job in Jenkins"
  task frontend_react_analytics: [:init, "frontend:react_analytics"]

  desc "Used by auth0-hosted-pages-e2e-cucumber-tests job in Jenkins"
  task frontend_auth0: [:init, "frontend:auth0"]

  desc "Used by frontend-dd-search-e2e-cucumber-tests job in Jenkins"
  task frontend_dd_search: [:init, "frontend:dd_search"]

  desc "Used by frontend-contract-e2e-cucumber-tests job in Jenkins"
  task frontend_contract: [:init, "frontend:contract"]

  desc "Used by frontend-blacklist-manager-e2e-cucumber-tests job in Jenkins"
  task frontend_blacklist_manager: [:init, "frontend:blacklist_manager"]

  desc "Used by frontend-users-e2e-cucumber-tests job in Jenkins"
  task frontend_users: [:init, "frontend:users"]

  desc "Used by ows-product-physical-cucumber-tests job in Jenkins"
  task ows_product_physical: [:init, "ows:product_physical"]

  desc "Used by ows-reporting-cucumber-tests job in Jenkins"
  task ows_reporting: [:init, "ows:reporting"]

  desc "Used by ows-grass-cucucmber-tests job in Jenkins"
  task ows_grass: [:init, "ows:grass"]

  desc "Used by ows-artists-cucumber-tests job in Jenkins"
  task ows_artist: [:init, "ows:artist"]

  desc "Used by ows-assets-cucumber-tests job in Jenkins"
  task ows_assets: [:init, "ows:assets"]

  desc "Used by ows-track-cucumber-tests job in Jenkins"
  task ows_track: [:init, "ows:track"]

  desc "Used by ows-carveouts-cucumber-tests job in Jenkins"
  task ows_carveouts: [:init, "ows:carveouts"]

  desc "Used by ows-territories-cucumber-tests job in Jenkins"
  task ows_territories: [:init, "ows:territories"]

  desc "Used by ows-royalties-cucumber-tests job in Jenkins"
  task ows_royalties: [:init, "ows:royalties"]

  desc "Used by ows-payee-cucumber-tests job in Jenkins"
  task ows_payee: [:init, "ows:payee"]

  desc "Used by ows-payment-cucumber-tests job in Jenkins"
  task ows_payment: [:init, "ows:payment"]

  desc "Used by ows-ledger-cucumber-tests job in Jenkins"
  task ows_ledger: [:init, "ows:ledger"]

  desc "Used by ows-abacus-contract-cucumber-tests job in Jenkins"
  task ows_abacus_contract: [:init, "ows:abacus_contract"]

  desc "Used by ows-project-manager-cucumber-tests job in Jenkins"
  task ows_project_manager: [:init, "ows:project_manager"]

  desc "Used by ows-product-cucumber-tests job in Jenkins"
  task ows_product: [:init, "ows:product"]

  desc "Used by ows-product-digital-cucumber-tests in Jenkins"
  task ows_product_digital: [:init, "ows:product_digital"]

  desc "Used by ows-product-configuration-cucumber-tests in Jenkins"
  task ows_product_configuration: [:init, "ows:product_configuration"]

  desc "Used by ows-account-cucumber-tests job in Jenkins"
  task ows_account: [:init, "ows:account"]

  desc "Used by ows-metadata-cucumber-tests job in Jenkins"
  task ows_metadata: [:init, "ows:metadata"]

  desc "Used by ows-sales-sheets-cucumber-tests job in Jenkins"
  task ows_sales_sheets: [:init, "ows:sales_sheets"]

  desc "Used by ows-masters-registry-cucumber-tests job in Jenkins"
  task ows_masters_registry: [:init, "ows:masters_registry"]

  desc "Used by ows-marketing-cucumber-tests job in Jenkins"
  task ows_marketing: [:init, "ows:marketing"]

  desc "Used by ows-conflict-manager-cucumber-tests job in Jenkins"
  task ows_conflict_manager: [:init, "ows:conflict_manager"]

  desc "Used by ows-promo-player-cucumber-tests job in Jenkins"
  task ows_promo_player: [:init, "ows:promo_player"]

  desc "Used by ows-vectororder-cucumber-tests job in Jenkins"
  task ows_vectororder: [:init, "ows:vectororder"]

  desc "Used by ows-abacus-event-e2e job in Jenkins"
  task ows_abacus_event: [:init, "ows:abacus_event"]

  desc "Used by ows-search-cucumber-tests job in Jenkins"
  task ows_search: [:init, "ows:search"]

  desc "Used by ows-lyrics-e2e-cucumber-tests job in Jenkins"
  task ows_lyrics: [:init, "ows:lyrics"]

  desc "Used by ows-product-workflow-e2e-cucumber-tests job in Jenkins"
  task ows_product_workflow: [:init, "ows:product_workflow"]

  desc "Used by ows-stores-e2e-cucumber-tests job in Jenkins"
  task ows_stores: [:init, "ows:stores"]

  desc "Used by ows-sales-goals-e2e-cucumber-tests job in Jenkins"
  task ows_sales_goals: [:init, "ows:sales_goals"]

  desc "Used by mobile-analytics-cucumber-tests job in Jenkins"
  task mobile_analytics: [:init, "cucumber:mobile_analytics"]

  desc "Used by ows-product-film-e2e-cucumber-tests job in Jenkins"
  task ows_product_film: [:init, "ows:product_film"]

  desc "Used by ows-contracts-e2e-cucumber-tests job in Jenkins"
  task ows_contracts: [:init, "ows:contracts"]

  desc "Used by ows-users-e2e-cucumber-tests job in Jenkins"
  task ows_users: [:init, "ows:users"]

  desc "Used by ows-video-cucumber-tests job in Jenkins"
  task ows_video: [:init, "ows:video"]

  desc "Used by ows-timed-release-e2e-cucumber-tests job in Jenkins"
  task ows_timed_release: [:init, "ows:timed_release"]

  desc "Used by ows-blacklist-manager-e2e-cucumber-tests job in Jenkins"
  task ows_blacklist_manager: [:init, "ows:blacklist_manager"]

  desc "Used by ows-collaborator-e2e-cucumber-tests job in Jenkins"
  task ows_collaborator: [:init, "ows:collaborator"]

  desc "Used by ows-features-e2e-cucumber-tests job in Jenkins"
  task regression: [:init, "cucumber:regression_legacy"]

  desc "Used by Orchard-PR build in Jenkins"
  task pr: [:init, "cucumber:pr"]

  desc "Sanity check new cukes against QAorch"
  task sanity_pr: [
    :init,
    "cucumber:pr",
    "cucumber:full_legacy_run",
    "cucumber:standalone",
    "cucumber:prod"
  ]

  desc "Sanity check new STANDALONE and PROD cukes against QAorch"
  task sanity_standalone_pr: [
    :init,
    "cucumber:pr",
    "cucumber:standalone",
    "cucumber:prod"
  ]

  desc "Run Cucumber Acceptance Tests against a PR"
  task uat_pr: [:init, "cucumber:pr"]

  desc "Run ci:all using only fast scenarios (good for debugging)"
  task debug_all: [:init, "cucumber:fast_ci"]

  desc "Run continuous tests against production"
  task prod: [:init, "cucumber:prod"]

  desc "Used by ows-metadata-cucumber-tests job in Jenkins against production"
  task ows_metadata_prod: [:init, "ows:metadata_prod"]

  desc "Initialize the environment for the CI"
  task init: ["cucumber:init_dotenv", "cucumber:sync_env"] do
    Support::EntryPoint.new.setup_ci_workspace
  end

  desc "Run regression for roles with limited permissions on workstation"
  task alw_grass_roles: [:init, "cucumber:alw_grass_roles"]

  desc "Run smoke tests against dev environment"
  task dev: [:init, "cucumber:dev"]

  namespace :cache do
    desc "Clear redis cache"
    task :clear do
      sh "redis-cli -h qaredis01.qaorch.com FLUSHALL"
    end

    desc "Warm cache by running @cache_warm scenarios with extra-long timeout"
    task warm: [:init, "cucumber:cache_warm"]

    desc "Clear then warm redis cache"
    task daily: ["cache:clear", "cache:warm"]
  end
end

desc "Used by cucumber physical delivery bootstrap jenkins job"
task physical_asset_creation: ["ci:init", "scripts:physical_asset_creation"]

desc "Run PR tests against the local branch"
task pr: "cucumber:pr"

task default: "ci:all"
