-- Migration: 043_membership_proposed_changes.sql
-- Date: 2026-02-01
-- Description: Add workflow for admin-proposed changes requiring user approval

-- Add new columns for proposed changes workflow
ALTER TABLE membership_applications
ADD COLUMN IF NOT EXISTS proposed_changes JSONB,
ADD COLUMN IF NOT EXISTS proposed_changes_at TIMESTAMP,
ADD COLUMN IF NOT EXISTS proposed_changes_by_id INTEGER REFERENCES users(id),
ADD COLUMN IF NOT EXISTS proposed_changes_comment TEXT;

-- Add index for efficient querying by status
CREATE INDEX IF NOT EXISTS idx_membership_proposed_status
ON membership_applications(status)
WHERE status = 'pending_user_approval';

-- Grant permissions
GRANT ALL ON TABLE membership_applications TO nordabiz_app;

COMMENT ON COLUMN membership_applications.proposed_changes IS 'JSONB containing proposed field changes from registry data';
COMMENT ON COLUMN membership_applications.proposed_changes_at IS 'When the changes were proposed by admin';
COMMENT ON COLUMN membership_applications.proposed_changes_by_id IS 'Admin user who proposed the changes';
COMMENT ON COLUMN membership_applications.proposed_changes_comment IS 'Optional comment from admin explaining the changes';
