-- 验收后的售后追责单：不改变已验收工单的生命周期和历史结算。

CREATE TABLE IF NOT EXISTS worker_after_sales_cases (
  id BIGSERIAL PRIMARY KEY,
  case_no TEXT NOT NULL UNIQUE,
  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 RESTRICT,
  problem_type TEXT NOT NULL DEFAULT 'fake_evidence',
  complaint_note TEXT NOT NULL DEFAULT '',
  complaint_files_json JSONB NOT NULL DEFAULT '[]'::jsonb,
  acceptance_snapshot_json JSONB NOT NULL DEFAULT '{}'::jsonb,
  status TEXT NOT NULL DEFAULT 'awaiting_worker_response',
  worker_response TEXT NOT NULL DEFAULT '',
  worker_response_files_json JSONB NOT NULL DEFAULT '[]'::jsonb,
  responded_at TIMESTAMPTZ,
  resolution_action TEXT NOT NULL DEFAULT '',
  resolution_note TEXT NOT NULL DEFAULT '',
  recovery_amount INTEGER NOT NULL DEFAULT 0,
  pending_deposit_deduction_amount INTEGER NOT NULL DEFAULT 0,
  available_deduction_amount INTEGER NOT NULL DEFAULT 0,
  debt_amount INTEGER NOT NULL DEFAULT 0,
  created_by TEXT NOT NULL DEFAULT '',
  resolved_by TEXT NOT NULL DEFAULT '',
  resolved_at TIMESTAMPTZ,
  created_at TIMESTAMPTZ NOT NULL,
  updated_at TIMESTAMPTZ NOT NULL
);

CREATE UNIQUE INDEX IF NOT EXISTS idx_worker_after_sales_cases_active_target
  ON worker_after_sales_cases(work_order_id, worker_id, COALESCE(work_order_share_id, 0))
  WHERE status IN ('awaiting_worker_response', 'under_review', 'recovery_pending');

CREATE INDEX IF NOT EXISTS idx_worker_after_sales_cases_status_created
  ON worker_after_sales_cases(status, created_at DESC);

CREATE INDEX IF NOT EXISTS idx_worker_after_sales_cases_worker_created
  ON worker_after_sales_cases(worker_id, created_at DESC);

CREATE TABLE IF NOT EXISTS worker_after_sales_debts (
  id BIGSERIAL PRIMARY KEY,
  case_id BIGINT NOT NULL UNIQUE REFERENCES worker_after_sales_cases(id) ON DELETE CASCADE,
  worker_id BIGINT NOT NULL REFERENCES worker_users(id) ON DELETE RESTRICT,
  original_amount INTEGER NOT NULL,
  outstanding_amount INTEGER NOT NULL,
  status TEXT NOT NULL DEFAULT 'open',
  created_at TIMESTAMPTZ NOT NULL,
  settled_at TIMESTAMPTZ,
  updated_at TIMESTAMPTZ NOT NULL
);

CREATE INDEX IF NOT EXISTS idx_worker_after_sales_debts_worker_open
  ON worker_after_sales_debts(worker_id, status, created_at ASC);

CREATE TABLE IF NOT EXISTS worker_after_sales_case_events (
  id BIGSERIAL PRIMARY KEY,
  case_id BIGINT NOT NULL REFERENCES worker_after_sales_cases(id) ON DELETE CASCADE,
  actor_type TEXT NOT NULL,
  actor_id TEXT NOT NULL DEFAULT '',
  event_type TEXT NOT NULL,
  payload_json JSONB NOT NULL DEFAULT '{}'::jsonb,
  created_at TIMESTAMPTZ NOT NULL
);

CREATE INDEX IF NOT EXISTS idx_worker_after_sales_case_events_case_created
  ON worker_after_sales_case_events(case_id, created_at ASC, id ASC);

COMMENT ON TABLE worker_after_sales_cases IS '已验收工单的独立售后追责单，不回退原工单状态';
COMMENT ON COLUMN worker_after_sales_cases.acceptance_snapshot_json IS '创建售后单时固化的验收证据快照';
COMMENT ON TABLE worker_after_sales_debts IS '售后扣款不足时形成的打手追缴欠款';
COMMENT ON TABLE worker_after_sales_case_events IS '售后问题单不可篡改的操作记录';
