ALTER TABLE worker_wallets
  ADD COLUMN pending_unfreeze_amount INTEGER NOT NULL DEFAULT 0;

CREATE TABLE IF NOT EXISTS worker_deposit_unfreezes (
  id BIGSERIAL PRIMARY KEY,
  worker_id BIGINT NOT NULL REFERENCES worker_users(id) ON DELETE CASCADE,
  work_order_id BIGINT NOT NULL REFERENCES work_orders(id) ON DELETE CASCADE,
  amount INTEGER NOT NULL,
  unfreeze_at TIMESTAMPTZ NOT NULL,
  status TEXT NOT NULL DEFAULT 'pending',
  released_at TIMESTAMPTZ,
  created_at TIMESTAMPTZ NOT NULL,
  updated_at TIMESTAMPTZ NOT NULL
);

CREATE INDEX idx_worker_deposit_unfreezes_scan
  ON worker_deposit_unfreezes(status, unfreeze_at);
CREATE INDEX idx_worker_deposit_unfreezes_worker
  ON worker_deposit_unfreezes(worker_id, status);

COMMENT ON COLUMN worker_wallets.pending_unfreeze_amount IS '待解冻押金：验收通过后 3 天到账';
COMMENT ON TABLE worker_deposit_unfreezes IS '押金解冻计划：验收通过后进入待解冻，到期自动转入可用余额';
