/**
 * Delivery note templates seeded per company (Settings → Print templates).
 * All use `itemType: 'delivery-note'`; one `isDefault: true`.
 * `canvasRects` are built with `buildCanvasRectsFromSections` using renderer-aligned
 * height estimates in `lib/utils/canvasLayout.ts` so freeform canvas cells match content.
 */

import type { DocumentTemplate } from '../lib/types/documentTemplate';
import { buildCanvasRectsFromSections } from '../lib/utils/canvasLayout';
import { createWorkScheduleTemplateDraft } from '../lib/utils/documentDefaults';

/** Top-of-page banner image (company letterhead field + fallback). */
function seedBannerImage(heightMm: number, opacity: number) {
  return {
    type: 'image' as const,
    heightMm,
    opacity,
    source: 'field' as const,
    field: 'company.letterheadUrl',
    objectFit: 'contain' as const,
    objectPosition: 'center',
    align: 'center' as const,
    layout: 'fill' as const,
    useCompanyLetterheadFallback: true,
    marginBottomMm: 2,
  };
}

const companySeedPrintTemplatesRaw = [
  {
    id: 'default-delivery-note',
    name: 'Delivery Note',
    itemType: 'delivery-note' as const,
    isDefault: true,
    pageMargins: { top: 10, right: 12, bottom: 10, left: 12 },
    pageStyle: {
      bodyFontFamily: "'Segoe UI', 'Helvetica Neue', Helvetica, Arial, sans-serif",
      pageBackgroundColor: '#ffffff',
      contentLayerOpacity: 1,
    },
    sections: [
      seedBannerImage(22, 0.14),
      { type: 'line', thickness: 2, color: '#1e3a8a', marginTop: 0, marginBottom: 0, width: 100 },
      { type: 'spacer', height: 1 },
      {
        type: 'heading',
        text: 'DELIVERY NOTE',
        fontSize: 16,
        align: 'center',
        bold: true,
        color: '#0f172a',
      },
      {
        type: 'text',
        content: 'Verify quantities and condition at receipt.',
        fontSize: 8,
        align: 'center',
        bold: false,
        color: '#64748b',
      },
      { type: 'spacer', height: 2 },
      {
        type: 'field-row',
        cells: [
          { label: 'Document No.', field: 'dn.number', width: 32, bold: true, fontSize: 10, color: '#0f172a' },
          { label: 'Delivery date', field: 'dn.date', width: 34, fontSize: 9, color: '#334155' },
          { label: 'Printed', field: 'today', width: 34, align: 'right', fontSize: 8, color: '#64748b' },
        ],
        bordered: false,
        minHeight: 10,
      },
      { type: 'divider', thickness: 1, color: '#cbd5e1', marginTop: 1, marginBottom: 2 },
      {
        type: 'info-grid',
        columns: 2,
        bordered: true,
        items: [
          { label: 'Customer', field: 'customer.name', bold: true },
          { label: 'Contact', field: 'customer.contactPerson' },
          { label: 'Phone', field: 'customer.phone' },
          { label: 'Email', field: 'customer.email' },
          { label: 'Address', field: 'customer.address' },
          { label: 'Site', field: 'job.site' },
        ],
      },
      { type: 'spacer', height: 2 },
      {
        type: 'field-row',
        cells: [
          { label: 'Job No.', field: 'job.jobNumber', width: 30, bold: true, fontSize: 9, color: '#0f172a' },
          { label: 'Project', field: 'job.projectName', width: 38, fontSize: 9, color: '#334155' },
          { label: 'LPO', field: 'job.lpoNumber', width: 32, align: 'right', fontSize: 8, color: '#475569' },
        ],
        bordered: false,
      },
      {
        type: 'field-row',
        cells: [
          { label: 'Contact', field: 'job.contactPerson', width: 40, fontSize: 8, color: '#334155' },
          { label: 'Phone', field: 'job.contactPhone', width: 30, fontSize: 8, color: '#334155' },
          { label: 'Email', field: 'job.contactEmail', width: 30, align: 'right', fontSize: 8, color: '#475569' },
        ],
        bordered: false,
      },
      {
        type: 'field-row',
        cells: [
          { label: 'Scope', field: 'job.description', width: 58, fontSize: 8, color: '#334155' },
          { label: 'Quotation', field: 'job.quotationNumber', width: 42, align: 'right', fontSize: 8, color: '#475569' },
        ],
        bordered: false,
      },
      { type: 'spacer', height: 2 },
      { type: 'divider', thickness: 2, color: '#0f172a', marginTop: 0, marginBottom: 1 },
      {
        type: 'table',
        dataSource: 'customItems',
        columns: [
          { header: '#', field: 'slno', width: 6, align: 'center' },
          { header: 'Item / material', field: 'name', width: 38, align: 'left' },
          { header: 'Specification', field: 'description', width: 28, align: 'left' },
          { header: 'Qty', field: 'qty', width: 14, align: 'right' },
          { header: 'Unit', field: 'unit', width: 14, align: 'center' },
        ],
        fontSize: 8,
        showBorders: true,
        headerBg: '#1e3a8a',
        headerColor: '#ffffff',
        repeatHeaderOnNewPage: true,
        minRows: 4,
        rowPadding: 2,
      },
      { type: 'spacer', height: 1 },
      {
        type: 'field-row',
        cells: [
          { label: 'Total qty', field: 'dn.quantity', width: 50, bold: true, fontSize: 8, color: '#334155' },
          { label: 'Declared value (ref.)', field: 'dn.totalCost', width: 50, align: 'right', fontSize: 8, color: '#64748b' },
        ],
        bordered: false,
      },
      { type: 'spacer', height: 2 },
      {
        type: 'text',
        content: 'Received in good condition. Report issues within 24 hours.',
        fontSize: 8,
        align: 'center',
        bold: false,
        color: '#475569',
      },
      { type: 'spacer', height: 3 },
      {
        type: 'signatures',
        items: [
          { label: 'Prepared (Store)' },
          { label: 'Delivered (Driver)' },
          { label: 'Received (Customer)' },
        ],
        lineHeight: 16,
        pageAnchor: 'bottom' as const,
      },
    ],
  },

  {
    id: 'seed-dn-minimal-pro',
    name: 'Delivery Note — Minimal Pro',
    itemType: 'delivery-note' as const,
    isDefault: false,
    pageMargins: { top: 12, right: 16, bottom: 14, left: 16 },
    pageStyle: {
      bodyFontFamily: "'Inter', system-ui, -apple-system, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif",
      pageBackgroundColor: '#fafafa',
      contentLayerOpacity: 1,
    },
    sections: [
      seedBannerImage(26, 0.1),
      { type: 'line', thickness: 2, color: '#0d9488', marginTop: 0, marginBottom: 0, width: 100 },
      { type: 'spacer', height: 5 },
      {
        type: 'heading',
        text: 'Delivery note',
        fontSize: 20,
        align: 'center',
        bold: true,
        color: '#134e4a',
      },
      {
        type: 'text',
        content: 'Handoff record · Confirm receipt on site',
        fontSize: 8,
        align: 'center',
        bold: false,
        color: '#5eead4',
      },
      { type: 'spacer', height: 4 },
      {
        type: 'field-row',
        cells: [
          { label: 'No.', field: 'dn.number', width: 34, bold: true, fontSize: 11, color: '#134e4a' },
          { label: 'Date', field: 'dn.date', width: 33, fontSize: 10, color: '#115e59' },
          { label: 'Printed', field: 'today', width: 33, align: 'right', fontSize: 9, color: '#64748b' },
        ],
        bordered: false,
        minHeight: 12,
      },
      { type: 'divider', thickness: 1, color: '#99f6e4', marginTop: 2, marginBottom: 3 },
      {
        type: 'info-grid',
        columns: 2,
        bordered: true,
        items: [
          { label: 'Customer', field: 'customer.name', bold: true },
          { label: 'Phone', field: 'customer.phone' },
          { label: 'Address', field: 'customer.address' },
          { label: 'Site', field: 'job.site' },
        ],
      },
      { type: 'spacer', height: 3 },
      {
        type: 'field-row',
        cells: [
          { label: 'Job', field: 'job.jobNumber', width: 40, bold: true, fontSize: 10, color: '#134e4a' },
          { label: 'Project', field: 'job.projectName', width: 60, fontSize: 9, color: '#0f766e' },
        ],
        bordered: false,
      },
      {
        type: 'field-row',
        cells: [
          { label: 'Contact', field: 'job.contactPerson', width: 40, fontSize: 8, color: '#134e4a' },
          { label: 'Phone', field: 'job.contactPhone', width: 30, fontSize: 8, color: '#115e59' },
          { label: 'Email', field: 'job.contactEmail', width: 30, align: 'right', fontSize: 8, color: '#64748b' },
        ],
        bordered: false,
      },
      { type: 'spacer', height: 3 },
      {
        type: 'table',
        dataSource: 'customItems',
        columns: [
          { header: '#', field: 'slno', width: 8, align: 'center' },
          { header: 'Description', field: 'name', width: 52, align: 'left' },
          { header: 'Qty', field: 'qty', width: 20, align: 'right' },
          { header: 'Unit', field: 'unit', width: 20, align: 'center' },
        ],
        fontSize: 9,
        showBorders: true,
        headerBg: '#ccfbf1',
        headerColor: '#134e4a',
        repeatHeaderOnNewPage: true,
        minRows: 7,
        rowPadding: 2.5,
      },
      { type: 'spacer', height: 4 },
      {
        type: 'text',
        content: 'Goods received in good condition unless noted.',
        fontSize: 9,
        align: 'center',
        bold: false,
        color: '#64748b',
      },
      { type: 'spacer', height: 6 },
      {
        type: 'signatures',
        items: [{ label: 'Prepared' }, { label: 'Delivered' }, { label: 'Received' }],
        lineHeight: 22,
        pageAnchor: 'bottom' as const,
      },
    ],
  },

  {
    id: 'seed-dn-executive',
    name: 'Delivery Note — Executive',
    itemType: 'delivery-note' as const,
    isDefault: false,
    pageMargins: { top: 14, right: 18, bottom: 16, left: 18 },
    pageStyle: {
      bodyFontFamily: "'Georgia', 'Times New Roman', serif",
      pageBackgroundColor: '#fffef9',
      contentLayerOpacity: 1,
    },
    sections: [
      seedBannerImage(32, 0.12),
      { type: 'line', thickness: 1, color: '#b45309', marginTop: 0, marginBottom: 1, width: 100 },
      { type: 'spacer', height: 3 },
      {
        type: 'heading',
        text: 'Delivery Note',
        fontSize: 22,
        align: 'center',
        bold: true,
        color: '#422006',
      },
      { type: 'spacer', height: 2 },
      {
        type: 'field-row',
        cells: [
          { label: 'Reference', field: 'dn.number', width: 38, bold: true, fontSize: 11, color: '#422006' },
          { label: 'Date', field: 'dn.date', width: 32, fontSize: 10, color: '#713f12' },
          { label: 'Printed', field: 'today', width: 30, align: 'right', fontSize: 9, color: '#78716c' },
        ],
        bordered: false,
        minHeight: 13,
      },
      { type: 'divider', thickness: 1, color: '#d6d3d1', marginTop: 3, marginBottom: 3 },
      {
        type: 'info-grid',
        columns: 2,
        bordered: true,
        items: [
          { label: 'Deliver to', field: 'customer.name', bold: true },
          { label: 'Attention', field: 'customer.contactPerson' },
          { label: 'Telephone', field: 'customer.phone' },
          { label: 'Project site', field: 'job.site' },
        ],
      },
      { type: 'spacer', height: 3 },
      {
        type: 'field-row',
        cells: [
          { label: 'Job ref.', field: 'job.jobNumber', width: 35, bold: true, fontSize: 10 },
          { label: 'LPO', field: 'job.lpoNumber', width: 30, fontSize: 10 },
          { label: 'Quotation', field: 'job.quotationNumber', width: 35, align: 'right', fontSize: 9 },
        ],
        bordered: false,
      },
      {
        type: 'field-row',
        cells: [
          { label: 'Contact', field: 'job.contactPerson', width: 40, fontSize: 9, color: '#57534e' },
          { label: 'Phone', field: 'job.contactPhone', width: 30, fontSize: 9, color: '#57534e' },
          { label: 'Email', field: 'job.contactEmail', width: 30, align: 'right', fontSize: 9, color: '#78716c' },
        ],
        bordered: false,
      },
      {
        type: 'field-row',
        cells: [{ label: 'Scope of supply', field: 'job.description', width: 100, fontSize: 9, color: '#57534e' }],
        bordered: false,
      },
      { type: 'spacer', height: 3 },
      {
        type: 'table',
        dataSource: 'customItems',
        columns: [
          { header: 'Item', field: 'slno', width: 8, align: 'center' },
          { header: 'Description', field: 'name', width: 44, align: 'left' },
          { header: 'Details', field: 'description', width: 28, align: 'left' },
          { header: 'Qty', field: 'qty', width: 10, align: 'right' },
          { header: 'UoM', field: 'unit', width: 10, align: 'center' },
        ],
        fontSize: 10,
        showBorders: true,
        headerBg: '#fef3c7',
        headerColor: '#422006',
        repeatHeaderOnNewPage: true,
        minRows: 6,
        rowPadding: 2.5,
      },
      { type: 'spacer', height: 4 },
      {
        type: 'text',
        content: 'The undersigned confirms receipt of the materials described above.',
        fontSize: 10,
        align: 'center',
        bold: false,
        color: '#57534e',
      },
      { type: 'spacer', height: 7 },
      {
        type: 'signatures',
        items: [{ label: 'Warehouse' }, { label: 'Carrier' }, { label: 'Consignee' }],
        lineHeight: 24,
      },
    ],
  },

  {
    id: 'seed-dn-compact',
    name: 'Delivery Note — Compact',
    itemType: 'delivery-note' as const,
    isDefault: false,
    pageMargins: { top: 8, right: 10, bottom: 8, left: 10 },
    pageStyle: {
      bodyFontFamily: "system-ui, -apple-system, 'Segoe UI', Roboto, Arial, sans-serif",
      pageBackgroundColor: '#ffffff',
      contentLayerOpacity: 1,
    },
    sections: [
      seedBannerImage(22, 0.1),
      { type: 'line', thickness: 2, color: '#0369a1', marginTop: 0, marginBottom: 0, width: 100 },
      { type: 'spacer', height: 2 },
      {
        type: 'heading',
        text: 'DELIVERY NOTE',
        fontSize: 14,
        align: 'center',
        bold: true,
        color: '#0c4a6e',
      },
      {
        type: 'field-row',
        cells: [
          { label: 'DN', field: 'dn.number', width: 40, bold: true, fontSize: 9, color: '#0c4a6e' },
          { label: 'Date', field: 'dn.date', width: 30, fontSize: 8 },
          { label: '', field: 'today', width: 30, align: 'right', fontSize: 8, color: '#64748b' },
        ],
        bordered: false,
        minHeight: 10,
      },
      {
        type: 'info-grid',
        columns: 2,
        bordered: true,
        items: [
          { label: 'Customer', field: 'customer.name', bold: true },
          { label: 'Job', field: 'job.jobNumber' },
          { label: 'Site', field: 'job.site' },
          { label: 'Phone', field: 'customer.phone' },
        ],
      },
      {
        type: 'field-row',
        cells: [
          { label: 'Contact', field: 'job.contactPerson', width: 40, fontSize: 8, color: '#0c4a6e' },
          { label: 'Phone', field: 'job.contactPhone', width: 30, fontSize: 8, color: '#0c4a6e' },
          { label: 'Email', field: 'job.contactEmail', width: 30, align: 'right', fontSize: 8, color: '#64748b' },
        ],
        bordered: false,
      },
      { type: 'spacer', height: 2 },
      {
        type: 'table',
        dataSource: 'customItems',
        columns: [
          { header: '#', field: 'slno', width: 7, align: 'center' },
          { header: 'Material', field: 'name', width: 50, align: 'left' },
          { header: 'Qty', field: 'qty', width: 18, align: 'right' },
          { header: 'U', field: 'unit', width: 25, align: 'center' },
        ],
        fontSize: 8,
        showBorders: true,
        headerBg: '#e0f2fe',
        headerColor: '#0c4a6e',
        repeatHeaderOnNewPage: true,
        minRows: 8,
        rowPadding: 2,
      },
      { type: 'spacer', height: 2 },
      {
        type: 'text',
        content: 'Received OK.',
        fontSize: 8,
        align: 'center',
        bold: true,
        color: '#334155',
      },
      { type: 'spacer', height: 4 },
      {
        type: 'signatures',
        items: [{ label: 'Prep.' }, { label: 'Del.' }, { label: 'Recv.' }],
        lineHeight: 18,
      },
    ],
  },

  {
    id: 'seed-dn-violet-pro',
    name: 'Delivery Note — Violet Pro',
    itemType: 'delivery-note' as const,
    isDefault: false,
    pageMargins: { top: 12, right: 15, bottom: 13, left: 15 },
    pageStyle: {
      bodyFontFamily: "'Segoe UI', system-ui, sans-serif",
      pageBackgroundColor: '#faf5ff',
      contentLayerOpacity: 1,
    },
    sections: [
      seedBannerImage(28, 0.11),
      { type: 'line', thickness: 3, color: '#6d28d9', marginTop: 0, marginBottom: 0, width: 100 },
      { type: 'spacer', height: 3 },
      {
        type: 'heading',
        text: 'Delivery Note',
        fontSize: 19,
        align: 'center',
        bold: true,
        color: '#4c1d95',
      },
      {
        type: 'text',
        content: 'Shipment documentation',
        fontSize: 8,
        align: 'center',
        bold: false,
        color: '#7c3aed',
      },
      { type: 'spacer', height: 3 },
      {
        type: 'field-row',
        cells: [
          { label: 'DN #', field: 'dn.number', width: 36, bold: true, fontSize: 11, color: '#4c1d95' },
          { label: 'Delivery date', field: 'dn.date', width: 34, fontSize: 10 },
          { label: 'Printed', field: 'today', width: 30, align: 'right', fontSize: 9, color: '#6b7280' },
        ],
        bordered: false,
        minHeight: 12,
      },
      { type: 'divider', thickness: 1, color: '#ddd6fe', marginTop: 2, marginBottom: 3 },
      {
        type: 'info-grid',
        columns: 2,
        bordered: true,
        items: [
          { label: 'Customer', field: 'customer.name', bold: true },
          { label: 'Contact', field: 'customer.contactPerson' },
          { label: 'Phone', field: 'customer.phone' },
          { label: 'Address', field: 'customer.address' },
          { label: 'Job #', field: 'job.jobNumber', bold: true },
          { label: 'Site', field: 'job.site' },
        ],
      },
      { type: 'spacer', height: 3 },
      {
        type: 'field-row',
        cells: [
          { label: 'Project', field: 'job.projectName', width: 55, fontSize: 9 },
          { label: 'LPO', field: 'job.lpoNumber', width: 45, align: 'right', fontSize: 9 },
        ],
        bordered: false,
      },
      {
        type: 'field-row',
        cells: [
          { label: 'Contact', field: 'job.contactPerson', width: 40, fontSize: 8, color: '#5b21b6' },
          { label: 'Phone', field: 'job.contactPhone', width: 30, fontSize: 8, color: '#5b21b6' },
          { label: 'Email', field: 'job.contactEmail', width: 30, align: 'right', fontSize: 8, color: '#6b7280' },
        ],
        bordered: false,
      },
      { type: 'spacer', height: 2 },
      {
        type: 'table',
        dataSource: 'customItems',
        columns: [
          { header: '#', field: 'slno', width: 7, align: 'center' },
          { header: 'Item', field: 'name', width: 40, align: 'left' },
          { header: 'Spec.', field: 'description', width: 28, align: 'left' },
          { header: 'Qty', field: 'qty', width: 12, align: 'right' },
          { header: 'Unit', field: 'unit', width: 13, align: 'center' },
        ],
        fontSize: 9,
        showBorders: true,
        headerBg: '#6d28d9',
        headerColor: '#ffffff',
        repeatHeaderOnNewPage: true,
        minRows: 7,
        rowPadding: 2.5,
      },
      { type: 'spacer', height: 3 },
      {
        type: 'text',
        content: 'Received in good condition.',
        fontSize: 9,
        align: 'center',
        bold: false,
        color: '#5b21b6',
      },
      { type: 'spacer', height: 6 },
      {
        type: 'signatures',
        items: [{ label: 'Prepared' }, { label: 'Delivered' }, { label: 'Received' }],
        lineHeight: 22,
      },
    ],
  },

  {
    id: 'seed-dn-monochrome',
    name: 'Delivery Note — Monochrome',
    itemType: 'delivery-note' as const,
    isDefault: false,
    pageMargins: { top: 11, right: 14, bottom: 12, left: 14 },
    pageStyle: {
      bodyFontFamily: "'Helvetica Neue', Helvetica, Arial, sans-serif",
      pageBackgroundColor: '#ffffff',
      contentLayerOpacity: 1,
    },
    sections: [
      seedBannerImage(28, 0.08),
      { type: 'divider', thickness: 2, color: '#000000', marginTop: 0, marginBottom: 2 },
      {
        type: 'heading',
        text: 'DELIVERY NOTE',
        fontSize: 17,
        align: 'center',
        bold: true,
        color: '#000000',
      },
      { type: 'spacer', height: 2 },
      {
        type: 'field-row',
        cells: [
          { label: 'No.', field: 'dn.number', width: 40, bold: true, fontSize: 10 },
          { label: 'Date', field: 'dn.date', width: 30, fontSize: 10 },
          { label: 'Printed', field: 'today', width: 30, align: 'right', fontSize: 9, color: '#525252' },
        ],
        bordered: false,
        minHeight: 11,
      },
      {
        type: 'info-grid',
        columns: 2,
        bordered: true,
        items: [
          { label: 'Customer', field: 'customer.name', bold: true },
          { label: 'Address', field: 'customer.address' },
          { label: 'Job', field: 'job.jobNumber' },
          { label: 'Site', field: 'job.site' },
        ],
      },
      {
        type: 'field-row',
        cells: [
          { label: 'Contact', field: 'job.contactPerson', width: 40, fontSize: 9, color: '#404040' },
          { label: 'Phone', field: 'job.contactPhone', width: 30, fontSize: 9, color: '#404040' },
          { label: 'Email', field: 'job.contactEmail', width: 30, align: 'right', fontSize: 9, color: '#525252' },
        ],
        bordered: false,
      },
      { type: 'spacer', height: 2 },
      {
        type: 'field-row',
        cells: [{ label: 'Description', field: 'job.description', width: 100, fontSize: 9, color: '#404040' }],
        bordered: false,
      },
      { type: 'spacer', height: 2 },
      {
        type: 'table',
        dataSource: 'customItems',
        columns: [
          { header: '#', field: 'slno', width: 8, align: 'center' },
          { header: 'Material', field: 'name', width: 50, align: 'left' },
          { header: 'Qty', field: 'qty', width: 14, align: 'right' },
          { header: 'Unit', field: 'unit', width: 28, align: 'center' },
        ],
        fontSize: 9,
        showBorders: true,
        headerBg: '#e5e5e5',
        headerColor: '#000000',
        repeatHeaderOnNewPage: true,
        minRows: 7,
        rowPadding: 2.5,
      },
      { type: 'spacer', height: 3 },
      {
        type: 'text',
        content: 'Received in good condition.',
        fontSize: 8,
        align: 'center',
        bold: false,
        color: '#525252',
      },
      { type: 'spacer', height: 6 },
      {
        type: 'signatures',
        items: [{ label: 'Prepared' }, { label: 'Delivered' }, { label: 'Received' }],
        lineHeight: 22,
      },
    ],
  },

  (() => {
    const template = createWorkScheduleTemplateDraft(
      'default-work-schedule',
      'Daily Work Schedule - Color Matrix'
    );
    template.isDefault = true;
    template.pageMargins = { top: 7, right: 7, bottom: 8, left: 7 };
    template.pageStyle = {
      ...template.pageStyle,
      pageOrientation: 'landscape',
      bodyFontFamily: "'Segoe UI', Arial, sans-serif",
      pageBackgroundColor: '#f8fafc',
    };
    template.sections = [
      seedBannerImage(18, 0.1),
      {
        type: 'heading',
        text: 'DAILY WORK SCHEDULE',
        fontSize: 15,
        align: 'center',
        bold: true,
        color: '#0f172a',
      },
      {
        type: 'field-row',
        bordered: true,
        minHeight: 10,
        cells: [
          { label: 'Date', field: 'schedule.workDateLabel', width: 20, bold: true, fontSize: 8.5, color: '#0f172a' },
          { label: 'Status', field: 'schedule.status', width: 14, fontSize: 8.2, color: '#1d4ed8' },
          { label: 'Teams', field: 'schedule.groupCount', width: 10, align: 'center', fontSize: 8.2, color: '#0f172a' },
          { label: 'Workers', field: 'schedule.assignedWorkerCount', width: 12, align: 'center', fontSize: 8.2, color: '#0f172a' },
          { label: 'Trips', field: 'schedule.driverTripSummary', width: 18, fontSize: 8.2, color: '#7c2d12' },
          { label: 'Primary Job', field: 'job.jobNumber', width: 12, fontSize: 8.2, color: '#0f172a' },
          { label: 'Primary Location', field: 'job.locationLabel', width: 14, align: 'right', fontSize: 8.2, color: '#0f172a' },
        ],
      },
      {
        type: 'table',
        dataSource: 'scheduleGroups',
        layoutMode: 'group-columns',
        fontSize: 6.9,
        showBorders: true,
        headerBg: '#0f172a',
        headerColor: '#ffffff',
        headerFontWeight: 'bold',
        headerFontStyle: 'normal',
        repeatHeaderOnNewPage: true,
        minRows: 3,
        rowPadding: 1.5,
        rowMinHeightMm: 6.8,
        columns: [
          {
            header: 'Team / Location',
            field: 'locationDisplay',
            align: 'left',
            useGlobalHeaderStyle: false,
            headerBg: '#0f172a',
            headerColor: '#ffffff',
            cellBg: '#e0f2fe',
            cellColor: '#0c4a6e',
            fontWeight: 'bold',
          },
          { header: 'Job No', field: 'jobNumber', align: 'left', cellBg: '#f8fafc', cellColor: '#0f172a' },
          { header: 'Customer', field: 'customerName', align: 'left', cellBg: '#ecfeff', cellColor: '#155e75' },
          { header: 'Work Process Details', field: 'workProcessDetails', align: 'left', cellBg: '#eef2ff', cellColor: '#3730a3' },
          { header: 'Duty Timing', field: 'dutyRange', align: 'center', cellBg: '#dcfce7', cellColor: '#166534', fontWeight: 'bold' },
          { header: 'Break Timing', field: 'breakRange', align: 'center', cellBg: '#fef3c7', cellColor: '#92400e' },
          { header: 'Target Qty', field: 'targetQty', align: 'left', cellBg: '#fae8ff', cellColor: '#7e22ce', fontWeight: 'bold' },
          { header: 'Team / Sub-team', field: 'workerStructuredRows', align: 'left', cellBg: '#fff7ed', cellColor: '#9a3412' },
          { header: 'Drivers', field: 'driverNames', align: 'left', cellBg: '#fce7f3', cellColor: '#9d174d' },
          { header: 'Remarks', field: 'remarks', align: 'left', cellBg: '#f8fafc', cellColor: '#334155', fontStyle: 'italic' },
        ],
      },
      {
        type: 'field-row',
        bordered: true,
        minHeight: 10,
        cells: [
          { label: 'Schedule Notes', field: 'schedule.notes', width: 100, fontSize: 8, color: '#334155' },
        ],
      },
      {
        type: 'heading',
        text: 'Driver Trip Plan',
        fontSize: 11,
        align: 'left',
        bold: true,
        color: '#0f172a',
        pageAnchor: 'bottom',
      },
      {
        type: 'table',
        dataSource: 'driverTrips',
        columns: [
          { header: '#', field: 'slno', width: 8, align: 'center' },
          { header: 'Driver', field: 'driverName', width: 24, align: 'left' },
          { header: 'Trip Order / Route', field: 'tripOrder', width: 68, align: 'left' },
        ],
        fontSize: 7.8,
        showBorders: true,
        headerBg: '#dbeafe',
        headerColor: '#1e3a8a',
        repeatHeaderOnNewPage: false,
        minRows: 3,
        rowPadding: 1.8,
        pageAnchor: 'bottom',
      },
    ];
    return template;
  })(),

  (() => {
    const template = createWorkScheduleTemplateDraft(
      'seed-work-schedule-site-focus',
      'Daily Work Schedule - Crew Board'
    );
    template.isDefault = false;
    template.pageMargins = { top: 7, right: 7, bottom: 7, left: 7 };
    template.pageStyle = {
      ...template.pageStyle,
      pageOrientation: 'landscape',
      bodyFontFamily: "'Inter', 'Segoe UI', Arial, sans-serif",
      pageBackgroundColor: '#f0fdf4',
    };
    template.sections = [
      seedBannerImage(18, 0.08),
      {
        type: 'heading',
        text: 'SITE CREW BOARD',
        fontSize: 15,
        align: 'center',
        bold: true,
        color: '#14532d',
      },
      {
        type: 'field-row',
        bordered: false,
        minHeight: 10,
        cells: [
          { label: 'Date', field: 'schedule.workDateLabel', width: 20, bold: true, fontSize: 8.5, color: '#14532d' },
          { label: 'Status', field: 'schedule.status', width: 14, fontSize: 8.2, color: '#166534' },
          { label: 'Teams', field: 'schedule.groupCount', width: 10, align: 'center', fontSize: 8.2, color: '#14532d' },
          { label: 'Workers', field: 'schedule.assignedWorkerCount', width: 12, align: 'center', fontSize: 8.2, color: '#14532d' },
          { label: 'Customer', field: 'job.customerName', width: 24, fontSize: 8.2, color: '#166534' },
          { label: 'Primary Job', field: 'job.jobNumber', width: 20, align: 'right', fontSize: 8.2, color: '#166534' },
        ],
      },
      {
        type: 'table',
        dataSource: 'scheduleGroups',
        layoutMode: 'group-columns',
        fontSize: 6.9,
        showBorders: true,
        headerBg: '#14532d',
        headerColor: '#ffffff',
        headerFontWeight: 'bold',
        headerFontStyle: 'normal',
        repeatHeaderOnNewPage: true,
        minRows: 3,
        rowPadding: 1.5,
        rowMinHeightMm: 6.6,
        columns: [
          {
            header: 'Site / Location',
            field: 'locationDisplay',
            align: 'left',
            useGlobalHeaderStyle: false,
            headerBg: '#14532d',
            headerColor: '#ffffff',
            cellBg: '#dcfce7',
            cellColor: '#14532d',
            fontWeight: 'bold',
          },
          { header: 'Job No', field: 'jobNumber', align: 'left', cellBg: '#f0fdf4', cellColor: '#14532d' },
          { header: 'Customer', field: 'customerName', align: 'left', cellBg: '#ecfccb', cellColor: '#3f6212' },
          { header: 'Work Process Details', field: 'workProcessDetails', align: 'left', cellBg: '#ecfeff', cellColor: '#0f766e' },
          { header: 'Target Qty', field: 'targetQty', align: 'left', cellBg: '#d1fae5', cellColor: '#065f46', fontWeight: 'bold' },
          { header: 'Team / Sub-team', field: 'workerBlocks', align: 'left', cellBg: '#fffbeb', cellColor: '#92400e' },
          { header: 'Drivers', field: 'driverNames', align: 'left', cellBg: '#fef2f2', cellColor: '#991b1b' },
          { header: 'Duty Timing', field: 'dutyRange', align: 'center', cellBg: '#dbeafe', cellColor: '#1d4ed8' },
          { header: 'Break Timing', field: 'breakRange', align: 'center', cellBg: '#fef9c3', cellColor: '#a16207' },
          { header: 'Remarks', field: 'remarks', align: 'left', cellBg: '#f8fafc', cellColor: '#334155' },
        ],
      },
      {
        type: 'field-row',
        bordered: true,
        minHeight: 10,
        cells: [
          { label: 'Schedule Notes', field: 'schedule.notes', width: 100, fontSize: 8, color: '#334155' },
        ],
      },
      {
        type: 'heading',
        text: 'Transport Plan',
        fontSize: 11,
        align: 'left',
        bold: true,
        color: '#14532d',
        pageAnchor: 'bottom',
      },
      {
        type: 'table',
        dataSource: 'driverTrips',
        columns: [
          { header: '#', field: 'slno', width: 8, align: 'center' },
          { header: 'Driver', field: 'driverName', width: 24, align: 'left' },
          { header: 'Trip Order / Route', field: 'tripOrder', width: 68, align: 'left' },
        ],
        fontSize: 7.5,
        showBorders: true,
        headerBg: '#bbf7d0',
        headerColor: '#14532d',
        repeatHeaderOnNewPage: false,
        minRows: 3,
        rowPadding: 1.8,
        pageAnchor: 'bottom',
      },
    ];
    return template;
  })(),

  (() => {
    const template = createWorkScheduleTemplateDraft(
      'seed-work-schedule-driver-plan',
      'Daily Work Schedule - Driver & Notes'
    );
    template.isDefault = false;
    template.pageMargins = { top: 7, right: 7, bottom: 7, left: 7 };
    template.pageStyle = {
      ...template.pageStyle,
      pageOrientation: 'landscape',
      bodyFontFamily: "'Segoe UI', Arial, sans-serif",
      pageBackgroundColor: '#fff7ed',
    };
    template.sections = [
      seedBannerImage(18, 0.08),
      {
        type: 'heading',
        text: 'DAILY WORK SCHEDULE & TRANSPORT',
        fontSize: 15,
        align: 'center',
        bold: true,
        color: '#9a3412',
      },
      {
        type: 'field-row',
        bordered: true,
        minHeight: 10,
        cells: [
          { label: 'Date', field: 'schedule.workDateLabel', width: 20, bold: true, fontSize: 8.5, color: '#9a3412' },
          { label: 'Status', field: 'schedule.status', width: 14, fontSize: 8.2, color: '#9a3412' },
          { label: 'Teams', field: 'schedule.groupCount', width: 10, align: 'center', fontSize: 8.2, color: '#9a3412' },
          { label: 'Workers', field: 'schedule.assignedWorkerCount', width: 12, align: 'center', fontSize: 8.2, color: '#9a3412' },
          { label: 'Trips', field: 'schedule.driverTripSummary', width: 16, fontSize: 8.2, color: '#7c2d12' },
          { label: 'Notes', field: 'schedule.notes', width: 28, fontSize: 8.2, color: '#7c2d12' },
        ],
      },
      {
        type: 'table',
        dataSource: 'scheduleGroups',
        layoutMode: 'group-columns',
        fontSize: 6.8,
        showBorders: true,
        headerBg: '#7c2d12',
        headerColor: '#ffffff',
        headerFontWeight: 'bold',
        headerFontStyle: 'normal',
        repeatHeaderOnNewPage: true,
        minRows: 3,
        rowPadding: 1.5,
        rowMinHeightMm: 6.6,
        columns: [
          {
            header: 'Location',
            field: 'locationDisplay',
            align: 'left',
            useGlobalHeaderStyle: false,
            headerBg: '#7c2d12',
            headerColor: '#ffffff',
            cellBg: '#ffedd5',
            cellColor: '#7c2d12',
            fontWeight: 'bold',
          },
          { header: 'Job No', field: 'jobNumber', align: 'left', cellBg: '#fff7ed', cellColor: '#7c2d12' },
          { header: 'Drivers', field: 'driverNames', align: 'left', cellBg: '#ffedd5', cellColor: '#9a3412', fontWeight: 'bold' },
          { header: 'Duty Timing', field: 'dutyRange', align: 'center', cellBg: '#dbeafe', cellColor: '#1d4ed8' },
          { header: 'Target Qty', field: 'targetQty', align: 'left', cellBg: '#fde68a', cellColor: '#92400e', fontWeight: 'bold' },
          { header: 'Team / Sub-team', field: 'workerStructuredRows', align: 'left', cellBg: '#fef3c7', cellColor: '#92400e' },
          { header: 'Remarks', field: 'remarks', align: 'left', cellBg: '#fff7ed', cellColor: '#7c2d12', fontStyle: 'italic' },
        ],
      },
      {
        type: 'field-row',
        bordered: true,
        minHeight: 10,
        cells: [
          { label: 'Work Process Details', field: 'job.workProcessDetails', width: 60, fontSize: 8, color: '#7c2d12' },
          { label: 'Schedule Notes', field: 'schedule.notes', width: 40, fontSize: 8, color: '#7c2d12' },
        ],
      },
      {
        type: 'heading',
        text: 'Driver Trip Order',
        fontSize: 11,
        align: 'left',
        bold: true,
        color: '#7c2d12',
        pageAnchor: 'bottom',
      },
      {
        type: 'table',
        dataSource: 'driverTrips',
        columns: [
          { header: '#', field: 'slno', width: 8, align: 'center' },
          { header: 'Driver', field: 'driverName', width: 24, align: 'left' },
          { header: 'Trip Order / Route', field: 'tripOrder', width: 68, align: 'left' },
        ],
        fontSize: 8,
        showBorders: true,
        headerBg: '#fdba74',
        headerColor: '#7c2d12',
        repeatHeaderOnNewPage: false,
        minRows: 3,
        rowPadding: 2,
        pageAnchor: 'bottom',
      },
    ];
    return template;
  })(),
];

export const companySeedPrintTemplates: DocumentTemplate[] = companySeedPrintTemplatesRaw.map((tpl) => {
  const base = tpl as DocumentTemplate;
  return {
    ...base,
    canvasMode: true,
    canvasRects: buildCanvasRectsFromSections({ ...base, canvasMode: true }),
  };
});
