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

export type MovementStatus = '已完成' | '进行中' | '已取消';

export interface MovementRecordRow {
  id: string;
  plateNo: string;
  startDate: string;
  estimatedEndDate: string;
  status: MovementStatus;
  destinationType: string;
  destinationName: string;
  movementType: string;
  estimatedMileageKm: number;
  startMileageKm: number;
  startBatteryKwh: number;
  startHydrogenPct: number;
  endMileageKm: number;
  endBatteryKwh: number;
  endHydrogenPct: number;
  createdBy: string;
  createdAt: string;
}

export interface MovementRecordFilters {
  startDate: string;
  endDate: string;
  destinationType: string;
  movementType: string;
}

export const EMPTY_MOVEMENT_RECORD_FILTERS: MovementRecordFilters = {
  startDate: '',
  endDate: '',
  destinationType: '',
  movementType: '',
};

export const MOVEMENT_STATUS_OPTIONS: MovementStatus[] = ['已完成', '进行中', '已取消'];

const SEED_MOVEMENT_RECORDS = movementRecordsSeed as MovementRecordRow[];

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

export function resolveVehicleMovementRecords(record: VehicleRecord): MovementRecordRow[] {
  return SEED_MOVEMENT_RECORDS
    .filter((item) => item.plateNo === record.plateNo)
    .sort((a, b) => String(b.startDate).localeCompare(String(a.startDate)));
}

export function buildMovementRecordOptions(
  rows: MovementRecordRow[],
  field: 'destinationType' | 'movementType',
): 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 applyMovementRecordFilters(
  rows: MovementRecordRow[],
  filters: MovementRecordFilters,
): MovementRecordRow[] {
  return rows.filter((row) => {
    const date = movementDatePart(row.startDate);
    if (filters.startDate && date < filters.startDate) return false;
    if (filters.endDate && date > filters.endDate) return false;
    if (filters.destinationType && row.destinationType !== filters.destinationType) return false;
    if (filters.movementType && row.movementType !== filters.movementType) return false;
    return true;
  });
}

export function formatMovementNumber(value: number | null | undefined, digits = 0): string {
  if (value === null || value === undefined || Number.isNaN(value)) return '—';
  return value.toLocaleString('zh-CN', {
    minimumFractionDigits: digits,
    maximumFractionDigits: digits,
  });
}

export function formatMovementPercent(value: number | null | undefined): string {
  if (value === null || value === undefined || Number.isNaN(value)) return '—';
  return `${value.toLocaleString('zh-CN', { minimumFractionDigits: 2, maximumFractionDigits: 2 })} %`;
}

export function movementStatusTone(status: MovementStatus): 'green' | 'amber' | 'gray' {
  if (status === '已完成') return 'green';
  if (status === '进行中') return 'amber';
  return 'gray';
}
