import leaseRecordsSeed from '../data/lease-records.json';
import type { VehicleRecord } from '../types';
import { formatDateYmd, isEmpty } from './vehicle';

export type LeaseBusinessType = '租赁' | '自营';

export interface LeaseRecordRow {
  id: string;
  plateNo: string;
  contractNo: string;
  projectName: string;
  customerName: string;
  businessType: LeaseBusinessType;
  deliveryDate: string;
  deliveryPerson: string;
  returnDate: string;
  returnPerson: string;
}

export interface LeaseRecordFilters {
  contractNo: string;
  projectName: string;
  customerName: string;
  businessType: string;
}

export const EMPTY_LEASE_RECORD_FILTERS: LeaseRecordFilters = {
  contractNo: '',
  projectName: '',
  customerName: '',
  businessType: '',
};

const SEED_LEASE_RECORDS = leaseRecordsSeed as LeaseRecordRow[];

function normalizeField(value: string): string {
  if (isEmpty(value) || value === '-') return '';
  return String(value).trim();
}

function resolveBusinessType(record: VehicleRecord): LeaseBusinessType {
  if (record.operateStatus === '自营') return '自营';
  return '租赁';
}

function buildDerivedLeaseRecord(record: VehicleRecord): LeaseRecordRow | null {
  const contractNo = normalizeField(record.contractNo);
  const projectName = normalizeField(record.projectName);
  const customerName = normalizeField(record.customer);
  const deliveryDate = formatDateYmd(record.lastDeliveryTime || '');
  const deliveryPerson = normalizeField(record.lastDeliveryPerson || '');
  const returnDate = formatDateYmd(record.lastReturnTime || '');
  const returnPerson = normalizeField(record.lastReturnPerson || '');

  if (!contractNo && !projectName && !customerName && !deliveryDate) {
    return null;
  }

  return {
    id: `derived-${record.id}`,
    plateNo: record.plateNo,
    contractNo,
    projectName,
    customerName,
    businessType: resolveBusinessType(record),
    deliveryDate,
    deliveryPerson,
    returnDate,
    returnPerson,
  };
}

export function resolveVehicleLeaseRecords(record: VehicleRecord): LeaseRecordRow[] {
  const seeded = SEED_LEASE_RECORDS.filter((item) => item.plateNo === record.plateNo);
  if (seeded.length) return seeded;

  const derived = buildDerivedLeaseRecord(record);
  return derived ? [derived] : [];
}

export function buildLeaseRecordOptions(rows: LeaseRecordRow[], field: keyof LeaseRecordFilters): string[] {
  const values = rows
    .map((row) => {
      if (field === 'businessType') return row.businessType;
      return row[field];
    })
    .filter((value) => Boolean(value));
  return [...new Set(values)].sort((a, b) => a.localeCompare(b, 'zh-CN'));
}

export function applyLeaseRecordFilters(
  rows: LeaseRecordRow[],
  filters: LeaseRecordFilters,
): LeaseRecordRow[] {
  return rows.filter((row) => {
    if (filters.contractNo && row.contractNo !== filters.contractNo) return false;
    if (filters.projectName && row.projectName !== filters.projectName) return false;
    if (filters.customerName && row.customerName !== filters.customerName) return false;
    if (filters.businessType && row.businessType !== filters.businessType) return false;
    return true;
  });
}

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

export function isLeaseRecordLinkable(value: string): boolean {
  return Boolean(value) && value !== '—';
}
