-- Add points column to customers table if it doesn't exist
SET @dbname = DATABASE();
SET @tablename = "customers";
SET @columnname = "points";
SET @preparedStatement = (SELECT IF(
  (
    SELECT COUNT(*) FROM INFORMATION_SCHEMA.COLUMNS
    WHERE
      (table_name = @tablename)
      AND (table_schema = @dbname)
      AND (column_name = @columnname)
  ) > 0,
  "SELECT 1",
  "ALTER TABLE customers ADD COLUMN points INT DEFAULT 0;"
));
PREPARE alterIfNotExists FROM @preparedStatement;
EXECUTE alterIfNotExists;
DEALLOCATE PREPARE alterIfNotExists;

-- Create loyalty_settings table
CREATE TABLE IF NOT EXISTS loyalty_settings (
    id INT PRIMARY KEY,
    points_per_order INT DEFAULT 10,
    points_for_free_meal INT DEFAULT 70
);

-- Seed default settings
INSERT IGNORE INTO loyalty_settings (id, points_per_order, points_for_free_meal) VALUES (1, 10, 70);
