import ExcelJS from 'exceljs';

export async function createDocumentedFinancialSample(changeFirstQuantity?: number): Promise<Buffer> {
  const workbook = new ExcelJS.Workbook();
  const sheet = workbook.addWorksheet('Sheet');
  sheet.addRow([
    'شماره پيش فاكتور', 'تاريخ پيش فاكتور', 'كد مشتري', 'عنوان مشتري', 'نوع فروش',
    'كد كالا/خدمت', 'كالا/خدمت', 'واحد اصلي', 'مقدار-اصلي'
  ]);
  let rowIndex = 0;
  for (let invoice = 1; invoice <= 110; invoice += 1) {
    const lineCount = invoice <= 10 ? 5 : invoice <= 20 ? 4 : 1;
    for (let line = 0; line < lineCount; line += 1) {
      rowIndex += 1;
      const product = ((rowIndex - 1) % 19) + 1;
      sheet.addRow([
        String(invoice).padStart(6, '0'), '1405/05/01', String((invoice % 17) + 1).padStart(5, '0'),
        `مشتری نمونه ${invoice}`, 'فروش نقدی', String(product).padStart(4, '0'), `کالای نمونه ${product}`, 'عدد',
        invoice === 1 && line === 0 && changeFirstQuantity !== undefined ? changeFirstQuantity : line + 1
      ]);
    }
  }
  sheet.addRow([249, 249, 249, 249, 249, 249, 249, 249, 187]);
  const data = await workbook.xlsx.writeBuffer();
  return Buffer.from(data);
}

