-- Rate cycles: per-instructor rates on schedule_slot, generic billing terms on enrollment,
-- replace base_rate_monthly on lesson_type with weekly/monthly/quarterly presets

CREATE TYPE billing_unit AS ENUM ('day', 'week', 'month', 'quarter', 'year');

-- lesson_type: replace base_rate_monthly varchar with three numeric rate columns
ALTER TABLE lesson_type ADD COLUMN rate_weekly    numeric(10,2);
ALTER TABLE lesson_type ADD COLUMN rate_monthly   numeric(10,2);
ALTER TABLE lesson_type ADD COLUMN rate_quarterly numeric(10,2);
UPDATE lesson_type
  SET rate_monthly = base_rate_monthly::numeric
  WHERE base_rate_monthly IS NOT NULL
    AND base_rate_monthly ~ '^\d+(\.\d+)?$';
ALTER TABLE lesson_type DROP COLUMN base_rate_monthly;

-- schedule_slot: add per-instructor preset rates
ALTER TABLE schedule_slot ADD COLUMN rate_weekly    numeric(10,2);
ALTER TABLE schedule_slot ADD COLUMN rate_monthly   numeric(10,2);
ALTER TABLE schedule_slot ADD COLUMN rate_quarterly numeric(10,2);

-- enrollment: replace monthly_rate with generic billing terms
ALTER TABLE enrollment ADD COLUMN rate             numeric(10,2);
ALTER TABLE enrollment ADD COLUMN billing_interval integer;
ALTER TABLE enrollment ADD COLUMN billing_unit     billing_unit;
UPDATE enrollment
  SET rate = monthly_rate, billing_interval = 1, billing_unit = 'month'
  WHERE monthly_rate IS NOT NULL;
ALTER TABLE enrollment DROP COLUMN monthly_rate;
