-- Добавить назначение "мероприятие" и привязку к pr_events
-- 1. Расширить CHECK purpose_type: добавить 'event'
ALTER TABLE payment_invoices DROP CONSTRAINT IF EXISTS payment_invoices_purpose_type_check;
ALTER TABLE payment_invoices ADD CONSTRAINT payment_invoices_purpose_type_check
  CHECK (purpose_type IN ('building', 'district', 'legal', 'office', 'hr', 'other', 'event'));

-- 2. Колонка привязки счёта к мероприятию
ALTER TABLE payment_invoices ADD COLUMN IF NOT EXISTS purpose_event_id BIGINT REFERENCES pr_events(id) ON DELETE SET NULL;
CREATE INDEX IF NOT EXISTS idx_payment_invoices_purpose_event ON payment_invoices(purpose_event_id) WHERE purpose_event_id IS NOT NULL;

COMMENT ON COLUMN payment_invoices.purpose_event_id IS 'ID мероприятия (pr_events), если purpose_type = event';
