CREATE TABLE billing_error (
    id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
    scope VARCHAR(20) NOT NULL,
    scope_id UUID NOT NULL,
    error_type VARCHAR(50) NOT NULL,
    message TEXT NOT NULL,
    details JSONB,
    resolved BOOLEAN DEFAULT FALSE NOT NULL,
    created_at TIMESTAMPTZ DEFAULT NOW()
);

CREATE INDEX idx_billing_error_scope ON billing_error (scope, scope_id);
CREATE INDEX idx_billing_error_resolved ON billing_error (resolved) WHERE resolved = FALSE;