/**
 * `robot_error_reports` を読むときの `select`（指示書AU 段階0-4。§11 Q6。D-158）。
 *
 * ★★`robot_id` と `robot` リレーションを読まないこと★★ —
 *   先方の DDL では `robot_id` が NULL 可になった（TeamWorks 第11回 P-8 b）が、
 *   `prisma/schema.prisma` は NOT NULL のまま（`db pull` は監督が行う。本リポジトリではスキーマを変えない）。
 *   ★`select` なし（＝全列）や `include` で読むと、NULL の行が1件入っただけで読み取りが落ちる★。
 *   ★読む列を `select` で絞っておけば、NULL の行が入っても落ちない★。
 *
 * ★読み取りは必ずこのファイルの定数を使う★（`lib/db/index.ts` の `robotErrorReport.findMany` /
 *   `findUnique`）。列を足すときも ★`robot_id` / `robot` は足さない★。
 *   `lib/db/__tests__/robot-error-select.test.ts` が固定している。
 * ★書き込み（`lib/db/robot-error-write.ts` の `updateMany`）は対象外★（行を読まない）。
 */
import type { Prisma } from "@/lib/generated/prisma/client";

/**
 * 本清掃行ごとのエラー件数（`errorCountByMainTimetable()`）に要る列。
 * `reportKpis()` / `facilityPerformance()` の当期が使う。
 */
export const ROBOT_ERROR_COUNT_SELECT = {
  clean_timetable_id: true,
} as const satisfies Prisma.RobotErrorReportSelect;

/**
 * 清掃詳細ログのロボット状態（`RobotErrorLike`。`cleanLogs()`）に要る列。
 */
export const ROBOT_ERROR_STATUS_SELECT = {
  clean_timetable_id: true,
  error_type: true,
  errored_at: true,
} as const satisfies Prisma.RobotErrorReportSelect;

/**
 * 当期のエラー内訳（`ErrorReportLike`。`countMainErrorTypesInRange()`。`errorBreakdown()`）に要る列。
 * ★`errored_at` / `created_at` は `ErrorReportLike`（`PrecheckDetectedAtSource`）の型が要求するので読む★。
 */
export const ROBOT_ERROR_TYPE_SELECT = {
  clean_timetable_id: true,
  error_type: true,
  errored_at: true,
  created_at: true,
} as const satisfies Prisma.RobotErrorReportSelect;

/**
 * 事前チェックのアラート行（`PrecheckErrorRowSource`。`precheckItemFromErrorRow()`）に要る列。
 * ★`prechecks()`（一覧）と `precheckItemById()`（PATCH の前後の読み取り）が同じ定数を使う★
 *   （行の作り方を1本にしたのと同じ理由。指示書AT 段階4-1）。
 * ★`facility` は施設名と `deleted_at`、`section` は `deleted_at` だけ★（旧: `include: { facility: true }`。`deleted_at` は可視性の判定用。指示書BB U1）。
 */
export const ROBOT_ERROR_PRECHECK_ROW_SELECT = {
  error_id: true,
  clean_timetable_id: true,
  serial_number: true,
  facility_id: true,
  // ★`deleted_at` は可視性の判定用（`isVisibleErrorRow()`。削除済みの施設・清掃エリアの行は出さない。指示書BB U1・5-4）★
  facility: { select: { facility_name: true, deleted_at: true } },
  section: { select: { deleted_at: true } },
  error_type: true,
  errored_at: true,
  created_at: true,
  status: true,
  responded_at: true,
  resolved_at: true,
  comment: true,
} as const satisfies Prisma.RobotErrorReportSelect;

/**
 * エラー一覧の1段目（キーだけ。指示書AW D8）。絞り込み・並べ替えに要る列だけ。
 * ★`clean_timetable_id` は予定あり／予定外の絞り込みに使う★。
 * ★`serial_number`・`error_level`・`comment` は並べ替えに使う★
 */
export const ROBOT_ERROR_LIST_KEY_SELECT = {
  error_id: true,
  clean_timetable_id: true,
  facility_id: true,
  // ★可視性（`isVisibleErrorRow()`。削除済みの施設・清掃エリアの行を外す。指示書AX U8・D12）★
  facility: { select: { deleted_at: true } },
  section: { select: { deleted_at: true } },
  serial_number: true,
  error_type: true,
  error_level: true,
  errored_at: true,
  created_at: true,
  status: true,
  comment: true,
} as const satisfies Prisma.RobotErrorReportSelect;

/**
 * エラー一覧の2段目・PATCH の前後の読み取り（`errorReportItemFromRow()`）に要る列。
 * ★`errorReports()` と `errorReportById()` が同じ定数を使う★（行の作り方を1本にする。AT 段階4-1 と同じ理由）
 * ★`error_list_id` はこの定数だけが読む★（エラー一覧の行だけが `ErrorReportItem.error_list*` を持つ。指示書BD §5-3）
 * ★`section` は `deleted_at` だけ（指示書AX U8）。`robot_id` / `robot` は足さない★（指示書AW 4-1。`robot_id` が NULL の行で落ちる）
 */
export const ROBOT_ERROR_LIST_ROW_SELECT = {
  error_id: true,
  clean_timetable_id: true,
  serial_number: true,
  facility_id: true,
  facility: { select: { facility_name: true, deleted_at: true } },
  section: { select: { deleted_at: true } },
  error_type: true,
  error_level: true,
  errored_at: true,
  created_at: true,
  status: true,
  responded_at: true,
  resolved_at: true,
  comment: true,
  // ★エラー内容（指示書BD §5-3。D-184）★ — 名前解決は `lib/db/error-list-source.ts`（`error_lists` を id IN で1回）
  error_list_id: true,
} as const satisfies Prisma.RobotErrorReportSelect;

/**
 * D-Sec アラート（`dsAlertFromErrorRow()`。`dsAlerts()`）に要る列（指示書AX）。
 * ★`section` は清掃エリア名だけ★（D-Sec のカードに出す）。★`robot_id` / `robot` を足さない★（D-Sec の行は全部 `robot_id` NULL）。
 * ★`clean_timetable_id` は使わないが、select の共通テスト（紐づけのキーを含む）のために読む★
 */
export const ROBOT_ERROR_DS_ALERT_SELECT = {
  error_id: true,
  clean_timetable_id: true,
  serial_number: true,
  facility_id: true,
  facility: { select: { facility_name: true, deleted_at: true } }, // deleted_at は可視性（U8・段階2-7）
  section: { select: { section_name: true, deleted_at: true } },
  error_type: true,
  errored_at: true,
  created_at: true,
  status: true,
  responded_at: true,
  resolved_at: true,
  comment: true,
} as const satisfies Prisma.RobotErrorReportSelect;
