CREATE TABLE IF NOT EXISTS qipai_cleaning_tasks (
  id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
  tenant_id BIGINT UNSIGNED NOT NULL,
  store_id BIGINT UNSIGNED NOT NULL,
  room_id BIGINT UNSIGNED NOT NULL,
  order_id BIGINT UNSIGNED NULL,
  task_no VARCHAR(64) NOT NULL,
  status VARCHAR(32) NOT NULL DEFAULT 'WAITING',
  cleaner_user_id BIGINT UNSIGNED NULL,
  priority TINYINT UNSIGNED NOT NULL DEFAULT 5,
  reward_cents INT UNSIGNED NOT NULL DEFAULT 0,
  requirement VARCHAR(512) NOT NULL DEFAULT '',
  photo_urls_json JSON NOT NULL,
  reject_reason VARCHAR(512) NOT NULL DEFAULT '',
  claimed_at DATETIME(3) NULL,
  started_at DATETIME(3) NULL,
  submitted_at DATETIME(3) NULL,
  completed_at DATETIME(3) NULL,
  settled_at DATETIME(3) NULL,
  cancelled_at DATETIME(3) NULL,
  created_at DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
  updated_at DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3) ON UPDATE CURRENT_TIMESTAMP(3),
  deleted_at DATETIME(3) NULL,
  CONSTRAINT fk_qipai_cleaning_tasks_tenant FOREIGN KEY (tenant_id) REFERENCES qipai_tenants(id),
  CONSTRAINT fk_qipai_cleaning_tasks_store FOREIGN KEY (store_id) REFERENCES qipai_stores(id),
  CONSTRAINT fk_qipai_cleaning_tasks_room FOREIGN KEY (room_id) REFERENCES qipai_rooms(id),
  CONSTRAINT fk_qipai_cleaning_tasks_order FOREIGN KEY (order_id) REFERENCES qipai_orders(id),
  CONSTRAINT fk_qipai_cleaning_tasks_cleaner FOREIGN KEY (cleaner_user_id) REFERENCES qipai_users(id),
  CONSTRAINT chk_qipai_cleaning_task_status CHECK (
    status IN (
      'WAITING', 'CLAIMED', 'STARTED', 'SUBMITTED', 'COMPLETED',
      'REJECTED', 'EXEMPT', 'SETTLED', 'CANCELLED'
    )
  ),
  UNIQUE KEY uq_qipai_cleaning_task_no (tenant_id, task_no),
  UNIQUE KEY uq_qipai_cleaning_task_order (tenant_id, order_id),
  KEY idx_qipai_cleaning_hall (tenant_id, store_id, status, priority, created_at),
  KEY idx_qipai_cleaning_cleaner (tenant_id, cleaner_user_id, status, updated_at),
  KEY idx_qipai_cleaning_room (tenant_id, room_id, status)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS qipai_cleaning_task_events (
  id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
  tenant_id BIGINT UNSIGNED NOT NULL,
  task_id BIGINT UNSIGNED NOT NULL,
  from_status VARCHAR(32) NULL,
  to_status VARCHAR(32) NOT NULL,
  action VARCHAR(32) NOT NULL,
  actor_id BIGINT UNSIGNED NOT NULL,
  trace_id VARCHAR(128) NOT NULL,
  note VARCHAR(512) NOT NULL DEFAULT '',
  metadata JSON NOT NULL,
  created_at DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
  CONSTRAINT fk_qipai_cleaning_events_tenant FOREIGN KEY (tenant_id) REFERENCES qipai_tenants(id),
  CONSTRAINT fk_qipai_cleaning_events_task FOREIGN KEY (task_id) REFERENCES qipai_cleaning_tasks(id),
  UNIQUE KEY uq_qipai_cleaning_event_trace (tenant_id, task_id, trace_id),
  KEY idx_qipai_cleaning_events_task (tenant_id, task_id, id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

INSERT IGNORE INTO qipai_permissions (code, name, category) VALUES
  ('cleaning.task.write', '处理保洁任务', 'cleaning'),
  ('cleaning.statistics.read', '查看保洁统计', 'cleaning');

INSERT IGNORE INTO qipai_role_permissions (tenant_id, role_id, permission_id)
SELECT r.tenant_id, r.id, p.id
FROM qipai_roles r
INNER JOIN qipai_permissions p
  ON (r.code = 'CLEANER' AND p.code IN (
        'cleaning.task.read', 'cleaning.task.write', 'cleaning.statistics.read'
      ))
  OR (r.code IN ('STORE_ADMIN', 'TENANT_ADMIN', 'PLATFORM_ADMIN')
      AND p.code IN ('cleaning.task.read', 'cleaning.task.write', 'cleaning.statistics.read'))
WHERE r.status = 'ACTIVE' AND r.deleted_at IS NULL;

INSERT IGNORE INTO qipai_schema_migrations (version, name)
VALUES ('2026062525', 'm08b_cleaner_tasks');
