-- +goose Up
-- +goose StatementBegin
ALTER TABLE orders
    ADD COLUMN IF NOT EXISTS is_flagged BOOLEAN NOT NULL DEFAULT false,
    ADD COLUMN IF NOT EXISTS flag_reason VARCHAR(255) NOT NULL DEFAULT '',
    ADD COLUMN IF NOT EXISTS flagged_by BIGINT NOT NULL DEFAULT 0,
    ADD COLUMN IF NOT EXISTS flagged_at TIMESTAMPTZ,
    ADD COLUMN IF NOT EXISTS is_reconciled BOOLEAN NOT NULL DEFAULT false,
    ADD COLUMN IF NOT EXISTS reconcile_note VARCHAR(255) NOT NULL DEFAULT '',
    ADD COLUMN IF NOT EXISTS reconciled_by BIGINT NOT NULL DEFAULT 0,
    ADD COLUMN IF NOT EXISTS reconciled_at TIMESTAMPTZ;

COMMENT ON COLUMN orders.is_flagged IS '问题订单标记；用途：运营标注需复核订单；默认 false。';
COMMENT ON COLUMN orders.flag_reason IS '问题标记原因；用途：说明问题点与风险；默认空字符串。';
COMMENT ON COLUMN orders.flagged_by IS '问题标记操作者ID；用途：审计追踪；默认 0 表示未标记。';
COMMENT ON COLUMN orders.flagged_at IS '问题标记时间；用途：记录标记时效；未标记为空。';
COMMENT ON COLUMN orders.is_reconciled IS '对账状态；用途：标识是否完成人工对账；默认 false。';
COMMENT ON COLUMN orders.reconcile_note IS '对账说明；用途：记录对账备注与结论；默认空字符串。';
COMMENT ON COLUMN orders.reconciled_by IS '对账操作者ID；用途：审计追踪；默认 0 表示未对账。';
COMMENT ON COLUMN orders.reconciled_at IS '对账时间；用途：记录完成对账时间；未对账为空。';

CREATE INDEX IF NOT EXISTS orders_is_flagged_idx ON orders(is_flagged);
CREATE INDEX IF NOT EXISTS orders_is_reconciled_idx ON orders(is_reconciled);
CREATE INDEX IF NOT EXISTS orders_flagged_at_idx ON orders(flagged_at);
CREATE INDEX IF NOT EXISTS orders_reconciled_at_idx ON orders(reconciled_at);
-- +goose StatementEnd

-- +goose Down
-- +goose StatementBegin
ALTER TABLE orders
    DROP COLUMN IF EXISTS reconciled_at,
    DROP COLUMN IF EXISTS reconciled_by,
    DROP COLUMN IF EXISTS reconcile_note,
    DROP COLUMN IF EXISTS is_reconciled,
    DROP COLUMN IF EXISTS flagged_at,
    DROP COLUMN IF EXISTS flagged_by,
    DROP COLUMN IF EXISTS flag_reason,
    DROP COLUMN IF EXISTS is_flagged;
-- +goose StatementEnd
