-- 打手问题反馈：关联工单，由客服回复并关闭，不改变工单状态或资金。

CREATE TABLE IF NOT EXISTS worker_order_feedbacks (
  id BIGSERIAL PRIMARY KEY,
  work_order_id BIGINT NOT NULL REFERENCES work_orders(id) ON DELETE CASCADE,
  work_order_share_id BIGINT REFERENCES work_order_shares(id) ON DELETE SET NULL,
  worker_id BIGINT NOT NULL REFERENCES worker_users(id) ON DELETE CASCADE,
  content TEXT NOT NULL DEFAULT '',
  status TEXT NOT NULL DEFAULT 'pending',
  reply TEXT NOT NULL DEFAULT '',
  handled_by TEXT NOT NULL DEFAULT '',
  created_at TIMESTAMPTZ NOT NULL,
  handled_at TIMESTAMPTZ,
  updated_at TIMESTAMPTZ NOT NULL
);

CREATE INDEX IF NOT EXISTS idx_worker_order_feedbacks_status_created
  ON worker_order_feedbacks (status, created_at DESC);

CREATE INDEX IF NOT EXISTS idx_worker_order_feedbacks_worker_created
  ON worker_order_feedbacks (worker_id, created_at DESC);

COMMENT ON TABLE worker_order_feedbacks IS '打手针对工单提交的客户资料和处理问题反馈';
