import 'package:pweb/models/payment/multiple_payouts/csv_row.dart';

const String sampleFileName = 'sample.csv';

final List<CsvPayoutRow> sampleRows = [
  CsvPayoutRow(
    pan: "9022****11",
    firstName: "Alex",
    lastName: "Ivanov",
    expMonth: 12,
    expYear: 27,
    amount: "500",
    comment: "Salary payout",
  ),
  CsvPayoutRow(
    pan: "9022****12",
    firstName: "Maria",
    lastName: "Sokolova",
    expMonth: 7,
    expYear: 26,
    amount: "100",
  ),
  CsvPayoutRow(
    pan: "9022****13",
    firstName: "Dmitry",
    lastName: "Smirnov",
    expMonth: 3,
    expYear: 28,
    amount: "120",
    comment: "Refund",
  ),
];

String buildSampleCsvContent() {
  final rows = <String>[
    'pan,first_name,last_name,exp_month,exp_year,amount,comment',
    ...sampleRows.map(
      (row) =>
          '${row.pan},${row.firstName},${row.lastName},${row.expMonth},${row.expYear},${row.amount},${_escapeCsvCell(row.comment)}',
    ),
  ];
  return rows.join('\n');
}

String _escapeCsvCell(String? value) {
  if (value == null || value.isEmpty) return '';
  if (!value.contains(',') && !value.contains('"') && !value.contains('\n')) {
    return value;
  }
  final escaped = value.replaceAll('"', '""');
  return '"$escaped"';
}
