import { describe, expect, test } from 'vitest';
import type { QualityIssueRow } from '../api/types';
import { isLikelyVIN, qualityIssueVehicleLookup } from './vehicleLookup';

function issue(row: Partial<QualityIssueRow>): QualityIssueRow {
  return {
    vin: '',
    plate: '',
    phone: '',
    sourceEndpoint: '',
    protocol: 'JT808',
    issueType: 'VIN_MISSING',
    severity: 'warning',
    lastSeen: '',
    detail: '',
    ...row
  };
}

describe('qualityIssueVehicleLookup', () => {
  test('prefers a valid VIN', () => {
    expect(qualityIssueVehicleLookup(issue({ vin: 'VIN001', plate: '粤A12345', phone: '13300000001' }))).toEqual({
      key: 'VIN001',
      label: '车辆服务'
    });
  });

  test('uses plate when VIN is missing', () => {
    expect(qualityIssueVehicleLookup(issue({ vin: 'unknown', plate: '粤A12345', phone: '13300000001' }))).toEqual({
      key: '粤A12345',
      label: '按车牌查车'
    });
  });

  test('uses phone when VIN and plate are missing', () => {
    expect(qualityIssueVehicleLookup(issue({ vin: 'unknown', phone: '13300000001' }))).toEqual({
      key: '13300000001',
      label: '按手机号查车'
    });
  });
});

describe('isLikelyVIN', () => {
  test('accepts a 17-character VIN', () => {
    expect(isLikelyVIN('LA9GG68L2PBAF4773')).toBe(true);
  });

  test('rejects phone-like lookup keys', () => {
    expect(isLikelyVIN('64646848247')).toBe(false);
  });
});
