BEGIN;

ALTER TABLE public.hss_service_orders
  ADD COLUMN IF NOT EXISTS payment_status SMALLINT NOT NULL DEFAULT 1;

ALTER TABLE public.hss_service_orders
  ADD COLUMN IF NOT EXISTS pay_expire_at TIMESTAMPTZ;

ALTER TABLE public.hss_service_orders
  ADD COLUMN IF NOT EXISTS cancel_reason TEXT NOT NULL DEFAULT '';

ALTER TABLE public.hss_service_orders
  ADD COLUMN IF NOT EXISTS cancelled_at TIMESTAMPTZ;

ALTER TABLE public.hss_service_orders
  ADD COLUMN IF NOT EXISTS consumer_deleted_at TIMESTAMPTZ;

CREATE INDEX IF NOT EXISTS idx_hss_service_orders_user_visible
ON public.hss_service_orders(user_id, created_at DESC)
WHERE consumer_deleted_at IS NULL;

UPDATE public.hss_service_orders
SET pay_expire_at = created_at + INTERVAL '10 minutes'
WHERE pay_expire_at IS NULL
  AND status = 'created'
  AND payment_status = 1;

ALTER TABLE public.ml_orders
  ADD COLUMN IF NOT EXISTS pay_expire_at TIMESTAMPTZ,
  ADD COLUMN IF NOT EXISTS payment_status SMALLINT NOT NULL DEFAULT 1,
  ADD COLUMN IF NOT EXISTS cancel_reason TEXT NOT NULL DEFAULT '',
  ADD COLUMN IF NOT EXISTS cancelled_at TIMESTAMPTZ,
  ADD COLUMN IF NOT EXISTS consumer_deleted_at TIMESTAMPTZ;

COMMIT;
