import 'dart:async';

import 'package:flutter/services.dart';
import 'package:intl/intl.dart';
import 'package:pdf/pdf.dart';
import 'package:pdf/widgets.dart' as pw;
import 'package:salespro_admin/PDF/pdf_data.dart';

import '../Screen/tax rates/tax_model.dart';
import '../commas.dart';
import '../const.dart';
import '../model/add_to_cart_model.dart';
import '../model/general_setting_model.dart';
import '../model/personal_information_model.dart';
import '../model/sale_transaction_model.dart';

Future<Uint8List> generate3InchThermalDocument({
  required SaleTransactionModel transactions,
  required PersonalInformationModel personalInformation,
  required GeneralSettingModel generalSetting,
}) async {
  final doc = pw.Document();

  double totalAmount({required SaleTransactionModel transactions}) {
    double amount = 0;
    for (var element in transactions.productList!) {
      amount = amount + double.parse(element.subTotal) * double.parse(element.quantity.toString());
    }
    return double.parse(amount.toStringAsFixed(2));
  }

  final String imageUrl = personalInformation.pictureUrl;
  print('------------image url-------${personalInformation.pictureUrl}--------------');
  dynamic imageData = await PDFCommonFunctions().getNetworkImage(imageUrl);
  imageData ??= await PDFCommonFunctions().loadAssetImage(appLogo);
  // Load the NotoSans font
  final font = await rootBundle.load('fonts/NotoSans/NotoSans-Regular.ttf');
  final boldFont = await rootBundle.load('fonts/NotoSans/NotoSans-SemiBold.ttf');
  final notoSansBold = pw.Font.ttf(boldFont);
  final notoSans = pw.Font.ttf(font);

  // Define styles for thermal printer
  final headerStyle = pw.TextStyle(
    fontSize: 12,
    fontWeight: pw.FontWeight.bold,
    font: notoSansBold,
  );
  // Define styles for thermal printer
  final storeStyle = pw.TextStyle(
    fontSize: 14,
    fontWeight: pw.FontWeight.bold,
    font: notoSansBold,
  );
  final normalStyle = pw.TextStyle(
    fontSize: 9,
    font: notoSans,
  );
  final smallStyle = pw.TextStyle(
    fontSize: 9,
    font: notoSans,
  );
  final boldStyle = pw.TextStyle(
    fontSize: 9,
    fontWeight: pw.FontWeight.bold,
    font: notoSansBold,
  );

  doc.addPage(
    pw.Page(
      pageFormat: PdfPageFormat(80 * PdfPageFormat.mm, double.infinity, marginAll: 3),
      build: (pw.Context context) {
        return pw.Column(
          crossAxisAlignment: pw.CrossAxisAlignment.start,
          children: [
            /// Header Section
            // image section
            if (imageData is Uint8List)
              pw.Center(
                child: pw.Container(
                  height: 54.12,
                  width: 52,
                  child: pw.Image(
                    pw.MemoryImage(imageData),
                    fit: pw.BoxFit.cover,
                  ),
                ),
              )
            else if (imageData is String)
              pw.Center(
                child: pw.Container(
                  height: 54.12,
                  width: 52,
                  child: pw.SvgImage(
                    svg: imageData,
                    fit: pw.BoxFit.cover,
                  ),
                ),
              )
            else
              pw.Center(
                child: pw.Container(
                  height: 54.12,
                  width: 52,
                  child: pw.Image(pw.MemoryImage(imageData)),
                ),
              ),
            pw.SizedBox(width: 20.0),
            pw.Center(
              child: pw.Text(
                personalInformation.companyName,
                style: storeStyle,
                textAlign: pw.TextAlign.center,
              ),
            ),
            pw.SizedBox(height: 2),
            pw.Center(
              child: pw.Text(
                'Mobile: ${personalInformation.phoneNumber}',
                style: normalStyle,
                textAlign: pw.TextAlign.center,
              ),
            ),
            pw.SizedBox(height: 2),
            pw.Center(
              child: pw.Padding(
                padding: pw.EdgeInsets.only(right: 22),
                child: pw.Text(
                  'Address: ${personalInformation.countryName ?? 'n/a'}',
                  style: normalStyle,
                  textAlign: pw.TextAlign.center,
                ),
              ),
            ),
            if (personalInformation.gst.trim().isNotEmpty) pw.SizedBox(height: 2),
            if (personalInformation.gst.trim().isNotEmpty)
              pw.Center(
                child: pw.Text(
                  '${personalInformation.gstOrTinNameType == "" ? 'GST' : personalInformation.gstOrTinNameType}: ${personalInformation.gst}',
                  style: normalStyle,
                  textAlign: pw.TextAlign.center,
                ),
              ),
            pw.SizedBox(height: 6),
            pw.Center(
              child: pw.DecoratedBox(
                  decoration: pw.BoxDecoration(
                    border: pw.Border(
                      bottom: pw.BorderSide(color: PdfColors.black),
                    ),
                  ),
                  child: pw.Text(
                    'INVOICE',
                    style: headerStyle,
                  )),
            ),
            pw.SizedBox(height: 14),
            // Invoice Info
            pw.Row(
              crossAxisAlignment: pw.CrossAxisAlignment.end,
              mainAxisAlignment: pw.MainAxisAlignment.end,
              children: [
                pw.Expanded(
                  child: buildInfo(
                    title: 'Invoice',
                    value: transactions.invoiceNumber,
                    normalFont: notoSans,
                    boldFont: notoSans,
                  ),
                ),
                // pw.SizedBox(width: 25),
                pw.Expanded(
                  child: buildInfoEnd(
                    title: 'Date',
                    value: DateFormat.yMd().format(DateTime.parse(transactions.purchaseDate)),
                    normalFont: notoSans,
                    boldFont: notoSans,
                  ),
                ),
                pw.SizedBox(width: 22),
              ],
            ),
            pw.SizedBox(height: 2),
            pw.Row(
              crossAxisAlignment: pw.CrossAxisAlignment.end,
              mainAxisAlignment: pw.MainAxisAlignment.end,
              children: [
                pw.Expanded(
                  child: buildInfo(
                    title: 'Name',
                    value: transactions.customerName,
                    normalFont: notoSans,
                    boldFont: notoSans,
                  ),
                ),
                pw.Expanded(
                  child: buildInfoEnd(
                    title: 'Time',
                    value: DateFormat.jm().format(DateTime.parse(transactions.purchaseDate)),
                    normalFont: notoSans,
                    boldFont: notoSans,
                  ),
                ),
                pw.SizedBox(width: 22),
              ],
            ),
            pw.SizedBox(height: 2),
            pw.Row(
              crossAxisAlignment: pw.CrossAxisAlignment.end,
              mainAxisAlignment: pw.MainAxisAlignment.end,
              children: [
                pw.Expanded(
                  child: buildInfo(
                    title: 'Mobile',
                    value: transactions.customerPhone,
                    normalFont: notoSans,
                    boldFont: notoSans,
                  ),
                ),
                pw.Expanded(
                  child: pw.Text('Sales By: ${'Admin'}', style: normalStyle, textAlign: pw.TextAlign.end),
                  // child: buildInfo(
                  //   title: 'Sales By',
                  //   value: 'Admin',
                  //   normalFont: notoSans,
                  //   boldFont: notoSans,
                  // ),
                ),
                pw.SizedBox(width: 22),
              ],
            ),
            pw.SizedBox(height: 12),
            buildDottedLine(),
            pw.Padding(
              padding: pw.EdgeInsets.symmetric(vertical: 4),
              child: pw.Row(children: [
                pw.Container(
                  margin: pw.EdgeInsets.zero,
                  padding: pw.EdgeInsets.zero,
                  width: 20,
                  alignment: pw.Alignment.centerLeft,
                  // padding: const pw.EdgeInsets.all(1),
                  child: pw.Text('SL', style: boldStyle),
                ),
                pw.Container(
                  width: 75,
                  alignment: pw.Alignment.centerLeft,
                  // padding: const pw.EdgeInsets.all(1),
                  child: pw.Text('Item', style: boldStyle),
                ),
                pw.Container(
                  width: 27,
                  alignment: pw.Alignment.center,
                  // padding: const pw.EdgeInsets.all(1),
                  child: pw.Text('Qty', style: boldStyle),
                ),
                pw.Container(
                  width: 32,
                  alignment: pw.Alignment.center,
                  // padding: const pw.EdgeInsets.all(1),
                  child: pw.Text('Price', style: boldStyle),
                ),
                pw.Container(
                  width: 45,
                  alignment: pw.Alignment.centerRight,
                  // padding: const pw.EdgeInsets.all(1),
                  child: pw.Text('Amount', style: boldStyle),
                ),
              ]),
            ),
            buildDottedLine(),
            pw.SizedBox(height: 2),
            pw.Column(
              children: List.generate(
                transactions.productList?.length ?? 0,
                (i) {
                  final product = transactions.productList![i];
                  return pw.Padding(
                    padding: const pw.EdgeInsets.symmetric(vertical: 1),
                    child: pw.Row(
                      crossAxisAlignment: pw.CrossAxisAlignment.start,
                      children: [
                        pw.Container(
                          margin: pw.EdgeInsets.zero,
                          padding: pw.EdgeInsets.zero,
                          width: 20,
                          alignment: pw.Alignment.centerLeft,
                          // padding: const pw.EdgeInsets.all(1),
                          child: pw.Text('${i + 1}', style: smallStyle),
                        ),
                        pw.Container(
                          width: 75,
                          alignment: pw.Alignment.centerLeft,
                          // padding: const pw.EdgeInsets.all(1),
                          child: pw.Text(
                            product.productName ?? '',
                            style: smallStyle,
                          ),
                        ),
                        pw.Container(
                          width: 27,
                          alignment: pw.Alignment.center,
                          // padding: const pw.EdgeInsets.all(1),
                          child: pw.Text(
                            '${product.quantity ?? 0}',
                            style: smallStyle,
                          ),
                        ),
                        pw.Container(
                          width: 32,
                          alignment: pw.Alignment.center,
                          // padding: const pw.EdgeInsets.all(1),
                          child: pw.Text(
                            (myFormat.format(double.tryParse(transactions.productList!.elementAt(i).subTotal.toString()) ?? 0)),
                            style: smallStyle,
                          ),
                        ),
                        pw.Container(
                          width: 45,
                          alignment: pw.Alignment.centerRight,
                          child: pw.Text(
                            myFormat.format(double.parse(transactions.productList!.elementAt(i).subTotal) * transactions.productList!.elementAt(i).quantity.toDouble()),
                            style: smallStyle,
                          ),
                        ),
                      ],
                    ),
                  );
                },
              ),
            ),
            pw.SizedBox(height: 2),
            // pw.Column(
            //     children: List.generate(transactions.productList!.length, (i) {
            //   return pw.Padding(
            //       padding: pw.EdgeInsets.symmetric(vertical: 2),
            //       child: pw.Row(children: [
            //         pw.Container(
            //           width: 20,
            //           alignment: pw.Alignment.center,
            //           padding: const pw.EdgeInsets.all(1),
            //           child: pw.Text('${i + 1}', style: smallStyle),
            //         ),
            //         pw.Container(
            //           width: 65,
            //           alignment: pw.Alignment.centerLeft,
            //           padding: const pw.EdgeInsets.all(1),
            //           child: pw.Text(
            //             _shorten(transactions.productList![i].productName ?? '', maxChars: 18),
            //             style: smallStyle,
            //           ),
            //         ),
            //         pw.Container(
            //           width: 25,
            //           alignment: pw.Alignment.center,
            //           padding: const pw.EdgeInsets.all(1),
            //           child: pw.Text(
            //             transactions.productList![i].quantity.toString(),
            //             style: smallStyle,
            //           ),
            //         ),
            //         pw.Container(
            //           width: 45,
            //           alignment: pw.Alignment.centerRight,
            //           padding: const pw.EdgeInsets.all(1),
            //           child: pw.Text(
            //             myFormat.format(double.tryParse(transactions.productList![i].unitPrice) ?? 0),
            //             style: smallStyle,
            //           ),
            //         ),
            //         pw.Container(
            //           width: 50,
            //           alignment: pw.Alignment.centerRight,
            //           padding: const pw.EdgeInsets.all(1),
            //           child: pw.Text(
            //             myFormat.format(double.tryParse(transactions.productList![i].subTotal) ?? 0),
            //             style: smallStyle,
            //           ),
            //         ),
            //       ]));
            // })),
            buildDottedLine(),
            pw.SizedBox(height: 6),
            // Summary Section
            pw.Align(
              alignment: pw.Alignment.centerRight,
              child: pw.SizedBox(
                  width: 160,
                  child: pw.Column(crossAxisAlignment: pw.CrossAxisAlignment.end, mainAxisAlignment: pw.MainAxisAlignment.end, children: [
                    pw.Row(
                      mainAxisAlignment: pw.MainAxisAlignment.end,
                      crossAxisAlignment: pw.CrossAxisAlignment.end,
                      children: [
                        pw.Expanded(
                          flex: 2,
                          child: pw.Text(
                            'Sub-Total : ',
                            style: normalStyle,
                            textAlign: pw.TextAlign.end,
                          ),
                        ),
                        pw.Expanded(
                          child: pw.Text(
                            myFormat.format(totalAmount(transactions: transactions)),
                            style: normalStyle,
                            textAlign: pw.TextAlign.end,
                          ),
                        ),
                        pw.SizedBox(width: 22),
                      ],
                    ),
                    pw.SizedBox(height: 4),
                    // if (transactions.discountAmount != null && transactions.discountAmount != 0)
                    pw.Row(
                      mainAxisAlignment: pw.MainAxisAlignment.end,
                      crossAxisAlignment: pw.CrossAxisAlignment.end,
                      children: [
                        pw.Expanded(
                          flex: 2,
                          child: pw.Text(
                            'Discount : ',
                            style: normalStyle,
                            textAlign: pw.TextAlign.end,
                          ),
                        ),
                        pw.Expanded(
                          child: pw.Text(
                            myFormat.format(double.tryParse(transactions.discountAmount.toString()) ?? 0),
                            style: normalStyle,
                            textAlign: pw.TextAlign.end,
                          ),
                        ),
                        pw.SizedBox(width: 22),
                      ],
                    ),
                    pw.SizedBox(height: 4),

                    pw.Row(
                      mainAxisAlignment: pw.MainAxisAlignment.end,
                      crossAxisAlignment: pw.CrossAxisAlignment.end,
                      children: [
                        pw.Expanded(
                          flex: 2,
                          child: pw.Text(
                            'Total Vat ${calculateTotalTaxRate(cart: transactions.productList ?? [])}% : ',
                            style: normalStyle,
                            textAlign: pw.TextAlign.end,
                          ),
                        ),
                        pw.Expanded(
                          child: pw.Text(
                            calculateTotalVat(products: transactions.productList ?? []).toString(),
                            style: normalStyle,
                            textAlign: pw.TextAlign.end,
                          ),
                        ),
                        pw.SizedBox(width: 22),
                      ],
                    ),
                    pw.SizedBox(height: 4),
                    // Tax breakdown
                    // for (final tax in getAllTaxFromCartList(cart: transactions.productList ?? []))
                    //   pw.Column(children: [
                    //     pw.Row(
                    //       mainAxisAlignment: pw.MainAxisAlignment.end,
                    //       crossAxisAlignment: pw.CrossAxisAlignment.end,
                    //       children: [
                    //         // (myFormat.format(double.tryParse(transactions.productList!.elementAt(i).subTotal.toString()) ?? 0)),
                    //         // (calculateProductVat(product: transactions.productList!.elementAt(i))),
                    //         // (myFormat.format(double.tryParse(
                    //         //     (double.parse(transactions.productList!.elementAt(i).subTotal) * transactions.productList!.elementAt(i).quantity.toInt()).toStringAsFixed(2)) ??
                    //         //     0))
                    //         pw.Expanded(
                    //           flex: 2,
                    //           child: pw.Text(
                    //             '${tax.name} ${tax.taxRate}% :',
                    //             style: normalStyle,
                    //             textAlign: pw.TextAlign.end,
                    //           ),
                    //         ),
                    //         pw.Expanded(
                    //           child: pw.Text(
                    //             myFormat.format(
                    //               calculateThermalVat(tax: tax, products: transactions.productList ?? []),
                    //             ),
                    //             style: normalStyle,
                    //             textAlign: pw.TextAlign.end,
                    //           ),
                    //         ),
                    //         pw.SizedBox(width: 22),
                    //         // pw.Text(myFormat.format(tax.taxAmount), style: normalStyle),
                    //       ],
                    //     ),
                    //     pw.SizedBox(height: 4),
                    //   ]),
                    pw.Row(
                      mainAxisAlignment: pw.MainAxisAlignment.end,
                      crossAxisAlignment: pw.CrossAxisAlignment.end,
                      children: [
                        pw.Expanded(
                          flex: 2,
                          child: pw.Text(
                            'Shipping Charge : ',
                            style: normalStyle,
                            textAlign: pw.TextAlign.end,
                          ),
                        ),
                        pw.Expanded(
                          child: pw.Text(
                            myFormat.format(double.tryParse(transactions.serviceCharge.toString()) ?? 0),
                            style: normalStyle,
                            textAlign: pw.TextAlign.end,
                          ),
                        ),
                        pw.SizedBox(width: 22),
                      ],
                    ),
                    pw.SizedBox(height: 4),
                    buildDottedLine(),
                    pw.SizedBox(height: 4),
                    pw.Row(mainAxisAlignment: pw.MainAxisAlignment.end, crossAxisAlignment: pw.CrossAxisAlignment.end, children: [
                      pw.Expanded(
                        flex: 2,
                        child: pw.Text(
                          'Total Payable : ',
                          style: boldStyle,
                          textAlign: pw.TextAlign.end,
                        ),
                      ),
                      pw.Expanded(
                        child: pw.Text(
                          myFormat.format(double.tryParse(transactions.totalAmount.toString()) ?? 0),
                          style: boldStyle,
                          textAlign: pw.TextAlign.end,
                        ),
                      ),
                      pw.SizedBox(width: 22),
                    ]),
                    pw.SizedBox(height: 4),
                    pw.Row(
                      mainAxisAlignment: pw.MainAxisAlignment.end,
                      crossAxisAlignment: pw.CrossAxisAlignment.end,
                      children: [
                        pw.Expanded(
                          flex: 2,
                          child: pw.Text(
                            'Paid Amount : ',
                            style: normalStyle,
                            textAlign: pw.TextAlign.end,
                          ),
                        ),
                        pw.Expanded(
                          child: pw.Text(
                            myFormat.format(double.tryParse((transactions.totalAmount! - transactions.dueAmount!).toString()) ?? 0),
                            style: normalStyle,
                            textAlign: pw.TextAlign.end,
                          ),
                        ),
                        pw.SizedBox(width: 22),
                      ],
                    ),
                    pw.SizedBox(height: 4),
                    pw.Row(
                      mainAxisAlignment: pw.MainAxisAlignment.end,
                      crossAxisAlignment: pw.CrossAxisAlignment.end,
                      children: [
                        pw.Expanded(
                          flex: 2,
                          child: pw.Align(
                            alignment: pw.Alignment.centerRight,
                            child: pw.Text(
                              'Due Amount : ',
                              style: normalStyle,
                              textAlign: pw.TextAlign.end,
                            ),
                          ),
                        ),
                        pw.Expanded(
                          child: pw.Text(
                            myFormat.format(double.tryParse(transactions.dueAmount!.toString()) ?? 0),
                            style: normalStyle,
                            textAlign: pw.TextAlign.end,
                          ),
                        ),
                        pw.SizedBox(width: 22),
                      ],
                    ),
                    pw.SizedBox(height: 4),
                  ])),
            ),
            buildDottedLine(),
            pw.SizedBox(height: 4),
            // Footer
            // pw.Row(
            //   mainAxisAlignment: pw.MainAxisAlignment.spaceAround,
            //   children: [
            //     pw.Column(
            //       children: [
            //         pw.Text('Customer Signature', style: smallStyle),
            //         pw.SizedBox(height: 15),
            //         pw.Text('___________________', style: smallStyle),
            //       ],
            //     ),
            //     pw.Column(
            //       children: [
            //         pw.Text('Authorized Signature', style: smallStyle),
            //         pw.SizedBox(height: 15),
            //         pw.Text('___________________', style: smallStyle),
            //       ],
            //     ),
            //   ],
            // ),
            // pw.SizedBox(height: 5),
            // pw.Center(
            //   child: pw.Text(
            //     'Powered By ${generalSetting.companyName.isNotEmpty ? generalSetting.companyName : 'YourAppName'}',
            //     style: pw.TextStyle(fontSize: 7, color: PdfColors.black),
            //   ),
            // ),
            buildInfo(
              title: 'Payment Type',
              value: transactions.paymentType.toString(),
              normalFont: notoSans,
              boldFont: notoSansBold,
            ),
            pw.SizedBox(height: 14),
            // pw.Padding(
            //   padding: pw.EdgeInsets.only(right: 22),
            //   child: pw.Text(
            //     'Note : Goods once sold will not be taken back or exchanged',
            //     textAlign: pw.TextAlign.center,
            //     style: normalStyle,
            //   ),
            // ),
            if (personalInformation.invoiceNote.toString().isNotEmpty)
              pw.Padding(
                padding: pw.EdgeInsets.only(right: 22),
                child: buildInfo(
                  title: 'Note',
                  value: personalInformation.invoiceNote.toString(),
                  normalFont: notoSansBold,
                  boldFont: notoSans,
                ),
              ),
            pw.SizedBox(height: 6),
            pw.Center(
              child: pw.BarcodeWidget(
                barcode: pw.Barcode.qrCode(),
                data: 'https://acnoo.com',
                // width: 65,
                // height: 49,
                width: 55,
                height: 39,
              ),
            ),
            pw.SizedBox(height: 6),
            pw.Center(
              child: pw.Text(
                'Developed by acnoo.com',
                style: normalStyle,
                textAlign: pw.TextAlign.center,
              ),
            ),
            // pw.SizedBox(height: 6),
          ],
        );
      },
    ),
  );

  return doc.save();
}

double calculateTotalVat({required List<AddToCartModel> products}) {
  double totalVat = 0;

  for (var product in products) {
    totalVat += double.tryParse(calculateProductVat(product: product)) ?? 0;
  }

  return double.parse(totalVat.toStringAsFixed(2)); // Round to 2 decimal places
}

pw.Widget buildInfo({required String title, required String value, required pw.Font normalFont, required pw.Font boldFont}) {
  final normalStyle = pw.TextStyle(fontSize: 9);
  final boldStyle = pw.TextStyle(fontWeight: pw.FontWeight.bold, fontSize: 9);
  return pw.RichText(
    text: pw.TextSpan(
      baseline: 1,
      text: '$title: ',
      style: normalStyle.copyWith(font: normalFont),
      children: [
        pw.TextSpan(text: value, style: boldStyle.copyWith(font: boldFont)),
      ],
    ),
  );
}

//----------calculate vat rate--------------------
double calculateTotalTaxRate({required List<AddToCartModel> cart}) {
  List<TaxModel> uniqueTaxes = getAllTaxFromCartList(cart: cart);

  double totalRate = 0;
  for (var tax in uniqueTaxes) {
    totalRate += tax.taxRate;
  }

  return double.parse(totalRate.toStringAsFixed(2)); // Rounded to 2 decimal places
}

//----------calculate total vat-------------------

double calculateThermalVat({
  required TaxModel tax,
  required List<AddToCartModel> products,
}) {
  double totalVat = 0;

  for (final product in products) {
    // Match product's tax rate and name with the given tax
    if (product.groupTaxRate == tax.taxRate && product.taxType == tax.name) {
      double price = double.tryParse(product.productPurchasePrice.toString()) ?? 0;
      double taxRate = product.groupTaxRate / 100;
      double quantity = product.quantity.toDouble() ?? 1;
      double vat = 0;

      if (product.taxType.toLowerCase() == 'inclusive') {
        double netPrice = price / (1 + taxRate);
        double taxPerUnit = price - netPrice;
        vat = taxPerUnit * quantity;
      } else {
        vat = price * taxRate * quantity;
      }

      totalVat += vat;
    }
  }

  return totalVat;
}

pw.Widget buildInfoEnd({
  required String title,
  required String value,
  required pw.Font normalFont,
  required pw.Font boldFont,
}) {
  final normalStyle = pw.TextStyle(fontSize: 9);
  final boldStyle = pw.TextStyle(fontWeight: pw.FontWeight.bold, fontSize: 9);
  return pw.RichText(
    textAlign: pw.TextAlign.end,
    text: pw.TextSpan(
      baseline: 1,
      text: '$title: ',
      style: normalStyle.copyWith(font: normalFont),
      children: [
        pw.TextSpan(text: value, style: boldStyle.copyWith(font: boldFont)),
      ],
    ),
  );
}

pw.Widget buildDottedLine() {
  return pw.SizedBox(
    width: 250,
    height: 1,
    child: pw.Stack(
      children: [
        pw.Positioned(
          top: 0,
          left: 0,
          child: pw.Container(
            width: 250,
            height: 1,
            child: pw.LayoutBuilder(
              builder: (context, constraints) {
                final totalWidth = constraints!.maxWidth;
                const dotWidth = 2.5;
                const space = 2.0;
                final dotCount = (totalWidth / (dotWidth + space)).floor();

                return pw.Row(
                  children: List.generate(dotCount, (index) {
                    return pw.Container(
                      width: dotWidth,
                      height: 0.5,
                      color: PdfColors.black,
                      margin: const pw.EdgeInsets.only(right: space),
                    );
                  }),
                );
              },
            ),
          ),
        ),
      ],
    ),
  );

// pw.Widget buildDottedLine() {
//   return pw.Row(
//     children: List.generate(80, (index) {
//       return pw.Container(
//         height: 0.5,
//         width: 2.5,
//         color: PdfColor.fromInt(0xff000000), // Corrected color code
//         margin: const pw.EdgeInsets.only(right: 2), // Add spacing between dots
//       );
//     }),
//   );
// }
}
