import { describe, expect, it } from 'vitest'

import type { ReservationTaskFieldResult } from '@/types/reservation'
import {
  buildEditableFieldValues,
  groupReservationFields,
  validateReservationFieldValues,
} from '@/utils/reservationFieldRules'

function createField(overrides: Partial<ReservationTaskFieldResult>): ReservationTaskFieldResult {
  return {
    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: null,
    display_condition: null,
    validation_rule: null,
    write_path: null,
    opera_write_participation: 'N',
    opera_parameter_mapping: null,
    notes: null,
    value: null,
    ...overrides,
  }
}

describe('validateReservationFieldValues', () => {
  it('validates required, enum, date, and number fields from backend metadata', () => {
    const errors = validateReservationFieldValues(
      [
        createField({
          field_path: 'guest.name',
          display_name: '客人姓名',
          required_rule: 'Y',
        }),
        createField({
          field_path: 'room.type',
          display_name: '房型',
          select_editable: 'Y',
          enum_options: '豪华 Q1A, 行政套房',
        }),
        createField({
          field_path: 'stay.arrival_date',
          display_name: '入住日期',
          date_picker: 'Y',
        }),
        createField({
          field_path: 'room.count',
          display_name: '房间数',
          number_input: 'Y',
        }),
      ],
      {
        'guest.name': '',
        'room.type': '不存在的房型',
        'stay.arrival_date': '2026/07/08',
        'room.count': 'abc',
      },
    )

    expect(errors).toMatchObject({
      'guest.name': {
        code: 'required',
        field_name: '客人姓名',
      },
      'room.type': {
        code: 'enum',
        field_name: '房型',
      },
      'stay.arrival_date': {
        code: 'date',
        field_name: '入住日期',
      },
      'room.count': {
        code: 'number',
        field_name: '房间数',
      },
    })
  })

  it('groups only fields that match the current display condition', () => {
    const groups = groupReservationFields(
      [
        createField({
          field_path: 'case_keys.group_code',
          display_name: 'Group Code',
          display_condition: '对象为Group Block或Allotment时展示',
        }),
        createField({
          field_path: 'case_keys.confirmation_number',
          display_name: 'Confirmation No.',
          display_condition: '对象为FIT Reservation时展示',
        }),
      ],
      '基础信息',
      {
        'extracted_fields.booking_object_type': 'Group Block',
        'case_keys.group_code': 'GRP-001',
      },
    )

    expect(groups.flatMap((group) => group.fields.map((field) => field.field_path))).toEqual(['case_keys.group_code'])
  })

  it('builds a backend-safe editable payload without readonly or inactive fields', () => {
    const payload = buildEditableFieldValues(
      [
        createField({
          field_path: 'visible_reason',
          display_name: '生成原因',
          editable: 'N',
          input_editable: 'N',
          required_rule: 'Y',
        }),
        createField({
          field_path: 'case_keys.group_code',
          display_name: 'Group Code',
          display_condition: '对象为Group Block或Allotment时展示',
        }),
        createField({
          field_path: 'case_keys.confirmation_number',
          display_name: 'Confirmation No.',
          display_condition: '对象为FIT Reservation时展示',
        }),
      ],
      {
        visible_reason: 'AI extracted this task.',
        'case_keys.group_code': 'GRP-001',
        'case_keys.confirmation_number': '',
        'extracted_fields.booking_object_type': 'Group Block',
      },
      false,
    )

    expect(payload).toEqual({
      'case_keys.group_code': 'GRP-001',
    })
  })
})
