SET @courses_cover_url_exists := (
  SELECT COUNT(*) FROM information_schema.columns
  WHERE table_schema = DATABASE() AND table_name = 'courses' AND column_name = 'cover_url'
);
SET @courses_cover_url_sql := IF(
  @courses_cover_url_exists = 0,
  'ALTER TABLE courses ADD COLUMN cover_url VARCHAR(500) NULL AFTER price',
  'SELECT 1'
);
PREPARE courses_cover_url_stmt FROM @courses_cover_url_sql;
EXECUTE courses_cover_url_stmt;
DEALLOCATE PREPARE courses_cover_url_stmt;

SET @courses_stock_exists := (
  SELECT COUNT(*) FROM information_schema.columns
  WHERE table_schema = DATABASE() AND table_name = 'courses' AND column_name = 'stock'
);
SET @courses_stock_sql := IF(
  @courses_stock_exists = 0,
  'ALTER TABLE courses ADD COLUMN stock INT NULL AFTER content',
  'SELECT 1'
);
PREPARE courses_stock_stmt FROM @courses_stock_sql;
EXECUTE courses_stock_stmt;
DEALLOCATE PREPARE courses_stock_stmt;

SET @courses_sales_limit_exists := (
  SELECT COUNT(*) FROM information_schema.columns
  WHERE table_schema = DATABASE() AND table_name = 'courses' AND column_name = 'sales_limit'
);
SET @courses_sales_limit_sql := IF(
  @courses_sales_limit_exists = 0,
  'ALTER TABLE courses ADD COLUMN sales_limit INT NULL AFTER stock',
  'SELECT 1'
);
PREPARE courses_sales_limit_stmt FROM @courses_sales_limit_sql;
EXECUTE courses_sales_limit_stmt;
DEALLOCATE PREPARE courses_sales_limit_stmt;

SET @courses_fulfillment_type_exists := (
  SELECT COUNT(*) FROM information_schema.columns
  WHERE table_schema = DATABASE() AND table_name = 'courses' AND column_name = 'fulfillment_type'
);
SET @courses_fulfillment_type_sql := IF(
  @courses_fulfillment_type_exists = 0,
  'ALTER TABLE courses ADD COLUMN fulfillment_type VARCHAR(40) NOT NULL DEFAULT ''third_party_api'' AFTER sales_limit',
  'SELECT 1'
);
PREPARE courses_fulfillment_type_stmt FROM @courses_fulfillment_type_sql;
EXECUTE courses_fulfillment_type_stmt;
DEALLOCATE PREPARE courses_fulfillment_type_stmt;

SET @courses_after_sales_note_exists := (
  SELECT COUNT(*) FROM information_schema.columns
  WHERE table_schema = DATABASE() AND table_name = 'courses' AND column_name = 'after_sales_note'
);
SET @courses_after_sales_note_sql := IF(
  @courses_after_sales_note_exists = 0,
  'ALTER TABLE courses ADD COLUMN after_sales_note TEXT NULL AFTER fulfillment_type',
  'SELECT 1'
);
PREPARE courses_after_sales_note_stmt FROM @courses_after_sales_note_sql;
EXECUTE courses_after_sales_note_stmt;
DEALLOCATE PREPARE courses_after_sales_note_stmt;

SET @courses_sort_order_exists := (
  SELECT COUNT(*) FROM information_schema.columns
  WHERE table_schema = DATABASE() AND table_name = 'courses' AND column_name = 'sort_order'
);
SET @courses_sort_order_sql := IF(
  @courses_sort_order_exists = 0,
  'ALTER TABLE courses ADD COLUMN sort_order INT NOT NULL DEFAULT 0 AFTER after_sales_note',
  'SELECT 1'
);
PREPARE courses_sort_order_stmt FROM @courses_sort_order_sql;
EXECUTE courses_sort_order_stmt;
DEALLOCATE PREPARE courses_sort_order_stmt;

UPDATE courses
SET fulfillment_type = CASE
  WHEN source_type = 'self_owned' THEN 'local_only'
  ELSE 'third_party_api'
END
WHERE fulfillment_type IS NULL OR fulfillment_type = '';

SET @categories_source_enabled_index_exists := (
  SELECT COUNT(*) FROM information_schema.statistics
  WHERE table_schema = DATABASE()
    AND table_name = 'categories'
    AND index_name = 'idx_categories_source_enabled_sort'
);
SET @categories_source_enabled_index_sql := IF(
  @categories_source_enabled_index_exists = 0,
  'CREATE INDEX idx_categories_source_enabled_sort ON categories (source_type, enabled, sort_order)',
  'SELECT 1'
);
PREPARE categories_source_enabled_index_stmt FROM @categories_source_enabled_index_sql;
EXECUTE categories_source_enabled_index_stmt;
DEALLOCATE PREPARE categories_source_enabled_index_stmt;

SET @courses_source_enabled_index_exists := (
  SELECT COUNT(*) FROM information_schema.statistics
  WHERE table_schema = DATABASE()
    AND table_name = 'courses'
    AND index_name = 'idx_courses_source_enabled_sort'
);
SET @courses_source_enabled_index_sql := IF(
  @courses_source_enabled_index_exists = 0,
  'CREATE INDEX idx_courses_source_enabled_sort ON courses (source_type, enabled, sort_order, updated_at)',
  'SELECT 1'
);
PREPARE courses_source_enabled_index_stmt FROM @courses_source_enabled_index_sql;
EXECUTE courses_source_enabled_index_stmt;
DEALLOCATE PREPARE courses_source_enabled_index_stmt;
