-- apps/api/migrations/sprint51_approval_multisig.sql
-- Sprint 5.1 M-002: MultiSig 雙簽核支援
-- 執行者: Claude Sonnet 4.6 / 2026-04-08 Asia/Taipei
-- 說明: approval_records 新增 approval_level / approval_votes / required_votes

BEGIN;

ALTER TABLE approval_records
  ADD COLUMN IF NOT EXISTS approval_level VARCHAR(20)
    DEFAULT 'standard'
    CHECK (approval_level IN ('standard', 'critical')),
  ADD COLUMN IF NOT EXISTS approval_votes JSONB
    DEFAULT '[]'::jsonb,
  ADD COLUMN IF NOT EXISTS required_votes INTEGER
    DEFAULT 1;

COMMENT ON COLUMN approval_records.approval_level IS
  'standard=1票審核, critical=2票MultiSig';
COMMENT ON COLUMN approval_records.approval_votes IS
  'JSON array: [{"user_id": "123", "voted_at": "2026-04-08T...", "action": "approve"}]';
COMMENT ON COLUMN approval_records.required_votes IS
  'standard=1, critical=2';

-- 現有記錄回填（向後相容）
UPDATE approval_records
SET approval_level = 'standard',
    required_votes = 1,
    approval_votes = '[]'::jsonb
WHERE approval_level IS NULL;

COMMIT;
