CREATE TABLE IF NOT EXISTS catalog_sync_jobs (
  id VARCHAR(36) NOT NULL,
  provider VARCHAR(64) NOT NULL,
  trigger_type VARCHAR(32) NOT NULL,
  status VARCHAR(32) NOT NULL,
  started_at DATETIME(6) NOT NULL,
  finished_at DATETIME(6) NULL,
  category_total INT NOT NULL DEFAULT 0,
  product_total INT NOT NULL DEFAULT 0,
  category_created INT NOT NULL DEFAULT 0,
  category_updated INT NOT NULL DEFAULT 0,
  product_created INT NOT NULL DEFAULT 0,
  product_updated INT NOT NULL DEFAULT 0,
  product_removed INT NOT NULL DEFAULT 0,
  error_message TEXT NULL,
  created_at DATETIME(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6),
  updated_at DATETIME(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6),
  PRIMARY KEY (id),
  INDEX idx_catalog_sync_jobs_provider_started (provider, started_at),
  INDEX idx_catalog_sync_jobs_status_started (status, started_at)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

SET @courses_sync_status_exists := (
  SELECT COUNT(*) FROM information_schema.columns
  WHERE table_schema = DATABASE() AND table_name = 'courses' AND column_name = 'sync_status'
);
SET @courses_sync_status_sql := IF(
  @courses_sync_status_exists = 0,
  'ALTER TABLE courses ADD COLUMN sync_status VARCHAR(32) NOT NULL DEFAULT ''active'' AFTER enabled',
  'SELECT 1'
);
PREPARE courses_sync_status_stmt FROM @courses_sync_status_sql;
EXECUTE courses_sync_status_stmt;
DEALLOCATE PREPARE courses_sync_status_stmt;

SET @courses_last_seen_at_exists := (
  SELECT COUNT(*) FROM information_schema.columns
  WHERE table_schema = DATABASE() AND table_name = 'courses' AND column_name = 'last_seen_at'
);
SET @courses_last_seen_at_sql := IF(
  @courses_last_seen_at_exists = 0,
  'ALTER TABLE courses ADD COLUMN last_seen_at DATETIME NULL AFTER last_synced_at',
  'SELECT 1'
);
PREPARE courses_last_seen_at_stmt FROM @courses_last_seen_at_sql;
EXECUTE courses_last_seen_at_stmt;
DEALLOCATE PREPARE courses_last_seen_at_stmt;

SET @courses_last_seen_sync_job_id_exists := (
  SELECT COUNT(*) FROM information_schema.columns
  WHERE table_schema = DATABASE() AND table_name = 'courses' AND column_name = 'last_seen_sync_job_id'
);
SET @courses_last_seen_sync_job_id_sql := IF(
  @courses_last_seen_sync_job_id_exists = 0,
  'ALTER TABLE courses ADD COLUMN last_seen_sync_job_id VARCHAR(36) NULL AFTER last_seen_at',
  'SELECT 1'
);
PREPARE courses_last_seen_sync_job_id_stmt FROM @courses_last_seen_sync_job_id_sql;
EXECUTE courses_last_seen_sync_job_id_stmt;
DEALLOCATE PREPARE courses_last_seen_sync_job_id_stmt;

SET @courses_removed_at_exists := (
  SELECT COUNT(*) FROM information_schema.columns
  WHERE table_schema = DATABASE() AND table_name = 'courses' AND column_name = 'removed_at'
);
SET @courses_removed_at_sql := IF(
  @courses_removed_at_exists = 0,
  'ALTER TABLE courses ADD COLUMN removed_at DATETIME NULL AFTER last_seen_sync_job_id',
  'SELECT 1'
);
PREPARE courses_removed_at_stmt FROM @courses_removed_at_sql;
EXECUTE courses_removed_at_stmt;
DEALLOCATE PREPARE courses_removed_at_stmt;

UPDATE courses
SET sync_status = 'active',
    last_seen_at = COALESCE(last_seen_at, last_synced_at)
WHERE source_type = 'third_party'
  AND (sync_status IS NULL OR sync_status = '');

SET @courses_sync_status_index_exists := (
  SELECT COUNT(*) FROM information_schema.statistics
  WHERE table_schema = DATABASE()
    AND table_name = 'courses'
    AND index_name = 'idx_courses_sync_status_seen'
);
SET @courses_sync_status_index_sql := IF(
  @courses_sync_status_index_exists = 0,
  'CREATE INDEX idx_courses_sync_status_seen ON courses (source_type, provider, sync_status, last_seen_at)',
  'SELECT 1'
);
PREPARE courses_sync_status_index_stmt FROM @courses_sync_status_index_sql;
EXECUTE courses_sync_status_index_stmt;
DEALLOCATE PREPARE courses_sync_status_index_stmt;
