-- Migration: 046_workflow_history.sql
-- Date: 2026-02-01
-- Description: Add workflow_history field to track all status changes with details

-- Add workflow_history column (JSONB array of events)
ALTER TABLE membership_applications
ADD COLUMN IF NOT EXISTS workflow_history JSONB DEFAULT '[]'::jsonb;

-- Add user_changes_accepted_at for tracking when user accepted/rejected proposed changes
ALTER TABLE membership_applications
ADD COLUMN IF NOT EXISTS user_changes_accepted_at TIMESTAMP;

-- Add user_changes_action to track what user did (accepted/rejected)
ALTER TABLE membership_applications
ADD COLUMN IF NOT EXISTS user_changes_action VARCHAR(20);

COMMENT ON COLUMN membership_applications.workflow_history IS 'Historia zmian statusu: [{event, timestamp, user_id, user_name, details}]';
COMMENT ON COLUMN membership_applications.user_changes_accepted_at IS 'Kiedy użytkownik zaakceptował/odrzucił propozycje zmian';
COMMENT ON COLUMN membership_applications.user_changes_action IS 'Co użytkownik zrobił: accepted/rejected';

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