export const documentTypes = [
    { id: "booking_confirmation", name: "Standard Booking Confirmation" },
    { id: "premium_allotment", name: "Premium Allotment Letter" },
    { id: "lease_agreement", name: "Standard Lease Agreement" },
    { id: "rent_agreement", name: "Rent Agreement" },
    { id: "commercial_agreement", name: "Commercial Property Agreement" },
    { id: "payment_receipt", name: "Payment Receipt Template" },
    { id: "sale_deed", name: "Sale Deed" },
  ]


  export enum PAPER_SIZE {
    A4 = 'A4 (210mm × 297mm)',
    A3 = 'A3 (297mm × 420mm)',
    LETTER = 'Letter (8.5in × 11in)',
    LEGAL = 'Legal (8.5in × 14in)',
  }
  
  // Paper sizes
  export const paperSizes = [
    { id: "a4", name: PAPER_SIZE.A4 },
    { id: "letter", name: PAPER_SIZE.LETTER },
    { id: "legal", name: PAPER_SIZE.LEGAL },
  ];

export const paperSizeIdToLabel = (paperSizeId?: string) => {
  return paperSizes.find((p) => p.id === paperSizeId)?.name || paperSizeId;
};

export const paperSizeLabelToId = (paperSizeLabel?: string) => {
  return paperSizes.find((p) => p.name === paperSizeLabel)?.id || paperSizeLabel;
};

export const sampleProjects = [
    { id: "1", name: "Green Valley Residences" },
    { id: "2", name: "Lakeside Apartments" },
    { id: "3", name: "Urban Heights" },
    { id: "4", name: "Sunset Villas" },
    { id: "5", name: "Metro Business Park" },
  ]
  
  // Sample document templates data
export const sampleDocuments = [
    {
      id: "1",
      name: "Standard Booking Confirmation",
      status: "published",
      updatedAt: new Date(2023, 10, 15),
      updatedBy: {
        name: "Rajesh Kumar",
        avatar: "RK",
        seed: "Rajesh",
      },
      type: "booking_confirmation",
      paperSize: "a4",
      projectAvailability: "all",
      specificProjects: [],
      content: `Dear {{customer_name}},
  
  We are pleased to confirm your booking for {{property_name}} at {{project_name}}. 
  
  Booking Details:
  - Property ID: {{property_id}}
  - Unit Type: {{unit_type}}
  - Area: {{area}} sq. ft.
  - Basic Price: ₹{{basic_price}}
  - Booking Amount Received: ₹{{booking_amount}}
  
  Please note that this booking is subject to the terms and conditions mentioned in the agreement. The balance payment schedule is as follows:
  
  {{payment_schedule}}
  
  For any queries, please contact our customer service at {{customer_service_number}}.
  
  Thank you for choosing {{company_name}}.
  
  Warm Regards,
  {{sales_manager_name}}
  Sales Manager`,
    },
    {
      id: "2",
      name: "Premium Allotment Letter",
      status: "draft",
      updatedAt: new Date(2023, 11, 5),
      updatedBy: {
        name: "Priya Sharma",
        avatar: "PS",
        seed: "Priya",
      },
      type: "premium_allotment",
      paperSize: "a4",
      projectAvailability: "specific",
      specificProjects: ["1", "3"],
      content: `ALLOTMENT LETTER
  
  Date: {{current_date}}
  
  To,
  {{customer_name}}
  {{customer_address}}
  
  Subject: Allotment of Unit No. {{unit_number}} in {{project_name}}
  
  Dear {{customer_name}},
  
  With reference to your application dated {{application_date}} and subsequent payment of booking amount, we are pleased to inform you that Unit No. {{unit_number}} in {{project_name}} has been allotted to you.
  
  Details of Allotment:
  - Unit Number: {{unit_number}}
  - Floor: {{floor}}
  - Tower/Block: {{tower}}
  - Super Built-up Area: {{super_area}} sq. ft.
  - Carpet Area: {{carpet_area}} sq. ft.
  - Basic Sale Price: ₹{{basic_price}}
  
  This allotment is subject to the following terms and conditions:
  1. The allotment is provisional and subject to execution of the Agreement to Sell.
  2. The payment plan selected by you is {{payment_plan}}.
  3. All payments should be made as per the payment schedule.
  4. Possession will be handed over as per the terms mentioned in the Agreement to Sell.
  
  Please sign and return the duplicate copy of this letter as a token of your acceptance.
  
  Yours sincerely,
  For {{company_name}}
  
  {{authorized_signatory}}
  Authorized Signatory`,
    },
    {
      id: "3",
      name: "Standard Lease Agreement",
      status: "unpublished",
      updatedAt: new Date(2023, 9, 20),
      updatedBy: {
        name: "Vikram Singh",
        avatar: "VS",
        seed: "Vikram",
      },
      type: "lease_agreement",
      paperSize: "legal",
      projectAvailability: "all",
      specificProjects: [],
      content: `LEASE AGREEMENT
  
  THIS LEASE AGREEMENT is made and executed on this {{agreement_date}} at {{city}} by and between:
  
  {{owner_name}}, residing at {{owner_address}} (hereinafter referred to as the "LESSOR")
  
  AND
  
  {{tenant_name}}, residing at {{tenant_address}} (hereinafter referred to as the "LESSEE")
  
  WHEREAS the Lessor is the absolute owner of the property bearing No. {{property_number}} situated at {{property_address}} (hereinafter referred to as the "PREMISES").
  
  AND WHEREAS the Lessee has approached the Lessor to take the Premises on lease for a period of {{lease_period}} months/years commencing from {{lease_start_date}} and ending on {{lease_end_date}}.
  
  NOW THIS AGREEMENT WITNESSETH AS FOLLOWS:
  
  1. RENT: The Lessee shall pay a monthly rent of ₹{{monthly_rent}} (Rupees {{rent_in_words}} only) to the Lessor on or before the {{rent_due_date}} day of each English calendar month.
  
  2. SECURITY DEPOSIT: The Lessee has paid a sum of ₹{{security_deposit}} (Rupees {{deposit_in_words}} only) as interest-free refundable security deposit.
  
  3. TERM: The lease shall be for a period of {{lease_period}} months/years commencing from {{lease_start_date}}.
  
  4. MAINTENANCE: The Lessee shall pay a monthly maintenance charge of ₹{{maintenance_charge}} to the {{maintenance_authority}}.
  
  5. UTILITIES: The Lessee shall pay for all utility charges such as electricity, water, gas, etc. as per actual consumption.
  
  6. TERMINATION: Either party may terminate this agreement by giving {{notice_period}} months' notice in writing.
  
  IN WITNESS WHEREOF, the parties hereto have set their hands on the day, month and year first above written.
  
  LESSOR                                LESSEE
  {{owner_name}}                        {{tenant_name}}
  
  WITNESSES:
  1. {{witness1_name}}
  2. {{witness2_name}}`,
    },
    {
      id: "4",
      name: "Payment Receipt Template",
      status: "published",
      updatedAt: new Date(2023, 11, 10),
      updatedBy: {
        name: "Ananya Patel",
        avatar: "AP",
        seed: "Ananya",
      },
      type: "payment_receipt",
      paperSize: "a4",
      projectAvailability: "all",
      specificProjects: [],
      content: `PAYMENT RECEIPT
  
  Receipt No: {{receipt_number}}
  Date: {{receipt_date}}
  
  Received with thanks from {{customer_name}} a sum of ₹{{amount}} (Rupees {{amount_in_words}} only) towards {{payment_purpose}} for Property/Unit No. {{property_id}} in {{project_name}}.
  
  Payment Details:
  - Mode of Payment: {{payment_mode}}
  - Cheque/DD/Transaction No: {{transaction_id}}
  - Bank: {{bank_name}}
  - Date: {{transaction_date}}
  
  This receipt is subject to realization of cheque/DD (if applicable).
  
  For {{company_name}}
  
  {{authorized_signatory}}
  Authorized Signatory
  
  Note: This is a computer-generated receipt and does not require a signature.`,
    },
    {
      id: "5",
      name: "Commercial Property Agreement",
      status: "draft",
      updatedAt: new Date(2023, 8, 25),
      updatedBy: {
        name: "Suresh Mehta",
        avatar: "SM",
        seed: "Suresh",
      },
      type: "commercial_agreement",
      paperSize: "legal",
      projectAvailability: "specific",
      specificProjects: ["5"],
      content: `COMMERCIAL PROPERTY AGREEMENT
  
  THIS AGREEMENT made on this {{agreement_date}} between {{owner_name}}, having address at {{owner_address}} (hereinafter called the "OWNER") of the ONE PART
  
  AND
  
  {{tenant_name}}, having address at {{tenant_address}} (hereinafter called the "TENANT") of the OTHER PART.
  
  WHEREAS the Owner is the absolute owner of the commercial property bearing No. {{property_number}} situated at {{property_address}} (hereinafter referred to as the "PREMISES").
  
  AND WHEREAS the Tenant has approached the Owner to take the Premises on lease for commercial purposes for a period of {{lease_period}} years commencing from {{lease_start_date}}.
  
  NOW THIS AGREEMENT WITNESSETH AS FOLLOWS:
  
  1. RENT: The Tenant shall pay a monthly rent of ₹{{monthly_rent}} plus applicable GST.
  
  2. SECURITY DEPOSIT: The Tenant has paid a sum of ₹{{security_deposit}} as interest-free refundable security deposit.
  
  3. LOCK-IN PERIOD: There shall be a lock-in period of {{lock_in_period}} months during which neither party can terminate this agreement except for breach of terms.
  
  4. ESCALATION: The rent shall be increased by {{escalation_rate}}% after completion of every {{escalation_period}} months.
  
  5. MAINTENANCE: The Tenant shall pay a monthly maintenance charge of ₹{{maintenance_charge}} to the {{maintenance_authority}}.
  
  6. PERMITTED USE: The Premises shall be used only for {{permitted_use}} and for no other purpose.
  
  7. TERMINATION: After the lock-in period, either party may terminate this agreement by giving {{notice_period}} months' notice in writing.
  
  IN WITNESS WHEREOF, the parties hereto have set their hands on the day, month and year first above written.
  
  OWNER                                TENANT
  {{owner_name}}                       {{tenant_name}}
  
  WITNESSES:
  1. {{witness1_name}}
  2. {{witness2_name}}`,
    },
  ]
  
  // Available shortcodes for templates
export const availableShortcodes = [
    { id: "customer_name", name: "Customer Name (all allottees)" },
    { id: "customer_names", name: "All customer names (booking)" },
    { id: "primary_customer_name", name: "Primary customer name" },
    { id: "customer_email", name: "Customer Email" },
    { id: "customer_address", name: "Customer Address" },
    { id: "allottee_count", name: "Number of Allottees" },
    { id: "property_name", name: "Property Name" },
    { id: "project_name", name: "Project Name" },
    { id: "project_state", name: "Project State" },
    { id: "project_city", name: "Project City" },
    { id: "property_id", name: "Property ID" },
    { id: "unit_type", name: "Unit Type" },
    { id: "unit_number", name: "Unit Number" },
    { id: "floor", name: "Floor" },
    { id: "floor_number", name: "Floor Number (ordinal, e.g. 12th)" },
    { id: "block", name: "Block" },
    { id: "tower", name: "Tower" },
    { id: "wing_name", name: "Wing Name" },
    { id: "unit_carpet_area", name: "Unit Carpet Area (sq.m)" },
    { id: "rera_carpet_sq_m", name: "RERA Carpet Area (sq.m)" },
    { id: "area", name: "Area" },
    { id: "basic_price", name: "Basic Price" },
    { id: "agreement_value", name: "Total Agreement Value (formatted)" },
    { id: "booking_amount", name: "Booking Amount" },
    { id: "booking_date", name: "Booking Date (full)" },
    { id: "booking_month", name: "Booking Month" },
    { id: "booking_year", name: "Booking Year" },
    { id: "advance_amount", name: "Advance Amount (formatted)" },
    { id: "advance_amount_words", name: "Advance Amount in Words" },
    { id: "balance_amount", name: "Balance Amount (formatted)" },
    { id: "agreement_date", name: "Agreement Date (day ordinal)" },
    { id: "agreement_month_words", name: "Agreement Month" },
    { id: "agreement_year_words", name: "Agreement Year (words)" },
    { id: "payment_schedule", name: "Payment Schedule" },
    { id: "customer_service_number", name: "Customer Service Number" },
    { id: "company_name", name: "Company Name" },
    { id: "promoter_name", name: "Promoter / company admin name" },
    { id: "promoter_address", name: "Promoter / company address" },
    { id: "sales_manager_name", name: "Sales Manager Name" },
    { id: "current_date", name: "Current Date" },
    { id: "super_area", name: "Super Area" },
    { id: "carpet_area", name: "Carpet Area" },
    { id: "payment_plan", name: "Payment Plan" },
    { id: "authorized_signatory", name: "Authorized Signatory" },
  ]


export const getPaperSizeDimensions = (paperSize: string) => {
    switch (paperSize) {
      case "a4":
        return { width: "210mm", height: "297mm" }
      case "letter":
        return { width: "215.9mm", height: "279.4mm" }
      case "legal":
        return { width: "215.9mm", height: "355.6mm" }
      default:
        return { width: "210mm", height: "297mm" }
    }
  }
  
  // Calculate scale for preview based on container width
export const calculateScale = (paperSize: string) => {
    // Use a larger percentage of the container width so the preview looks less narrow
    const containerWidth = 650 // Slightly increased to better fill the preview panel
    const paperWidth = getPaperSizeDimensions(paperSize).width
    const paperWidthInPx = Number.parseInt(paperWidth.replace("mm", "")) * 3.78 // Convert mm to px (approximate)
    return Math.min(1, containerWidth / paperWidthInPx)
  }
  
  // Calculate margin bottom to account for scaled height
export const calculateMarginBottom = (paperSize: string) => {
    const paperHeight = getPaperSizeDimensions(paperSize).height
    const paperHeightInPx = Number.parseInt(paperHeight.replace("mm", "")) * 3.78 // Convert mm to px (approximate)
    const scale = calculateScale(paperSize)
    return paperHeightInPx * scale - paperHeightInPx
  }
  
  // Function to get project names from IDs
export const getProjectNames = (projectIds: string[]) => {
    return projectIds
      .map((id) => sampleProjects.find((project) => project.id === id)?.name)
      .filter(Boolean)
      .join(", ")
  }


  export const previewDocument = (content: string) => {
    // In a real app, you would replace shortcodes with actual values
    // For now, we'll just highlight the shortcodes
    const highlightedContent = content.replace(
      /\{\{([^}]+)\}\}/g,
      '<span class="bg-yellow-100 dark:bg-yellow-900 px-1 rounded">{{$1}}</span>',
    )

    // Add page break visualization
    // This is a simplified approach - in a real app, you'd calculate actual page breaks
    const withPageBreaks = highlightedContent.replace(/\n{3,}/g, (match) => `${match}<div class="page-break"></div>`)

    return withPageBreaks
  }


export interface DocumentTemplate {
  id: string;
  name: string;
  type: string;
  content: string;
  status: "published" | "draft" | "unpublished";
  paperSize: string;
  projectAvailability: "all" | "specific";
  specificProjects?: string[];
  variables?: string[];
  updatedAt: string | Date;
  updatedBy: {
    name: string;
    avatar: string;
    seed: string;
  };
}
