-- Find DataHub Weekly Refresh job
SELECT 'JOB DATAHUB:' as info;
SELECT id, name, schedule_type, schedule_config FROM jobs WHERE name LIKE '%DataHub%Weekly%';

SELECT '';
SELECT 'SCHEDULES DIRETTI:' as info;
SELECT s.id, s.name, s.schedule_type, s.schedule_config, s.enabled, s.target_type, s.target_id
FROM schedules s
WHERE s.target_type = 'job'
  AND s.target_id IN (SELECT id FROM jobs WHERE name LIKE '%DataHub%Weekly%');

SELECT '';
SELECT 'GRUPPI CON DATAHUB:' as info;
SELECT jg.id, jg.name, jgm.job_id, jgm.execution_order
FROM job_groups jg
JOIN job_group_members jgm ON jg.id = jgm.group_id
WHERE jgm.job_id IN (SELECT id FROM jobs WHERE name LIKE '%DataHub%Weekly%');

SELECT '';
SELECT 'SCHEDULES PER GRUPPI DATAHUB:' as info;
SELECT s.id, s.name, s.schedule_type, s.schedule_config, s.enabled
FROM schedules s
WHERE s.target_type = 'group'
  AND s.target_id IN (
    SELECT DISTINCT jg.id
    FROM job_groups jg
    JOIN job_group_members jgm ON jg.id = jgm.group_id
    WHERE jgm.job_id IN (SELECT id FROM jobs WHERE name LIKE '%DataHub%Weekly%')
  );
