-- ============================================================================
-- 积分充值申请与低余额告警配置
-- 充值需上传打款凭证，由平台管理员审核通过后自动注入积分
-- ============================================================================

CREATE TABLE IF NOT EXISTS recharge_applications (
	id BIGSERIAL PRIMARY KEY,
	created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
	updated_at TIMESTAMPTZ NOT NULL DEFAULT now(),
	deleted_at TIMESTAMPTZ,
	merchant_id BIGINT NOT NULL,
	application_no VARCHAR(64) NOT NULL UNIQUE,
	amount_cny BIGINT NOT NULL,
	points_amount BIGINT NOT NULL,
	status VARCHAR(16) NOT NULL DEFAULT 'pending',
	vouchers TEXT NOT NULL DEFAULT '[]',
	note VARCHAR(512) NOT NULL DEFAULT '',
	review_note VARCHAR(512) NOT NULL DEFAULT '',
	reviewed_at TIMESTAMPTZ,
	reviewed_by BIGINT
);

CREATE INDEX IF NOT EXISTS idx_recharge_applications_merchant ON recharge_applications (merchant_id);
CREATE INDEX IF NOT EXISTS idx_recharge_applications_status ON recharge_applications (status);

CREATE TABLE IF NOT EXISTS merchant_alert_configs (
	id BIGSERIAL PRIMARY KEY,
	created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
	updated_at TIMESTAMPTZ NOT NULL DEFAULT now(),
	merchant_id BIGINT NOT NULL UNIQUE,
	enabled BOOLEAN NOT NULL DEFAULT false,
	threshold_points BIGINT NOT NULL DEFAULT 0,
	webhook_url VARCHAR(1024) NOT NULL DEFAULT ''
);
