import type { LeaseBusinessDetailRow, LeaseDetailKpiSummary } from '../types';

export type DetailColumnRender =
  | 'payment-status'
  | 'year-month'
  | 'stack-dept'
  | 'vehicle-type'
  | 'payment-method'
  | 'hydrogen-fee';

export interface DetailTableColumn {
  key: string;
  title: string;
  annotationId?: string;
  headerTip?: string;
  money?: boolean;
  className?: string;
  sticky?: 'left' | 'right';
  defaultWidth?: number;
  dateSort?: boolean;
  calculated?: boolean;
  render?: DetailColumnRender;
  /** 表尾汇总对应字段 */
  summaryKey?: keyof LeaseDetailKpiSummary;
  /** 单元格校验字段 */
  checkField?:
    | 'customerName'
    | 'pickupDate'
    | 'returnDate'
    | 'deposit'
    | 'contractRent'
    | 'receivableRent'
    | 'mileageDeduction'
    | 'otherDeduction'
    | 'assetOwner';
}

/** 页面展示列（年月、部门+业务员已合并） */
export const DETAIL_TABLE_COLUMNS: DetailTableColumn[] = [
  {
    key: 'paymentStatus',
    title: '状态',
    render: 'payment-status',
    sticky: 'left',
    defaultWidth: 96,
    annotationId: 'lbd-col-status',
  },
  {
    key: 'yearMonth',
    title: '月份',
    render: 'year-month',
    sticky: 'left',
    className: 'tabular-nums',
    defaultWidth: 88,
    annotationId: 'lbd-col-year-month',
    dateSort: true,
  },
  {
    key: 'businessAssignment',
    title: '业务部门',
    render: 'stack-dept',
    defaultWidth: 120,
    annotationId: 'lbd-col-business-dept',
  },
  { key: 'paymentDate', title: '付款日期', defaultWidth: 108, dateSort: true },
  {
    key: 'plateNo',
    title: '车牌号码',
    defaultWidth: 110,
    sticky: 'left',
    className: 'lbd-col-plate',
    annotationId: 'lbd-col-plate',
  },
  {
    key: 'vehicleType',
    title: '车型',
    render: 'vehicle-type',
    defaultWidth: 120,
    annotationId: 'lbd-col-vehicle-type',
  },
  {
    key: 'customerName',
    title: '客户名称',
    className: 'ldb-col-name',
    defaultWidth: 200,
    annotationId: 'lbd-col-customer',
    checkField: 'customerName',
  },
  { key: 'valueAddedService', title: '增值服务', defaultWidth: 96 },
  { key: 'exemptionPolicy', title: '享免政策', defaultWidth: 120 },
  {
    key: 'pickupDate',
    title: '提车日期',
    defaultWidth: 108,
    dateSort: true,
    annotationId: 'lbd-col-pickup-date',
    checkField: 'pickupDate',
  },
  { key: 'contractStartDate', title: '合同生效日期', defaultWidth: 118, dateSort: true },
  { key: 'contractEndDate', title: '合同到期日期', defaultWidth: 118, dateSort: true },
  { key: 'periodStartDate', title: '起始日期', defaultWidth: 108, dateSort: true },
  {
    key: 'returnDate',
    title: '退车日期',
    defaultWidth: 108,
    dateSort: true,
    annotationId: 'lbd-col-return-date',
    checkField: 'returnDate',
  },
  {
    key: 'avgDays',
    title: '平均天数',
    defaultWidth: 88,
    className: 'tabular-nums',
    annotationId: 'lbd-col-avg-days',
  },
  {
    key: 'deposit',
    title: '押金',
    money: true,
    defaultWidth: 100,
    summaryKey: 'deposit',
    annotationId: 'lbd-col-deposit',
    checkField: 'deposit',
  },
  {
    key: 'contractRent',
    title: '合同标的租金',
    money: true,
    defaultWidth: 110,
    summaryKey: 'contractRent',
    annotationId: 'lbd-col-contract-rent',
    checkField: 'contractRent',
  },
  {
    key: 'receivableTotal',
    title: '应收合计',
    money: true,
    defaultWidth: 100,
    calculated: true,
    summaryKey: 'receivableTotal',
    annotationId: 'lbd-col-receivable-total',
  },
  {
    key: 'receivableRent',
    title: '应收租金',
    money: true,
    defaultWidth: 100,
    summaryKey: 'receivableRent',
    annotationId: 'lbd-col-receivable-rent',
    checkField: 'receivableRent',
  },
  {
    key: 'monthlyIncome',
    title: '月度收入',
    money: true,
    defaultWidth: 100,
    calculated: true,
    summaryKey: 'monthlyIncome',
    annotationId: 'lbd-col-monthly-income',
  },
  {
    key: 'monthlyRent',
    title: '月度租金',
    money: true,
    defaultWidth: 100,
    summaryKey: 'monthlyRent',
    annotationId: 'lbd-col-monthly-rent',
  },
  { key: 'maintenanceIncome', title: '维保包干收入', money: true, defaultWidth: 110, summaryKey: 'maintenanceIncome' },
  { key: 'insuranceSurcharge', title: '保险上浮费', money: true, defaultWidth: 100, summaryKey: 'insuranceSurcharge' },
  { key: 'opsFeeIncome', title: '运维费', money: true, defaultWidth: 90, summaryKey: 'opsFeeIncome' },
  { key: 'otherIncome', title: '其他收入', money: true, defaultWidth: 100, summaryKey: 'otherIncome' },
  {
    key: 'mileageDeduction',
    title: '里程减免金额',
    money: true,
    defaultWidth: 110,
    summaryKey: 'mileageDeduction',
    annotationId: 'lbd-col-mileage-deduction',
    checkField: 'mileageDeduction',
  },
  {
    key: 'otherDeduction',
    title: '其他减免金额',
    money: true,
    defaultWidth: 110,
    summaryKey: 'otherDeduction',
    annotationId: 'lbd-col-other-deduction',
    checkField: 'otherDeduction',
  },
  { key: 'receivedAmount', title: '实收金额', money: true, defaultWidth: 100, summaryKey: 'receivedAmount' },
  {
    key: 'outstanding',
    title: '未收',
    money: true,
    defaultWidth: 90,
    calculated: true,
    summaryKey: 'outstanding',
    annotationId: 'lbd-col-outstanding',
  },
  { key: 'invoiceDate', title: '开票日期', defaultWidth: 108, dateSort: true },
  { key: 'actualPaymentDate', title: '实际付款日期', defaultWidth: 118, dateSort: true },
  {
    key: 'paymentMethod',
    title: '付款方式',
    render: 'payment-method',
    defaultWidth: 108,
    annotationId: 'lbd-col-payment-method',
  },
  { key: 'vehicleStandardCost', title: '车辆标准成本', money: true, defaultWidth: 110, summaryKey: 'vehicleStandardCost' },
  {
    key: 'vehicleActualCost',
    title: '车辆实际成本',
    money: true,
    defaultWidth: 110,
    calculated: true,
    summaryKey: 'vehicleActualCost',
    annotationId: 'lbd-col-vehicle-actual-cost',
  },
  { key: 'insuranceFee', title: '保险费', money: true, defaultWidth: 90, summaryKey: 'insuranceFee' },
  {
    key: 'hydrogenFee',
    title: '氢费',
    money: true,
    defaultWidth: 90,
    summaryKey: 'hydrogenFee',
    render: 'hydrogen-fee',
    annotationId: 'lbd-col-hydrogen-fee',
  },
  { key: 'opsFeeCost', title: '运维费', money: true, defaultWidth: 90, summaryKey: 'opsFeeCost' },
  { key: 'brokerageFee', title: '居间费', money: true, defaultWidth: 90, summaryKey: 'brokerageFee' },
  { key: 'otherCost', title: '其他', money: true, defaultWidth: 90, summaryKey: 'otherCost' },
  {
    key: 'totalCost',
    title: '总成本',
    money: true,
    defaultWidth: 100,
    calculated: true,
    summaryKey: 'totalCost',
    annotationId: 'lbd-col-total-cost',
  },
  {
    key: 'profitLoss',
    title: '盈亏',
    money: true,
    defaultWidth: 90,
    calculated: true,
    summaryKey: 'profitLoss',
    annotationId: 'lbd-col-profit-loss',
  },
  { key: 'gpsMileage', title: 'GPS里程', defaultWidth: 96, className: 'tabular-nums' },
  { key: 'otherDeductionDetail', title: '其他减免金额明细内容', className: 'ldb-col-remark', defaultWidth: 160 },
  {
    key: 'assetOwner',
    title: '资产归属',
    defaultWidth: 140,
    annotationId: 'lbd-col-asset-owner',
    checkField: 'assetOwner',
  },
  {
    key: 'signingCompany',
    title: '签约公司',
    className: 'ldb-col-name',
    defaultWidth: 180,
    annotationId: 'lbd-col-signing-company',
  },
  { key: 'remark', title: '备注', className: 'ldb-col-remark', defaultWidth: 140 },
];

/** Excel 导入/导出列规格（47 列，含独立年份/月份/业务员） */
export const DETAIL_EXPORT_SPECS: { key: keyof LeaseBusinessDetailRow; title: string }[] = [
  { key: 'year', title: '年份' },
  { key: 'month', title: '月份' },
  { key: 'businessDept', title: '业务部门' },
  { key: 'salesperson', title: '业务员' },
  { key: 'paymentDate', title: '付款日期' },
  { key: 'plateNo', title: '车牌号码' },
  { key: 'vehicleType', title: '车型' },
  { key: 'customerName', title: '客户名称' },
  { key: 'valueAddedService', title: '增值服务' },
  { key: 'exemptionPolicy', title: '享免政策' },
  { key: 'pickupDate', title: '提车日期' },
  { key: 'contractStartDate', title: '合同生效日期' },
  { key: 'contractEndDate', title: '合同到期日期' },
  { key: 'periodStartDate', title: '起始日期' },
  { key: 'returnDate', title: '退车日期' },
  { key: 'avgDays', title: '平均天数' },
  { key: 'deposit', title: '押金' },
  { key: 'contractRent', title: '合同标的租金' },
  { key: 'receivableTotal', title: '应收合计' },
  { key: 'receivableRent', title: '应收租金' },
  { key: 'monthlyIncome', title: '月度收入' },
  { key: 'monthlyRent', title: '月度租金' },
  { key: 'maintenanceIncome', title: '维保包干收入' },
  { key: 'insuranceSurcharge', title: '保险上浮费' },
  { key: 'opsFeeIncome', title: '运维费' },
  { key: 'otherIncome', title: '其他收入' },
  { key: 'mileageDeduction', title: '里程减免金额' },
  { key: 'otherDeduction', title: '其他减免金额' },
  { key: 'receivedAmount', title: '实收金额' },
  { key: 'outstanding', title: '未收' },
  { key: 'invoiceDate', title: '开票日期' },
  { key: 'actualPaymentDate', title: '实际付款日期' },
  { key: 'paymentMethod', title: '付款方式' },
  { key: 'vehicleStandardCost', title: '车辆标准成本' },
  { key: 'vehicleActualCost', title: '车辆实际成本' },
  { key: 'insuranceFee', title: '保险费' },
  { key: 'hydrogenFee', title: '氢费' },
  { key: 'opsFeeCost', title: '运维费' },
  { key: 'brokerageFee', title: '居间费' },
  { key: 'otherCost', title: '其他' },
  { key: 'totalCost', title: '总成本' },
  { key: 'profitLoss', title: '盈亏' },
  { key: 'gpsMileage', title: 'GPS里程' },
  { key: 'otherDeductionDetail', title: '其他减免金额明细内容' },
  { key: 'assetOwner', title: '资产归属' },
  { key: 'signingCompany', title: '签约公司' },
  { key: 'remark', title: '备注' },
];

export const DETAIL_EXPORT_HEADERS = DETAIL_EXPORT_SPECS.map((s) => s.title);

export const DEFAULT_DETAIL_COLUMN_WIDTHS: Record<string, number> = Object.fromEntries(
  DETAIL_TABLE_COLUMNS.map((col) => [col.key, col.defaultWidth ?? 100]),
);

export const DETAIL_TABLE_MIN_WIDTH = DETAIL_TABLE_COLUMNS.reduce(
  (sum, col) => sum + (col.defaultWidth ?? 100),
  0,
);

export const DETAIL_SUMMARY_COLUMN_KEYS = new Set(
  DETAIL_TABLE_COLUMNS.filter((c) => c.summaryKey).map((c) => c.summaryKey as string),
);
