// Manages standalone scheduler jobs that do not fit within the Organization Folder structure.
import jenkins.model.Jenkins
import hudson.model.Item
import hudson.model.Items

def schedulerJobs = [
    [
        name: 'ows-analytics-cache-warmup',
        description: 'This job warms up the Redis cache of ows-analytics as the last step part of dbt-scheduler-analytics-pipeline',
        repo: 'theorchard/ows-analytics',
        scriptPath: 'warmup_cache/Jenkinsfile'
    ],
    [
        name: 'dbt-fivetran-marketing-pipeline-daily',
        description: 'Daily dbt run of dbt-fivetran-marketing models behind the Fansifter app.',
        repo: 'theorchard/dbt-fivetran-marketing',
        scriptPath: 'Jenkinsfile'
    ],
    [
        name: 'dbt-fansifter-shopify-pipeline-scheduler',
        description: 'Scheduled run of dbt-fansifter-shopify models behind the Fansifter app.',
        repo: 'theorchard/dbt-fansifter-shopify',
        scriptPath: 'Jenkinsfile'
    ],
    [
        name: 'ows-text-campaigns-scheduler',
        description: 'Hourly dbt run of ows-text-campaigns scripts behind the Fansifter app.',
        repo: 'theorchard/ows-text-campaigns',
        scriptPath: 'Jenkinsfile.scheduler'
    ],
    [
        name: 'dbt-accounting-scheduler',
        description: 'Daily dbt run of dbt-accounting-scheduler models behind the Customer Accounting app.',
        repo: 'theorchard/dbt-accounting',
        scriptPath: 'Jenkinsfile'
    ],
    [
        name: 'dbt-audience-pipeline-daily',
        description: 'Daily dbt run of dbt-audience-pipeline-daily models behind the Fansifter app.',
        repo: 'theorchard/dbt-audience',
        scriptPath: 'Jenkinsfile'
    ],
    [
        name: 'dbt-luminate-scheduler',
        description: 'Scheduled dbt-luminate runs.',
        repo: 'theorchard/dbt-luminate',
        scriptPath: 'Jenkinsfile.scheduler'
    ],
    [
        name: 'dbt-analytics-hourly-pipeline',
        description: 'Hourly dbt run of hourly playlists models.',
        repo: 'theorchard/dbt-analytics',
        scriptPath: 'jenkins/dbt-analytics-hourly-pipeline/Jenkinsfile'
    ],
    [
        name: 'shopify-global-schema',
        description: 'Regular refresh of global shopify schema tables.',
        repo: 'theorchard/shopify-global-schema',
        scriptPath: 'Jenkinsfile.scheduler'
    ],
    // SWF exec jobs starts here
    [
        name: 'swf-any-exec',
        description: 'SWF exec any SWF flow for QA and PROD.',
        repo: 'theorchard/swf-feed-ingestion',
        scriptPath: 'feed_ingestion/flows/_anyexec/Jenkinsfile'
    ],
    [
        name: 'swf-yt-conflict-elasticsearch-exec',
        description: 'Execute swf-yt-conflict-elasticsearch',
        repo: 'theorchard/swf-yt-conflict-elasticsearch',
        scriptPath: 'exec.groovy'
    ],
    [
        name: 'swf-backload-runner-v2-exec',
        description: 'swf exec backload-runner-exec for QA and PROD',
        repo: 'theorchard/swf-feed-ingestion',
        scriptPath: 'feed_ingestion/bin/backload_runner.Jenkinsfile'
    ],
    [
        name: 'swf-backload-scheduler-v2-exec',
        description: 'swf exec backload-runner-exec for QA and PROD',
        repo: 'theorchard/swf-feed-ingestion',
        scriptPath: 'feed_ingestion/bin/backload_scheduler.Jenkinsfile'
    ],
    [
        name: 'swf-apple-podcasts-content-performance-exec',
        description: 'Run apple_podcasts_content_performance on QA/Prod.',
        repo: 'theorchard/swf-feed-ingestion',
        scriptPath: 'feed_ingestion/flows/apple_podcasts_content_performance/Jenkinsfile'
    ],
    [
        name: 'swf-apple-podcasts-sales-summary-exec',
        description: 'Run apple_podcasts_sales_summary on QA/Prod.',
        repo: 'theorchard/swf-feed-ingestion',
        scriptPath: 'feed_ingestion/flows/apple_podcasts_sales_summary/Jenkinsfile'
    ],
    [
        name: 'swf-apple-podcasts-sales-summary-monthly-exec',
        description: 'Run apple_podcasts_sales_summary_monthly on QA/Prod.',
        repo: 'theorchard/swf-feed-ingestion',
        scriptPath: 'feed_ingestion/flows/apple_podcasts_sales_summary_monthly/Jenkinsfile'
    ],
    [
        name: 'swf-apple-podcasts-sales-summary-weekly-exec',
        description: 'Run apple_podcasts_sales_summary_weekly on QA/Prod.',
        repo: 'theorchard/swf-feed-ingestion',
        scriptPath: 'feed_ingestion/flows/apple_podcasts_sales_summary_weekly/Jenkinsfile'
    ],
    [
        name: 'swf-apple-podcasts-subscription-events-exec',
        description: 'Run apple_podcasts_subscription_events on QA/Prod.',
        repo: 'theorchard/swf-feed-ingestion',
        scriptPath: 'feed_ingestion/flows/apple_podcasts_subscription_events/Jenkinsfile'
    ],
    [
        name: 'swf-apple-podcasts-subscription-events-monthly-exec',
        description: 'Run apple_podcasts_subscription_events_monthly on QA/Prod.',
        repo: 'theorchard/swf-feed-ingestion',
        scriptPath: 'feed_ingestion/flows/apple_podcasts_subscription_events_monthly/Jenkinsfile'
    ],
    [
        name: 'swf-apple-podcasts-subscription-events-weekly-exec',
        description: 'Run apple_podcasts_subscription_events_weekly on QA/Prod.',
        repo: 'theorchard/swf-feed-ingestion',
        scriptPath: 'feed_ingestion/flows/apple_podcasts_subscription_events_weekly/Jenkinsfile'
    ],
    [
        name: 'swf-apple-podcasts-subscription-snapshot-exec',
        description: 'Run apple_podcasts_subscription_snapshot on QA/Prod.',
        repo: 'theorchard/swf-feed-ingestion',
        scriptPath: 'feed_ingestion/flows/apple_podcasts_subscription_snapshot/Jenkinsfile'
    ],
    [
        name: 'swf-apple-podcasts-subscription-snapshot-monthly-exec',
        description: 'Run apple_podcasts_subscription_snapshot on QA/Prod.',
        repo: 'theorchard/swf-feed-ingestion',
        scriptPath: 'feed_ingestion/flows/apple_podcasts_subscription_snapshot_monthly/Jenkinsfile'
    ],
    [
        name: 'swf-apple-podcasts-subscription-snapshot-weekly-exec',
        description: 'Run apple_podcasts_subscription_snapshot_weekly on QA/Prod.',
        repo: 'theorchard/swf-feed-ingestion',
        scriptPath: 'feed_ingestion/flows/apple_podcasts_subscription_snapshot_weekly/Jenkinsfile'
    ],
    [
        name: 'Run auth0-m2m-config task',
        description: 'Run auth0-m2m-config sync cli commands to update QA/Prod.',
        repo: 'theorchard/auth0-m2m-config',
        scriptPath: 'Sync.groovy'
    ],
    [
        name: 'pdp-qa-refresh',
        description: 'Perform ad-hoc refresh operations to qa-ows-pdp datastore.',
        repo: 'theorchard/pdp-backfill',
        scriptPath: 'pdp_qa_refresh/Jenkinsfile'
    ],
    [
        name: 'pdp-seed-policy-metadata-cache',
        description: 'Re-seed the cerbos_policy_metadata Redis cache for QA/Prod ows-pdp.',
        repo: 'theorchard/ows-pdp',
        scriptPath: 'scheduler/seed_policy_metadata_cache/Jenkinsfile'
    ],
    [
        name: 'pdp-qa-refresh-old',
        description: 'Old pdp-qa-refresh, being deprecated.',
        repo: 'theorchard/ows-pdp',
        scriptPath: 'pdp_qa_refresh/Jenkinsfile'
    ],
    [
        name: 'ows-collaborator-ingest-script',
        description: 'Ingest data from template file into Collaborators database.',
        repo: 'theorchard/ows-collaborator',
        scriptPath: 'scripts/collaborator_ingest.Jenkinsfile'
    ],
    [
        name: 'ows-collaborator-ingest-splits',
        description: 'Ingest collaborator splits from an XLSX file.',
        repo: 'theorchard/ows-collaborator',
        scriptPath: 'scripts/ingest_splits.Jenkinsfile'
    ],
    [
        name: 'ows-collaborator-altafonte-collaborators-migration-script',
        description: 'Migrate collaborators data from ABO databases.',
        repo: 'theorchard/ows-collaborator',
        scriptPath: 'scripts/altafonte_migration/collaborator_migrate.Jenkinsfile'
    ],
    [
        name: 'ows-collaborator-altafonte-splits-migration-script',
        description: 'Migrate splits data from ABO databases.',
        repo: 'theorchard/ows-collaborator',
        scriptPath: 'scripts/altafonte_migration/splits_migrate.Jenkinsfile'
    ],
    [
        name: 'ows-collaborator-ingest-wht-transactions-script',
        description: 'Ingest collabs WHT transactions from Sean.',
        repo: 'theorchard/ows-collaborator',
        scriptPath: 'scripts/ingest_wht_transactions.Jenkinsfile'
    ],
    [
        name: 'swf-sme-analytics-labelcopy-bulk-ingest',
        description: 'SWF exec analytics-labelcopy-bulk-ingest for QA and PROD.',
        repo: 'theorchard/swf-feed-ingestion',
        scriptPath: 'feed_ingestion/flows/sme_analytics_labelcopy_bulk_ingest/Jenkinsfile'
    ],
    [
        name: 'chartmetric-ingestion-pipeline',
        description: 'Run Neo4j ingestion jobs one by one (or in parallel, if combined workload is acceptable) in order to avoid excessive load If it is needed to disable all neo4j jobs you also need to disable https://scheduler.theorchard.io/job/swf-spotify-charts-exec/',
        repo: 'theorchard/swf-feed-ingestion',
        scriptPath: 'feed_ingestion/pipelines/chartmetric-ingestion-pipeline/Jenkinsfile'
    ],
    [
        name: 'dbt-scheduler-analytics-parametrized-pipeline',
        description: 'DBT analytics build pipeline with custom DBT models selector.',
        repo: 'theorchard/dbt-analytics',
        scriptPath: 'jenkins/dbt-scheduler-analytics-parametrized-pipeline/Jenkinsfile'
    ],
    [
        name: 'tadas-etl-exec',
        description: 'Trigger ETL execution of TADAS ETL.',
        repo: 'theorchard/tadas-etl',
        scriptPath: 'jenkins/tadas-etl-exec/Jenkinsfile'
    ],
    [
        name: 'dim-refresh-etl-exec',
        description: 'Trigger dim-refresh-etl execution',
        repo: 'theorchard/dim-refresh-etl',
        scriptPath: 'jenkins/dim-refresh-etl-exec/Jenkinsfile'
    ],
    [
        name: 'ows-abacus-event-create-ingest-sales-event',
        description: "Create an `ingest_sales_{distro|nr}` Abacus event and thus trigger the `sales_ingest` DAG.",
        repo: 'theorchard/ows-abacus-event',
        scriptPath: 'scheduler/create_ingest_sales_event/Jenkinsfile'
    ],
    [
        name: 'invoke-populate-hfa-pending-request-lambda',
        description: "Invoke the populate-hfa-pending-request Lambda function using a custom datetime as the event input.",
        repo: 'theorchard/lambda-publishing-hfa',
        scriptPath: 'scripts/invoke-populate-hfa-pending-request-lambda/Jenkinsfile'
    ],
    [
        name: 'songwhip-api-send-emails-for-incomplete-presaves',
        description: "Send emails about presaves with incomplete data in Songwhip.",
        repo: 'theorchard/songwhip-api',
        scriptPath: 'scheduler/emails_for_incomplete_presaves/Jenkinsfile'
    ],
    [
        name: 'songwhip-api-upgrade-pending-prereleases',
        description: "Upgrade Songwhip prereleases (pre-save pages) to final landing pages",
        repo: 'theorchard/songwhip-api',
        scriptPath: 'scheduler/upgrade_pending_prereleases/Jenkinsfile'
    ],
    [
        name: 'songwhip-api-create-test-prereleases',
        description: "Create Songwhip test prereleases",
        repo: 'theorchard/songwhip-api',
        scriptPath: 'scheduler/create_test_prereleases/Jenkinsfile'
    ],
    [
        name: 'product-store-eligibility-check',
        description: 'Validate Store Delivery Eligibility for given UPCs',
        repo: 'theorchard/scripts-vector',
        scriptPath: 'product-store-eligibility-check/Jenkinsfile',
        branch: 'master'
    ],
    [
        name: 'content-sr-ddex-generator',
        description: 'Generate SRR delivery DDEX for Sound Recordings.',
        repo: 'theorchard/scripts-sound-recording',
        scriptPath: 'ddex-generator/Jenkinsfile',
        branch: 'master'
    ],
    [
        name: 'meta-force-takedown-via-srr',
        description: 'Bulk takedown of ISRCs from Meta via SRR feed.',
        repo: 'theorchard/distro-scripts',
        scriptPath: 'meta-force-takedown-via-srr/Jenkinsfile',
        branch: 'master'
    ],
    [
        name: 'spatial-asset-ingester',
        description: 'Ingest spatial audio.',
        repo: 'theorchard/scripts-assets',
        scriptPath: 'spatial-asset-ingester/Jenkinsfile',
        branch: 'master'
    ],
    [
        name: 'invoke-hive-ai-detection',
        description: 'Invoke Hive AI detection.',
        repo: 'theorchard/scripts-assets',
        scriptPath: 'invoke-hive-ai-detection/Jenkinsfile',
        branch: 'master'
    ],
]

swfFeedIngestionFlows = [
    'altafonte_singlestore',
    'amazon_datapulse',
    'amazon_digital_services',
    'amazon_music',
    'amazon_prime_marketshare',
    'amazon_unlimited_marketshare',
    'apple_financial',
    'apple_id_mapping_sme',
    'apple_music_streams',
    'awa',
    'bandsintown',
    'beatport',
    'chartmetric_charts',
    'chartmetric_participants',
    'chartmetric_playlists_share',
    'chartmetric_socials',
    'chartmetric_socials_backfill',
    'chartmetric_tracks',
    'deezer',
    'deezer_marketshare',
    'delphi_crm',
    'feature_fm_facebook',
    'gfk',
    'gfk_physical',
    'gfk_streaming',
    'insta_reels',
    'itunes',
    'itunes_hides',
    'itunes_marketshare',
    'itunes_tickets',
    'kugou',
    'kuwo',
    'line',
    'meta_daily',
    'netease',
    'pandora',
    'peloton',
    'physical_reporting',
    'physical_warehouse_reports',
    'proper_incoming',
    'qq',
    'rhapsody',
    'seated',
    'setlive',
    'sme_latam',
    'snapchat',
    'soundcloud',
    'spotify',
    'spotify_artificial_streams',
    'spotify_charts',
    'spotify_marketshare',
    'spotify_marketshare_week_ending',
    'spotify_marquee',
    'spotify_track_metrics',
    'ticketek',
    'tiktok',
    'tiktok_fraudulent_streams_report',
    'tiktok_openescrow',
    'tiktok_weekly',
    'uma',
    'uma_marketshare',
    'vevo',
    'youtube_analytics',
    'youtube_asset',
    'youtube_asset_conflict',
    'youtube_bulk_reports',
    'youtube_bulk_reports_facts_asset',
    'youtube_bulk_reports_facts_video',
    'youtube_channel_names',
    'youtube_claim',
    'youtube_facts',
    'youtube_monthly',
    'youtube_neo4j',
    'youtube_red_marketshare',
    'youtube_video',
    'youtube_weekly',
]

swfFeedIngestionFlows.each { flow->
    schedulerJobs << [
        name: 'swf-' + flow.replace('_', '-' ) + '-exec',
        description: "SWF exec for flow <a href=\"https://github.com/theorchard/swf-feed-ingestion/tree/master/feed_ingestion/flows/${flow}\" target=\"_blank\">${flow}</a>",
        repo: 'theorchard/swf-feed-ingestion',
        scriptPath: 'feed_ingestion/flows/' + flow + '/Jenkinsfile'
    ]
}

schedulerJobs.each { job ->
    def jobProperties
    Item existingJob = Jenkins.instance.getItemByFullName(job.name)
    if (existingJob) {
        jobProperties = existingJob.@properties
    }

    pipelineJob(job.name) {
        definition {
            cpsScm {
                lightweight(true)
                scm {
                    gitSCM {
                        browser {}
                        branches {
                            branchSpec {
                                name(job.branch ?: 'master')
                            }
                        }
                        gitTool(null)
                        userRemoteConfigs {
                            userRemoteConfig {
                                name('')
                                credentialsId('7c29b204-9e5b-45e0-bd0a-6535917c604f')
                                refspec('')
                                url("git@github.com:${job.repo}.git")
                            }
                        }
                    }
                }
                scriptPath(job.scriptPath)
            }
        }
        description("${job.description}<br><br>NOTE: The configuration for this job is managed by <a href=\"${GIT_URL}\" target=\"_blank\">${GIT_URL}</a>")

        // Grim workaround for preserving properties configured in the Jenkinsfile.
        // See https://issues.jenkins.io/browse/JENKINS-43758?focusedId=408718&page=com.atlassian.jira.plugin.system.issuetabpanels%3Acomment-tabpanel#comment-408718
        if (jobProperties) {
            configure { root ->
                def properties = root / 'properties'
                jobProperties.each { property ->
                    String xml = Items.XSTREAM2.toXML(property)
                    def jobPropertiesPropertyNode = new XmlParser().parseText(xml)
                    properties << jobPropertiesPropertyNode
                }
            }
        }
    }
}
