-- BLOC 2: Deferred validation (10 min) for sales. Run after 001_sale_listings.sql.
-- Adds validated_at: when accept is called, sold_at = now(), validated_at = now() + 10 minutes.
-- Coins are transferred only when validated_at <= now() (processed by processValidatedSales).
-- Buyer can deliver only after status = 'validated'.

ALTER TABLE sale_listings ADD COLUMN IF NOT EXISTS validated_at TIMESTAMPTZ;

ALTER TABLE sale_listings DROP CONSTRAINT IF EXISTS sale_listings_status_check;
ALTER TABLE sale_listings ADD CONSTRAINT sale_listings_status_check
  CHECK (status IN ('active', 'sold', 'expired', 'rejected', 'validated'));

COMMENT ON COLUMN sale_listings.validated_at IS 'When the sale becomes validated (coins transfer allowed). Set at accept to now() + 10 minutes; after processValidatedSales runs, status becomes validated.';
