CREATE TABLE IF NOT EXISTS qipai_cleaning_settlements (
  id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
  tenant_id BIGINT UNSIGNED NOT NULL,
  settlement_no VARCHAR(64) NOT NULL,
  cleaner_user_id BIGINT UNSIGNED NOT NULL,
  store_id BIGINT UNSIGNED NULL,
  status VARCHAR(32) NOT NULL DEFAULT 'DRAFT',
  task_count INT UNSIGNED NOT NULL DEFAULT 0,
  total_reward_cents INT UNSIGNED NOT NULL DEFAULT 0,
  period_start DATETIME(3) NULL,
  period_end DATETIME(3) NULL,
  generated_by BIGINT UNSIGNED NOT NULL,
  confirmed_by BIGINT UNSIGNED NULL,
  confirmed_at DATETIME(3) NULL,
  paid_at DATETIME(3) NULL,
  note VARCHAR(512) NOT NULL DEFAULT '',
  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_settlements_tenant FOREIGN KEY (tenant_id) REFERENCES qipai_tenants(id),
  CONSTRAINT fk_qipai_cleaning_settlements_cleaner FOREIGN KEY (cleaner_user_id) REFERENCES qipai_users(id),
  CONSTRAINT fk_qipai_cleaning_settlements_store FOREIGN KEY (store_id) REFERENCES qipai_stores(id),
  CONSTRAINT fk_qipai_cleaning_settlements_generated_by FOREIGN KEY (generated_by) REFERENCES qipai_users(id),
  CONSTRAINT fk_qipai_cleaning_settlements_confirmed_by FOREIGN KEY (confirmed_by) REFERENCES qipai_users(id),
  CONSTRAINT chk_qipai_cleaning_settlement_status CHECK (
    status IN ('DRAFT', 'CONFIRMED', 'PAID', 'CANCELLED')
  ),
  UNIQUE KEY uq_qipai_cleaning_settlement_no (tenant_id, settlement_no),
  KEY idx_qipai_cleaning_settlement_cleaner (tenant_id, cleaner_user_id, status, created_at),
  KEY idx_qipai_cleaning_settlement_store (tenant_id, store_id, status, created_at)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS qipai_cleaning_settlement_items (
  id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
  tenant_id BIGINT UNSIGNED NOT NULL,
  settlement_id BIGINT UNSIGNED NOT NULL,
  task_id BIGINT UNSIGNED NOT NULL,
  reward_cents INT UNSIGNED NOT NULL DEFAULT 0,
  created_at DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
  CONSTRAINT fk_qipai_cleaning_settlement_items_tenant FOREIGN KEY (tenant_id) REFERENCES qipai_tenants(id),
  CONSTRAINT fk_qipai_cleaning_settlement_items_settlement FOREIGN KEY (settlement_id) REFERENCES qipai_cleaning_settlements(id),
  CONSTRAINT fk_qipai_cleaning_settlement_items_task FOREIGN KEY (task_id) REFERENCES qipai_cleaning_tasks(id),
  UNIQUE KEY uq_qipai_cleaning_settlement_item_task (tenant_id, task_id),
  KEY idx_qipai_cleaning_settlement_items_parent (tenant_id, settlement_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

INSERT IGNORE INTO qipai_permissions (code, name, category) VALUES
  ('cleaning.settlement.read', '查看保洁结算', 'cleaning'),
  ('cleaning.settlement.write', '生成保洁结算', '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 = 'cleaning.settlement.read')
  OR (r.code IN ('STORE_ADMIN', 'TENANT_ADMIN', 'PLATFORM_ADMIN')
      AND p.code IN ('cleaning.settlement.read', 'cleaning.settlement.write'))
WHERE r.status = 'ACTIVE' AND r.deleted_at IS NULL;

INSERT IGNORE INTO qipai_schema_migrations (version, name)
VALUES ('2026062626', 'm08b_cleaning_settlements');
