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

export type ViolationPaymentStatus = '已缴费' | '未缴费';
export type ViolationProcessedStatus = '已处理' | '未处理';

export interface ViolationRecordRow {
  id: string;
  plateNo: string;
  customerName: string;
  violationTime: string;
  violationLocation: string;
  violationBehavior: string;
  pointsDeducted: number;
  fineAmount: number;
  paymentStatus: ViolationPaymentStatus;
  processed: ViolationProcessedStatus;
  collectionUnit: string;
}

export interface ViolationRecordFilters {
  startDate: string;
  endDate: string;
  customerName: string;
  paymentStatus: string;
  processed: string;
}

export const EMPTY_VIOLATION_RECORD_FILTERS: ViolationRecordFilters = {
  startDate: '',
  endDate: '',
  customerName: '',
  paymentStatus: '',
  processed: '',
};

export const VIOLATION_PAYMENT_STATUS_OPTIONS: ViolationPaymentStatus[] = ['已缴费', '未缴费'];
export const VIOLATION_PROCESSED_OPTIONS: ViolationProcessedStatus[] = ['已处理', '未处理'];

const SEED_VIOLATION_RECORDS = violationRecordsSeed as ViolationRecordRow[];

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

export function resolveVehicleViolationRecords(record: VehicleRecord): ViolationRecordRow[] {
  return SEED_VIOLATION_RECORDS
    .filter((item) => item.plateNo === record.plateNo)
    .sort((a, b) => String(b.violationTime).localeCompare(String(a.violationTime)));
}

export function buildViolationRecordOptions(
  rows: ViolationRecordRow[],
  field: 'customerName' | 'paymentStatus' | 'processed',
): 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 applyViolationRecordFilters(
  rows: ViolationRecordRow[],
  filters: ViolationRecordFilters,
): ViolationRecordRow[] {
  return rows.filter((row) => {
    const date = violationDatePart(row.violationTime);
    if (filters.startDate && date < filters.startDate) return false;
    if (filters.endDate && date > filters.endDate) return false;
    if (filters.customerName && row.customerName !== filters.customerName) return false;
    if (filters.paymentStatus && row.paymentStatus !== filters.paymentStatus) return false;
    if (filters.processed && row.processed !== filters.processed) return false;
    return true;
  });
}

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

export function formatViolationPoints(value: number): string {
  if (value === null || value === undefined || Number.isNaN(value)) return '—';
  return `${value}分`;
}

export function violationPaymentTone(status: ViolationPaymentStatus): 'green' | 'amber' {
  return status === '已缴费' ? 'green' : 'amber';
}

export function violationProcessedTone(status: ViolationProcessedStatus): 'green' | 'amber' {
  return status === '已处理' ? 'green' : 'amber';
}
