import type {
  ReservationOrderDetail,
  ReservationRecord,
  ReservationTaskAuditListResult,
  ReservationTaskDetailResult,
  ReservationWorkbenchItem,
} from '@/types/reservation'

const v3SourceMessageFixture: ReservationRecord = {
  source_message_id: '900083',
  source_provider: 'DEBUG_EML',
  external_message_id: 'debug-eml-900083',
  external_conversation_id: 'thread-900083',
  channel: 'EMAIL',
  sender_identifier: 'guest@example.test',
  thread_subject: 'New booking request #ORD-083',
  payload_received_at: '2026-07-08T09:18:00+08:00',
}

const v3MessageEventFixture: ReservationRecord = {
  event_role: 'current',
  current_or_history: 'CURRENT',
  source_event_index: 0,
  case_keys: {
    group_code: null,
    confirmation_number: '#ORD-083',
    reservation_number: null,
    block_code: null,
  },
  relevant_message_excerpt: 'Please book one deluxe Q1A room for July 9.',
  attachments: [],
  file_references: [],
  context_used: {
    history_message_count: 1,
  },
  extracted_fields: {
    room_items: [
      {
        room_quantity: 1,
        room_type_raw: 'Deluxe Q1A',
        pms_room_type_code: null,
      },
    ],
  },
  manual_review: {
    reason_code: 'missing_room_type_code',
    missing_fields: ['/extracted_fields/room_items/0/pms_room_type_code'],
  },
}

export const reservationWorkbenchItems: ReservationWorkbenchItem[] = [
  {
    order_id: '100083',
    hotel_id: 'HOTEL-TEST',
    display_name: '#ORD-083',
    order_key_type: 'CONFIRMATION_NUMBER',
    order_business_key: '#ORD-083',
    active_business_key: '#ORD-083',
    temporary_order_code: null,
    order_status: 'ACTIVE',
    business_key_source: 'AI_RESULT',
    source_message_id: '900083',
    guest_name: '王建国',
    room_type: '豪华 Q1A',
    arrival_date: '2026-07-09',
    departure_date: '2026-07-10',
    amount: 1246,
    currency_code: 'CNY',
    current_task_id: '700083',
    current_task_card_type: 'NEW_BOOKING',
    current_task_status: 'PENDING_CONFIRM',
    updated_at: '2026-07-08T09:26:00+08:00',
  },
  {
    order_id: '100084',
    hotel_id: 'HOTEL-TEST',
    display_name: 'GRP-SEA-0712',
    order_key_type: 'GROUP_CODE',
    order_business_key: 'GRP-SEA-0712',
    active_business_key: 'GRP-SEA-0712',
    temporary_order_code: null,
    order_status: 'ACTIVE',
    business_key_source: 'MANUAL_REVIEW',
    source_message_id: '900084',
    guest_name: 'Mark Lee',
    room_type: '行政套房',
    arrival_date: '2026-07-12',
    departure_date: '2026-07-15',
    amount: 3450,
    currency_code: 'CNY',
    current_task_id: '700084',
    current_task_card_type: 'ROOMING_LIST',
    current_task_status: 'READY',
    updated_at: '2026-07-08T08:42:00+08:00',
  },
  {
    order_id: '100085',
    hotel_id: 'HOTEL-TEST',
    display_name: 'TMP-20260708-001',
    order_key_type: 'TEMPORARY_CODE',
    order_business_key: null,
    active_business_key: null,
    temporary_order_code: 'TMP-20260708-001',
    order_status: 'TEMPORARY',
    business_key_source: 'PENDING_AI',
    source_message_id: '900085',
    guest_name: '李雪梅',
    room_type: '豪华双床',
    arrival_date: '2026-07-15',
    departure_date: '2026-07-16',
    amount: 665,
    currency_code: 'CNY',
    current_task_id: '700085',
    current_task_card_type: 'NEW_BOOKING',
    current_task_status: 'BLOCKED',
    updated_at: '2026-07-08T08:15:00+08:00',
  },
]

const firstWorkbenchItem = reservationWorkbenchItems[0] as ReservationWorkbenchItem
const secondWorkbenchItem = reservationWorkbenchItems[1] as ReservationWorkbenchItem
const thirdWorkbenchItem = reservationWorkbenchItems[2] as ReservationWorkbenchItem

export const reservationOrderDetails: ReservationOrderDetail[] = [
  {
    ...firstWorkbenchItem,
    source_message: {
      source_message_id: '900083',
      channel: 'EMAIL',
      sender_identifier: 'guest@example.test',
      thread_subject: 'New booking request #ORD-083',
      payload_received_at: '2026-07-08T09:18:00+08:00',
      summary: '客人请求 2026-07-09 入住 1 晚，豪华 Q1A，已提供手机号和预计到店时间。',
    },
    tasks: [
      {
        task_id: '700083',
        order_id: '100083',
        system_task_type: 'CREATE_RESERVATION',
        task_card_type: 'NEW_BOOKING',
        task_status: 'PENDING_CONFIRM',
        queue_participation: true,
        execution_order: 10,
        blocked_by_task_id: null,
        updated_at: '2026-07-08T09:26:00+08:00',
      },
      {
        task_id: '700086',
        order_id: '100083',
        system_task_type: 'ADD_TRACE_NOTE',
        task_card_type: 'TRACE_NOTE',
        task_status: 'READY',
        queue_participation: true,
        execution_order: 20,
        blocked_by_task_id: '700083',
        updated_at: '2026-07-08T09:27:00+08:00',
      },
    ],
  },
  {
    ...secondWorkbenchItem,
    source_message: {
      source_message_id: '900084',
      channel: 'EMAIL',
      sender_identifier: 'sales@example.test',
      thread_subject: 'Rooming list update for GRP-SEA-0712',
      payload_received_at: '2026-07-08T08:31:00+08:00',
      summary: '销售团队补充团队房表，需要核对姓名、房型和入住离店日期。',
    },
    tasks: [
      {
        task_id: '700084',
        order_id: '100084',
        system_task_type: 'UPDATE_ROOMING_LIST',
        task_card_type: 'ROOMING_LIST',
        task_status: 'READY',
        queue_participation: true,
        execution_order: 10,
        blocked_by_task_id: null,
        updated_at: '2026-07-08T08:42:00+08:00',
      },
    ],
  },
  {
    ...thirdWorkbenchItem,
    source_message: {
      source_message_id: '900085',
      channel: 'EMAIL',
      sender_identifier: 'guest@example.test',
      thread_subject: 'Need to change my booking',
      payload_received_at: '2026-07-08T08:02:00+08:00',
      summary: '客人提到需要修改订单，但缺少可稳定匹配的业务号，需要人工复核。',
    },
    tasks: [
      {
        task_id: '700085',
        order_id: '100085',
        system_task_type: 'NEW_BOOKING',
        task_card_type: 'NEW_BOOKING',
        task_status: 'BLOCKED',
        queue_participation: true,
        execution_order: 10,
        blocked_by_task_id: null,
        updated_at: '2026-07-08T08:15:00+08:00',
      },
    ],
  },
]

export const reservationTaskDetails: ReservationTaskDetailResult[] = [
  {
    task_id: '700083',
    order_id: '100083',
    source_message_id: '900083',
    source_subject: 'New booking request #ORD-083',
    source_sender_summary: 'guest@example.test',
    source_received_at: '2026-07-08T09:18:00+08:00',
    external_conversation_id: 'thread-900083',
    conversation_message_count: 2,
    result_type: 'normal_task',
    ai_task_type: 'NEW_BOOKING',
    task_subtype: 'NEW_BOOKING',
    route_code: 'NEW_BOOKING_NORMAL',
    system_process_category: 'BUSINESS_TASK',
    system_task_type: 'CREATE_RESERVATION',
    task_card_type: 'NEW_BOOKING',
    task_status: 'PENDING_CONFIRM',
    field_contract_version: 'reservation-task-card-v1',
    draft_payload: null,
    confirmed_payload: null,
    availability: {
      blocked: false,
      read_only: false,
      editable: true,
      confirmable: true,
      executable: false,
      blocked_by_task_id: null,
      blocked_reason: null,
    },
    fields: [
      {
        row_number: 1,
        card_name: 'New Booking',
        display_area: '客人信息',
        field_path: 'guest.name',
        display_name: '客人姓名',
        visible: 'Y',
        editable: 'Y',
        input_editable: 'Y',
        select_editable: 'N',
        date_picker: 'N',
        number_input: 'N',
        file_display: 'N',
        table_editable: 'N',
        enum_options: null,
        required_rule: 'Y',
        display_condition: null,
        validation_rule: '不能为空',
        write_path: 'reservation.guest.name',
        opera_write_participation: 'Y',
        opera_parameter_mapping: 'profile.name',
        notes: '来自邮件正文，确认前可修正。',
        value: '王建国',
      },
      {
        row_number: 2,
        card_name: 'New Booking',
        display_area: '客人信息',
        field_path: 'guest.phone',
        display_name: '手机号',
        visible: 'Y',
        editable: 'Y',
        input_editable: 'Y',
        select_editable: 'N',
        date_picker: 'N',
        number_input: 'N',
        file_display: 'N',
        table_editable: 'N',
        enum_options: null,
        required_rule: 'Y',
        display_condition: null,
        validation_rule: '手机号脱敏回显',
        write_path: 'reservation.guest.phone',
        opera_write_participation: 'Y',
        opera_parameter_mapping: 'profile.phone',
        notes: '前端只展示后端脱敏后的值。',
        value: '186****1234',
      },
      {
        row_number: 3,
        card_name: 'New Booking',
        display_area: '预订信息',
        field_path: 'stay.arrival_date',
        display_name: '入住日期',
        visible: 'Y',
        editable: 'Y',
        input_editable: 'N',
        select_editable: 'N',
        date_picker: 'Y',
        number_input: 'N',
        file_display: 'N',
        table_editable: 'N',
        enum_options: null,
        required_rule: 'Y',
        display_condition: null,
        validation_rule: 'YYYY-MM-DD',
        write_path: 'reservation.stay.arrivalDate',
        opera_write_participation: 'Y',
        opera_parameter_mapping: 'arrivalDate',
        notes: null,
        value: '2026-07-09',
      },
      {
        row_number: 4,
        card_name: 'New Booking',
        display_area: '预订信息',
        field_path: 'stay.departure_date',
        display_name: '离店日期',
        visible: 'Y',
        editable: 'Y',
        input_editable: 'N',
        select_editable: 'N',
        date_picker: 'Y',
        number_input: 'N',
        file_display: 'N',
        table_editable: 'N',
        enum_options: null,
        required_rule: 'Y',
        display_condition: null,
        validation_rule: 'YYYY-MM-DD',
        write_path: 'reservation.stay.departureDate',
        opera_write_participation: 'Y',
        opera_parameter_mapping: 'departureDate',
        notes: null,
        value: '2026-07-10',
      },
      {
        row_number: 5,
        card_name: 'New Booking',
        display_area: '预订信息',
        field_path: 'room.type',
        display_name: '房型',
        visible: 'Y',
        editable: 'Y',
        input_editable: 'N',
        select_editable: 'Y',
        date_picker: 'N',
        number_input: 'N',
        file_display: 'N',
        table_editable: 'N',
        enum_options: '豪华 Q1A, 行政套房, 豪华双床',
        required_rule: 'Y',
        display_condition: null,
        validation_rule: '必须来自房型枚举',
        write_path: 'reservation.room.type',
        opera_write_participation: 'Y',
        opera_parameter_mapping: 'roomType',
        notes: null,
        value: '豪华 Q1A',
      },
      {
        row_number: 6,
        card_name: 'New Booking',
        display_area: '预订信息',
        field_path: 'room.count',
        display_name: '房间数',
        visible: 'Y',
        editable: 'Y',
        input_editable: 'N',
        select_editable: 'N',
        date_picker: 'N',
        number_input: 'Y',
        file_display: 'N',
        table_editable: 'N',
        enum_options: null,
        required_rule: 'Y',
        display_condition: null,
        validation_rule: '正整数',
        write_path: 'reservation.room.count',
        opera_write_participation: 'Y',
        opera_parameter_mapping: 'roomCount',
        notes: null,
        value: 1,
      },
      {
        row_number: 7,
        card_name: 'New Booking',
        display_area: '付款信息',
        field_path: 'payment.amount',
        display_name: '订单金额',
        visible: 'Y',
        editable: 'N',
        input_editable: 'N',
        select_editable: 'N',
        date_picker: 'N',
        number_input: 'N',
        file_display: 'N',
        table_editable: 'N',
        enum_options: null,
        required_rule: 'N',
        display_condition: null,
        validation_rule: null,
        write_path: null,
        opera_write_participation: 'N',
        opera_parameter_mapping: null,
        notes: '金额由后端规则或外部系统确认，前端只读展示。',
        value: 'CNY 1,246',
      },
      {
        row_number: 8,
        card_name: 'New Booking',
        display_area: '附件与备注',
        field_path: 'attachments.voucher',
        display_name: '附件',
        visible: 'Y',
        editable: 'N',
        input_editable: 'N',
        select_editable: 'N',
        date_picker: 'N',
        number_input: 'N',
        file_display: 'Y',
        table_editable: 'N',
        enum_options: null,
        required_rule: 'N',
        display_condition: null,
        validation_rule: null,
        write_path: null,
        opera_write_participation: 'N',
        opera_parameter_mapping: null,
        notes: '只展示后端安全摘要，不展示原始附件 URL。',
        value: ['booking-request.pdf'],
      },
      {
        row_number: 9,
        card_name: 'New Booking',
        display_area: '附件与备注',
        field_path: 'notes.guest_request',
        display_name: '客房备注',
        visible: 'Y',
        editable: 'Y',
        input_editable: 'Y',
        select_editable: 'N',
        date_picker: 'N',
        number_input: 'N',
        file_display: 'N',
        table_editable: 'N',
        enum_options: null,
        required_rule: 'N',
        display_condition: null,
        validation_rule: null,
        write_path: 'reservation.notes.guestRequest',
        opera_write_participation: 'CONDITION',
        opera_parameter_mapping: 'comments',
        notes: '客人要求高楼层，预计 12:30 到店。',
        value: '需要安静房间，支持高楼层。',
      },
    ],
    opera_operations: [],
  },
  {
    task_id: '700084',
    order_id: '100084',
    source_message_id: '900084',
    source_subject: 'Rooming list update for GRP-SEA-0712',
    source_sender_summary: 'sales@example.test',
    source_received_at: '2026-07-08T08:31:00+08:00',
    external_conversation_id: 'thread-900084',
    conversation_message_count: 3,
    result_type: 'normal_task',
    ai_task_type: 'ROOMING_LIST',
    task_subtype: 'ROOMING_LIST',
    route_code: 'ROOMING_LIST_NORMAL',
    system_process_category: 'BUSINESS_TASK',
    system_task_type: 'UPDATE_ROOMING_LIST',
    task_card_type: 'ROOMING_LIST',
    task_status: 'READY',
    field_contract_version: 'reservation-task-card-v1',
    draft_payload: {
      'group.code': 'GRP-SEA-0712',
      'rooming_list.rows': '3 rows',
    },
    confirmed_payload: {
      'group.code': 'GRP-SEA-0712',
      'rooming_list.rows': '3 rows',
    },
    availability: {
      blocked: false,
      read_only: true,
      editable: false,
      confirmable: false,
      executable: true,
      blocked_by_task_id: null,
      blocked_reason: null,
    },
    fields: [
      {
        row_number: 1,
        card_name: 'Rooming List',
        display_area: '团队信息',
        field_path: 'group.code',
        display_name: '团队号',
        visible: 'Y',
        editable: 'N',
        input_editable: 'N',
        select_editable: 'N',
        date_picker: 'N',
        number_input: 'N',
        file_display: 'N',
        table_editable: 'N',
        enum_options: null,
        required_rule: 'Y',
        display_condition: null,
        validation_rule: null,
        write_path: 'group.code',
        opera_write_participation: 'Y',
        opera_parameter_mapping: 'groupCode',
        notes: null,
        value: 'GRP-SEA-0712',
      },
      {
        row_number: 2,
        card_name: 'Rooming List',
        display_area: '房表',
        field_path: 'rooming_list.rows',
        display_name: '房表行',
        visible: 'Y',
        editable: 'N',
        input_editable: 'N',
        select_editable: 'N',
        date_picker: 'N',
        number_input: 'N',
        file_display: 'N',
        table_editable: 'Y',
        enum_options: null,
        required_rule: 'Y',
        display_condition: null,
        validation_rule: '至少一行',
        write_path: 'group.roomingList',
        opera_write_participation: 'Y',
        opera_parameter_mapping: 'roomingList',
        notes: '表格编辑第一版先只做摘要展示，详细编辑后续补独立组件。',
        value: [
          { guest: 'Mark Lee', room_type: '行政套房', arrival_date: '2026-07-12' },
          { guest: 'Anna Kim', room_type: '行政套房', arrival_date: '2026-07-12' },
          { guest: '周为三', room_type: '豪华双床', arrival_date: '2026-07-13' },
        ],
      },
    ],
    opera_operations: [
      {
        operation_id: '810001',
        task_id: '700084',
        operation_sequence: 10,
        operation_code: 'UPDATE_ROOMING_LIST',
        operation_name: '更新团队房表',
        operation_status: 'PENDING',
        attempt_count: 0,
        last_error_message: null,
        attempts: [],
      },
    ],
  },
  {
    task_id: '700085',
    order_id: '100085',
    source_message_id: '900085',
    source_subject: 'Need to change my booking',
    source_sender_summary: 'guest@example.test',
    source_received_at: '2026-07-08T08:02:00+08:00',
    external_conversation_id: 'thread-900085',
    conversation_message_count: 2,
    result_type: 'manual_review',
    ai_task_type: 'NEW_BOOKING',
    task_subtype: 'NEW_BOOKING',
    route_code: 'NEW_BOOKING_MANUAL_REVIEW',
    system_process_category: 'BUSINESS_TASK',
    review_status: 'PENDING',
    manual_review: {
      reason_code: 'missing_room_type_code',
      review_instruction: '请确认 PMS 房型代码后解阻原任务卡。',
      missing_fields: ['/extracted_fields/room_items/0/pms_room_type_code'],
      source_message: {
        ...v3SourceMessageFixture,
        source_message_id: '900085',
        thread_subject: 'Need to change my booking',
      },
      message_events: [
        {
          ...v3MessageEventFixture,
          relevant_message_excerpt: 'I need to change my booking but did not include the PMS room type code.',
          case_keys: {
            group_code: null,
            confirmation_number: null,
            reservation_number: null,
            block_code: null,
          },
        },
      ],
    },
    system_task_type: 'NEW_BOOKING',
    task_card_type: 'NEW_BOOKING',
    task_status: 'BLOCKED',
    field_contract_version: '20260711-p0',
    draft_payload: null,
    confirmed_payload: null,
    availability: {
      blocked: true,
      read_only: true,
      editable: false,
      confirmable: false,
      executable: false,
      blocked_by_task_id: null,
      blocked_reason: 'MANUAL_REVIEW_REQUIRED',
    },
    fields: [
      {
        row_number: 1,
        card_name: 'New Booking',
        result_type: 'manual_review',
        task_type: 'NEW_BOOKING',
        task_subtype: 'NEW_BOOKING',
        display_area: '预订信息',
        field_path: 'extracted_fields.room_items.0.pms_room_type_code',
        field_pointer: '/extracted_fields/room_items/0/pms_room_type_code',
        legacy_field_path: 'extracted_fields.pms_room_type_code',
        display_name: 'PMS 房型代码',
        default_value_source: 'manual_review',
        visible: 'Y',
        editable: 'Y',
        input_editable: 'Y',
        select_editable: 'N',
        date_picker: 'N',
        number_input: 'N',
        file_display: 'N',
        table_editable: 'N',
        enum_options: null,
        required_rule: 'Y',
        display_condition: null,
        validation_rule: null,
        write_path: 'extracted_fields.room_items.0.pms_room_type_code',
        opera_write_participation: 'Y',
        opera_parameter_mapping: 'roomTypeCode',
        notes: 'V3 同卡人工复核缺失字段。',
        value: null,
      },
    ],
    opera_operations: [],
  },
  {
    task_id: '700090',
    order_id: 'TECH-S10-900090',
    source_message_id: '900090',
    source_subject: 'FYI arrival time',
    source_sender_summary: 'guest@example.test',
    source_received_at: '2026-07-08T07:40:00+08:00',
    external_conversation_id: 'thread-900090',
    conversation_message_count: 1,
    result_type: 'source_message_review_notification',
    ai_task_type: 'Message Notification',
    task_subtype: 'S10',
    route_code: 'S10',
    system_process_category: 'SOURCE_MESSAGE_NOTIFICATION',
    system_task_type: 'MESSAGE_NOTIFICATION',
    task_card_type: 'MESSAGE_NOTIFICATION',
    task_status: 'COMPLETED',
    field_contract_version: 'reservation-task-card-v3',
    draft_payload: null,
    confirmed_payload: null,
    availability: {
      blocked: false,
      read_only: true,
      editable: false,
      confirmable: false,
      executable: false,
      blocked_by_task_id: null,
      blocked_reason: null,
    },
    fields: [],
    source_message_only_result: {
      entry_result_code: 'S10',
      entry_result_meaning: 'PURE_INFORMATION',
      entry_result_description: '纯信息类邮件，仅展示来源消息通知。',
      entry_result_source_message_id: '900090',
      result_type: 'source_message_review_notification',
      route_code: 'S10',
      agent_assessment: {
        source_message: {
          ...v3SourceMessageFixture,
          source_message_id: '900090',
          thread_subject: 'FYI arrival time',
        },
        message_events: [
          {
            ...v3MessageEventFixture,
            relevant_message_excerpt: 'FYI: I may arrive at 3 PM.',
            manual_review: null,
            extracted_fields: {},
            case_keys: {
              group_code: null,
              confirmation_number: null,
              reservation_number: null,
              block_code: null,
            },
          },
        ],
      },
      notification: {
        summary: '客人仅同步到店时间，不需要生成业务任务。',
      },
      manual_review: null,
      raw_answer: 'S10,900090',
    },
    adapter_contract_errors: [],
    unhandled_intents: [],
    opera_operations: [],
  },
]

export const reservationTaskAudits: ReservationTaskAuditListResult[] = [
  {
    task_id: '700083',
    items: [
      {
        audit_id: 'AUD-700083-1',
        order_id: '100083',
        task_id: '700083',
        operation_id: null,
        actor_type: 'SYSTEM',
        actor_id: 'reservation-ai-intake',
        action: 'TASK_CREATED',
        reason: 'AI result projected to reservation task',
        before_snapshot: null,
        after_snapshot: { task_status: 'PENDING_CONFIRM' },
        occurred_at: '2026-07-08T09:26:00+08:00',
      },
    ],
  },
  {
    task_id: '700084',
    items: [
      {
        audit_id: 'AUD-700084-1',
        order_id: '100084',
        task_id: '700084',
        operation_id: null,
        actor_type: 'USER',
        actor_id: 'andy',
        action: 'TASK_CONFIRMED',
        reason: '房表字段人工确认',
        before_snapshot: { task_status: 'PENDING_CONFIRM' },
        after_snapshot: { task_status: 'READY' },
        occurred_at: '2026-07-08T08:42:00+08:00',
      },
    ],
  },
  {
    task_id: '700085',
    items: [
      {
        audit_id: 'AUD-700085-1',
        order_id: '100085',
        task_id: '700085',
        operation_id: null,
        actor_type: 'SYSTEM',
        actor_id: 'reservation-ai-intake',
        action: 'MANUAL_REVIEW_CREATED',
        reason: 'V3 type-known manual_review missing PMS room type code',
        before_snapshot: null,
        after_snapshot: {
          result_type: 'manual_review',
          review_status: 'PENDING',
          missing_fields: ['/extracted_fields/room_items/0/pms_room_type_code'],
        },
        occurred_at: '2026-07-08T08:15:00+08:00',
      },
    ],
  },
]
