import faultRecordsSeed from '../data/fault-records.json';
import type { VehicleRecord } from '../types';
import { isEmpty } from './vehicle';

export type FaultLevel = 'L1-特急' | 'L2-紧急' | 'L3-一般' | 'L4-提示';
export type FaultResolutionStatus = '已解决' | '';

export interface FaultRecordRow {
  id: string;
  plateNo: string;
  faultNo: string;
  resolutionStatus: FaultResolutionStatus;
  brand: string;
  model: string;
  operateCompany: string;
  faultLevel: FaultLevel;
  faultType: string;
  faultDescription: string;
  reportedAt: string;
  resolvedAt: string;
  aiMatched: boolean;
  lastOperator: string;
}

export interface FaultRecordFilters {
  faultNo: string;
  faultLevel: string;
  faultType: string;
  resolutionStatus: string;
  startDate: string;
  endDate: string;
  aiMatched: string;
}

export const EMPTY_FAULT_RECORD_FILTERS: FaultRecordFilters = {
  faultNo: '',
  faultLevel: '',
  faultType: '',
  resolutionStatus: '',
  startDate: '',
  endDate: '',
  aiMatched: '',
};

export const FAULT_LEVEL_OPTIONS: FaultLevel[] = ['L1-特急', 'L2-紧急', 'L3-一般', 'L4-提示'];
export const FAULT_RESOLUTION_OPTIONS = ['已解决', '未解决'] as const;
export const FAULT_AI_MATCH_OPTIONS = ['是', '否'] as const;

const SEED_FAULT_RECORDS = faultRecordsSeed as FaultRecordRow[];

function reportedDatePart(value: string): string {
  if (isEmpty(value)) return '';
  return String(value).slice(0, 10);
}

export function resolveVehicleFaultRecords(record: VehicleRecord): FaultRecordRow[] {
  return SEED_FAULT_RECORDS
    .filter((item) => item.plateNo === record.plateNo)
    .sort((a, b) => String(b.reportedAt).localeCompare(String(a.reportedAt)));
}

export function buildFaultRecordOptions(
  rows: FaultRecordRow[],
  field: 'faultNo' | 'faultLevel' | 'faultType',
): string[] {
  const values = rows
    .map((row) => row[field])
    .filter((value) => Boolean(value));
  return [...new Set(values)].sort((a, b) => a.localeCompare(b, 'zh-CN'));
}

export function applyFaultRecordFilters(
  rows: FaultRecordRow[],
  filters: FaultRecordFilters,
): FaultRecordRow[] {
  return rows.filter((row) => {
    if (filters.faultNo && !row.faultNo.includes(filters.faultNo.trim())) return false;
    if (filters.faultLevel && row.faultLevel !== filters.faultLevel) return false;
    if (filters.faultType && row.faultType !== filters.faultType) return false;
    if (filters.resolutionStatus) {
      const resolved = row.resolutionStatus === '已解决';
      if (filters.resolutionStatus === '已解决' && !resolved) return false;
      if (filters.resolutionStatus === '未解决' && resolved) return false;
    }
    const date = reportedDatePart(row.reportedAt);
    if (filters.startDate && date < filters.startDate) return false;
    if (filters.endDate && date > filters.endDate) return false;
    if (filters.aiMatched) {
      const matched = row.aiMatched ? '是' : '否';
      if (matched !== filters.aiMatched) return false;
    }
    return true;
  });
}

export function formatFaultCell(value: string): string {
  return value || '—';
}

export function faultLevelTone(level: FaultLevel): 'red' | 'amber' | 'blue' | 'gray' {
  if (level === 'L1-特急') return 'red';
  if (level === 'L2-紧急' || level === 'L3-一般') return 'amber';
  return 'blue';
}

export function formatAiMatched(value: boolean): string {
  return value ? '是' : '否';
}

export function aiMatchedTone(value: boolean): 'green' | 'gray' {
  return value ? 'green' : 'gray';
}
