-- 004_worker_order_platform.sql —— 接单平台一期结构。
--
-- 说明：接单平台状态独立于 fulfillment_tasks.task_status，避免影响现有自动履约状态机。

CREATE TABLE IF NOT EXISTS worker_levels (
  id BIGSERIAL PRIMARY KEY,
  level_key TEXT NOT NULL UNIQUE,
  name TEXT NOT NULL,
  sort_order INTEGER NOT NULL DEFAULT 100,
  status TEXT NOT NULL DEFAULT 'active',
  permission_json JSONB NOT NULL DEFAULT '{}'::jsonb,
  created_at TIMESTAMPTZ NOT NULL,
  updated_at TIMESTAMPTZ NOT NULL
);

CREATE TABLE IF NOT EXISTS worker_users (
  id BIGSERIAL PRIMARY KEY,
  username TEXT NOT NULL UNIQUE,
  password_hash TEXT NOT NULL,
  display_name TEXT NOT NULL DEFAULT '',
  phone TEXT NOT NULL DEFAULT '',
  level_id BIGINT REFERENCES worker_levels(id) ON DELETE SET NULL,
  status TEXT NOT NULL DEFAULT 'pending_review',
  review_note TEXT NOT NULL DEFAULT '',
  session_version INTEGER NOT NULL DEFAULT 1,
  created_at TIMESTAMPTZ NOT NULL,
  updated_at TIMESTAMPTZ NOT NULL,
  reviewed_at TIMESTAMPTZ
);

CREATE TABLE IF NOT EXISTS worker_wallets (
  worker_id BIGINT PRIMARY KEY REFERENCES worker_users(id) ON DELETE CASCADE,
  available_amount INTEGER NOT NULL DEFAULT 0,
  frozen_deposit_amount INTEGER NOT NULL DEFAULT 0,
  total_credited_amount INTEGER NOT NULL DEFAULT 0,
  total_settled_amount INTEGER NOT NULL DEFAULT 0,
  created_at TIMESTAMPTZ NOT NULL,
  updated_at TIMESTAMPTZ NOT NULL
);

CREATE TABLE IF NOT EXISTS worker_wallet_ledgers (
  id BIGSERIAL PRIMARY KEY,
  worker_id BIGINT NOT NULL REFERENCES worker_users(id) ON DELETE CASCADE,
  ledger_type TEXT NOT NULL,
  amount INTEGER NOT NULL,
  balance_after INTEGER NOT NULL,
  frozen_after INTEGER NOT NULL,
  related_work_order_id BIGINT,
  audit_status TEXT NOT NULL DEFAULT 'approved',
  note TEXT NOT NULL DEFAULT '',
  payload_json JSONB NOT NULL DEFAULT '{}'::jsonb,
  created_at TIMESTAMPTZ NOT NULL
);

CREATE TABLE IF NOT EXISTS work_categories (
  id BIGSERIAL PRIMARY KEY,
  category_key TEXT NOT NULL UNIQUE,
  name TEXT NOT NULL,
  sort_order INTEGER NOT NULL DEFAULT 100,
  status TEXT NOT NULL DEFAULT 'active',
  created_at TIMESTAMPTZ NOT NULL,
  updated_at TIMESTAMPTZ NOT NULL
);

CREATE TABLE IF NOT EXISTS work_orders (
  id BIGSERIAL PRIMARY KEY,
  work_order_no TEXT NOT NULL UNIQUE,
  order_id BIGINT REFERENCES orders(id) ON DELETE SET NULL,
  order_item_id BIGINT REFERENCES order_items(id) ON DELETE SET NULL,
  task_id BIGINT REFERENCES fulfillment_tasks(id) ON DELETE SET NULL,
  platform_order_id TEXT NOT NULL DEFAULT '',
  product_name TEXT NOT NULL,
  category_id BIGINT REFERENCES work_categories(id) ON DELETE SET NULL,
  status TEXT NOT NULL DEFAULT 'pending_material',
  reward_amount INTEGER NOT NULL DEFAULT 0,
  required_deposit_amount INTEGER NOT NULL DEFAULT 0,
  deposit_threshold_amount INTEGER NOT NULL DEFAULT 20000,
  assigned_worker_id BIGINT REFERENCES worker_users(id) ON DELETE SET NULL,
  material_json JSONB NOT NULL DEFAULT '{}'::jsonb,
  requirement_json JSONB NOT NULL DEFAULT '{}'::jsonb,
  acceptance_json JSONB NOT NULL DEFAULT '{}'::jsonb,
  problem_note TEXT NOT NULL DEFAULT '',
  published_at TIMESTAMPTZ,
  assigned_at TIMESTAMPTZ,
  submitted_at TIMESTAMPTZ,
  accepted_at TIMESTAMPTZ,
  created_at TIMESTAMPTZ NOT NULL,
  updated_at TIMESTAMPTZ NOT NULL
);

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

CREATE INDEX IF NOT EXISTS idx_worker_users_status
  ON worker_users(status, id DESC);
CREATE INDEX IF NOT EXISTS idx_worker_users_level
  ON worker_users(level_id);
CREATE INDEX IF NOT EXISTS idx_worker_wallet_ledgers_worker_created
  ON worker_wallet_ledgers(worker_id, created_at DESC);
CREATE INDEX IF NOT EXISTS idx_work_orders_status
  ON work_orders(status, id DESC);
CREATE INDEX IF NOT EXISTS idx_work_orders_worker_status
  ON work_orders(assigned_worker_id, status, id DESC);
CREATE INDEX IF NOT EXISTS idx_work_orders_platform_order
  ON work_orders(platform_order_id);
CREATE INDEX IF NOT EXISTS idx_work_order_events_order_created
  ON work_order_events(work_order_id, created_at DESC);

ALTER TABLE worker_wallet_ledgers
  ADD CONSTRAINT fk_worker_wallet_ledgers_work_order
  FOREIGN KEY (related_work_order_id) REFERENCES work_orders(id) ON DELETE SET NULL;

COMMENT ON TABLE worker_levels IS '打手等级与权限配置';
COMMENT ON COLUMN worker_levels.permission_json IS '等级权限：免押额度、最大同时接单量等';
COMMENT ON TABLE worker_users IS '打手账号，注册后需后台审核';
COMMENT ON TABLE worker_wallets IS '打手余额与冻结押金汇总';
COMMENT ON TABLE worker_wallet_ledgers IS '打手钱包流水，人工充值、冻结、解冻、结算均记录';
COMMENT ON TABLE work_categories IS '接单大厅分类';
COMMENT ON TABLE work_orders IS '接单平台工单，独立于现有履约任务状态机';
COMMENT ON COLUMN work_orders.reward_amount IS '打手看到并结算的金额，单位分';
COMMENT ON COLUMN work_orders.required_deposit_amount IS '单笔工单所需冻结押金，单位分';
COMMENT ON TABLE work_order_events IS '接单工单状态与操作流水';
