import { getCurrencySymbol } from "src/common/constant/currency.constants";

export class EstimateMapper {
  static toPdf(estimate: any ,  attachments: any[] = []) {
    const currencyCode =
      estimate.currency ?? estimate.userId?.currency ?? "USD";
      const baseUrl = process.env.BACKEND_URL || "http://localhost:5001";
    const currencySymbol = getCurrencySymbol(currencyCode);
    return {
      EstimateNo: estimate.estimateNo,
      date: new Date(estimate.createdAt).toDateString(),
      dueDate: new Date(estimate.expiryDate).toDateString(),
      status: estimate.status,
      taxType: estimate.taxType,

      // ================= CLIENT =================
      client: {
        name: estimate.clientId?.name ,
        email: estimate.clientId?.email ,
        phone: estimate.clientId?.phone ,
      },


       attachments: attachments.map(a => ({
      url: `${baseUrl}/${a.fileUrl.replace(/\\/g, "/")}`,
      type: a.fileType,
      name: a.fileName,
    })),

      //=============USER =================

      user: {
        companyName: estimate.userId?.organizationName ,
        busniessNo: estimate.userId?.businessNumber ,
        licenseNumber:estimate.userId?.licenseNumber,
        email: estimate.userId?.businessEmail,
        phone: estimate.userId?.phoneNumber ,
        address: estimate.userId?.address ,
        city: estimate.userId?.city ,
        state: estimate.userId?.state ,
        zipCode: estimate.userId?.zipCode ,
        country: estimate.userId?.country ,
        currency: estimate.userId?.currency ,
        currencySymbol,
        terms:estimate.userId?.terms,
        profileImage: estimate.userId?.profileImageUrl || null,
      },

      // ================= ITEMS =================
      items: estimate.items.map((item) => ({
        name: item.name,
        description: item.description || "",
        quantity: item.quantity,
        price: item.price,
        baseAmount: item.baseAmount,
        discountAmount: item.discountAmount,
        taxPercentage: item.taxPercentage,
        taxAmount: item.taxAmount,
        lineTotal: item.lineTotal,
        isTaxable: item.isTaxable,
      })),

      // ================= SUMMARY =================
      summary: {
        subTotal: estimate.subTotal,
        estimateDiscountAmount: estimate.discountAmount,
        taxTotal: estimate.taxAmount,
        grandTotal: estimate.total,
      },
    };
  }
}
