-- 006_worker_profile_finance_requests.sql —— 接单端个人中心资金申请能力。
--
-- 说明：
-- 1. 充值 / 提现先走申请单，由后台后续审核处理；
-- 2. 不直接改动现有钱包结算逻辑，避免影响抢单、验收、押金释放链路。

CREATE TABLE IF NOT EXISTS worker_finance_requests (
  id BIGSERIAL PRIMARY KEY,
  worker_id BIGINT NOT NULL REFERENCES worker_users(id) ON DELETE CASCADE,
  request_type TEXT NOT NULL,
  status TEXT NOT NULL DEFAULT 'pending',
  amount INTEGER NOT NULL DEFAULT 0,
  account_channel TEXT NOT NULL DEFAULT '',
  account_name TEXT NOT NULL DEFAULT '',
  account_no TEXT NOT NULL DEFAULT '',
  note TEXT NOT NULL DEFAULT '',
  reviewed_note TEXT NOT NULL DEFAULT '',
  payload_json JSONB NOT NULL DEFAULT '{}'::jsonb,
  created_at TIMESTAMPTZ NOT NULL,
  updated_at TIMESTAMPTZ NOT NULL,
  reviewed_at TIMESTAMPTZ
);

CREATE INDEX IF NOT EXISTS idx_worker_finance_requests_worker_created
  ON worker_finance_requests(worker_id, created_at DESC);

CREATE INDEX IF NOT EXISTS idx_worker_finance_requests_status_type
  ON worker_finance_requests(status, request_type, id DESC);

COMMENT ON TABLE worker_finance_requests IS '打手充值 / 提现申请记录';
COMMENT ON COLUMN worker_finance_requests.request_type IS '申请类型：recharge / withdraw';
COMMENT ON COLUMN worker_finance_requests.status IS '申请状态：pending / approved / rejected / cancelled';
COMMENT ON COLUMN worker_finance_requests.amount IS '申请金额，单位分';
COMMENT ON COLUMN worker_finance_requests.payload_json IS '扩展字段，便于后续接支付凭证、审核信息';
