// data.jsx — document type catalog, recent drafts, form schemas, draft templates
// All exported to window for cross-script access.

const DOC_CATEGORIES = [
  {
    id: 'rep',
    label: 'Representation Agreements',
    sub: 'Engagement contracts between CEG and your client',
    items: [
      {
        id: 'tenant-rep',
        name: 'Tenant Representation Agreement',
        short: 'Tenant Rep',
        icon: 'handshake',
        desc: 'Exclusive engagement to represent a tenant in identifying space and negotiating lease terms.',
        estTime: '3–5 min',
        status: 'live',
        approver: 'Halie',
      },
      {
        id: 'buyer-rep',
        name: 'Buyer Representation Agreement',
        short: 'Buyer Rep',
        icon: 'key-round',
        desc: 'Engagement to represent a buyer in identifying and acquiring commercial property.',
        estTime: '3–5 min',
        status: 'live',
        approver: 'Halie',
      },
    ],
  },
  {
    id: 'list',
    label: 'Listing Agreements',
    sub: 'Exclusive engagements to market property on the owner’s behalf',
    items: [
      {
        id: 'list-lease',
        name: 'Lease Listing Agreement',
        short: 'Lease Listing',
        icon: 'building',
        desc: 'CEG’s sole right to market available space to tenants on the owner’s behalf.',
        estTime: '4–6 min',
        status: 'live',
        approver: 'Halie',
      },
      {
        id: 'list-sale',
        name: 'Sale Listing Agreement',
        short: 'Sale Listing',
        icon: 'building-2',
        desc: 'CEG’s sole right to market the property for sale on the owner’s behalf.',
        estTime: '4–6 min',
        status: 'live',
        approver: 'Halie',
      },
      {
        id: 'list-lease-sale',
        name: 'Lease or Sale Listing Agreement',
        short: 'Lease or Sale Listing',
        icon: 'warehouse',
        desc: 'Combined exclusive — owner gives CEG both the lease and sale mandate at once.',
        estTime: '5–7 min',
        status: 'live',
        approver: 'Halie',
      },
    ],
  },
  {
    id: 'props',
    label: 'Proposals & Offers',
    sub: 'Deal-level documents pulled from approved CEG language',
    items: [
      {
        id: 'loi',
        name: 'Letter of Intent (LOI)',
        short: 'LOI',
        icon: 'file-text',
        desc: 'Non-binding proposal outlining the major business terms ahead of a definitive agreement.',
        estTime: '5–8 min',
        status: 'beta',
        approver: 'Halie',
      },
      {
        id: 'lease-proposal',
        name: 'Tenant Lease Proposal',
        short: 'Lease Proposal',
        icon: 'file-pen-line',
        desc: 'Detailed proposal of lease economics — rent, term, TI, options.',
        estTime: '6–10 min',
        status: 'soon',
        approver: 'Halie',
      },
      {
        id: 'purchase-offer',
        name: 'Purchase Offer',
        short: 'Purchase Offer',
        icon: 'file-signature',
        desc: 'Formal written offer to purchase commercial property.',
        estTime: 'coming soon',
        status: 'soon',
        approver: 'Halie',
      },
    ],
  },
  {
    id: 'transaction',
    label: 'Transaction Documents',
    sub: 'Binding contracts that govern the purchase of property',
    items: [
      {
        id: 'purchase-agreement',
        name: 'Purchase Agreement',
        short: 'PA',
        icon: 'file-pen-line',
        desc: 'Full real estate purchase contract — Buyer purchases property from Seller. Contingencies, earnest money, title work, exhibits.',
        estTime: '8–12 min',
        status: 'live',
        approver: 'Halie',
      },
    ],
  },
];

const ALL_DOCS = DOC_CATEGORIES.flatMap(c => c.items.map(d => ({...d, category: c.label, catId: c.id})));
const DOC_BY_ID = Object.fromEntries(ALL_DOCS.map(d => [d.id, d]));

const RECENT_DRAFTS = [
  {
    id: 'r1',
    docId: 'tenant-rep',
    title: 'Klear Industries — Tenant Rep',
    sub: 'Tenant Representation · 14,200 SF Industrial',
    updated: '2 hours ago',
    completeness: 92,
    status: 'review',
    statusLabel: 'In review · Halie',
  },
  {
    id: 'r2',
    docId: 'list-lease',
    title: '2400 Wacker, Floor 14 — Listing',
    sub: 'Lease Listing Agreement · 14,200 SF Class A',
    updated: 'Yesterday, 4:12pm',
    completeness: 60,
    status: 'draft',
    statusLabel: 'Draft',
  },
  {
    id: 'r3',
    docId: 'buyer-rep',
    title: 'Stinski Holdings — Buyer Rep',
    sub: 'Buyer Representation · NE Minneapolis warehouse',
    updated: '3 days ago',
    completeness: 100,
    status: 'sent',
    statusLabel: 'Sent · Jeff',
  },
  {
    id: 'r4',
    docId: 'list-sale',
    title: '1845 W Industrial Blvd — ERTS',
    sub: 'Sale Listing Agreement · 38,400 SF Industrial',
    updated: 'Last week',
    completeness: 100,
    status: 'sent',
    statusLabel: 'Signed',
  },
];

// ─── Form schemas keyed by doc type ───────────────────────────────────────
// fieldType: text | number | select | currency | sf | percent | date | tel | email | textarea
const FORM_SCHEMAS = {
  'tenant-rep': {
    title: 'Tenant Representation Agreement',
    subtitle: 'Engaging CEG to represent a tenant exclusively',
    sections: [
      {
        id: 'client',
        title: 'Client (Tenant)',
        icon: 'building',
        fields: [
          { id: 'clientEntity', label: 'Tenant entity', type: 'text', required: true, placeholder: 'Klear Industries, LLC', help: 'Legal entity that will sign. Match Secretary of State exactly.', span: 2 },
          { id: 'clientContact', label: 'Primary contact', type: 'text', required: true, placeholder: 'Eric Whelden', span: 1 },
          { id: 'clientTitle', label: 'Title', type: 'text', required: false, placeholder: 'COO', span: 1 },
          { id: 'clientEmail', label: 'Email', type: 'email', required: true, placeholder: 'eric@klearindustries.com', span: 1 },
          { id: 'clientPhone', label: 'Phone', type: 'tel', required: false, placeholder: '(612) 555-0142', span: 1 },
        ],
      },
      {
        id: 'engagement',
        title: 'Scope of Engagement',
        icon: 'compass',
        fields: [
          { id: 'propertyType', label: 'Target property type', type: 'select', required: true, options: ['Industrial Warehouse','Industrial Flex','Office','Retail / Bar / Restaurant','Mixed Use','Land','Government / Public'], span: 1 },
          { id: 'targetMarket', label: 'Geographic market', type: 'text', required: true, placeholder: 'Twin Cities Metro', help: 'Pre-filled from CEG boilerplate — edit only if the deal calls for it.', span: 1 },
          { id: 'sfMin', label: 'Min square footage', type: 'sf', required: true, placeholder: '10,000', span: 1 },
          { id: 'sfMax', label: 'Max square footage', type: 'sf', required: true, placeholder: '18,000', span: 1 },
          { id: 'occupancyDate', label: 'Target occupancy', type: 'date', required: false, span: 1 },
          { id: 'maxRent', label: 'Max budget (PSF)', type: 'currency', suffix: 'PSF/yr', required: false, placeholder: '14.00', span: 1 },
        ],
      },
      {
        id: 'terms',
        title: 'Engagement Terms',
        icon: 'file-text',
        fields: [
          { id: 'termMonths', label: 'Engagement term', type: 'select', required: true, options: ['6 months','12 months','18 months','24 months','Custom'], span: 1, coach: { tip: 'CEG standard for Tenant Rep is 12 months. Anything beyond 24mo requires Jeff to review.' } },
          { id: 'commissionPct', label: 'Commission', type: 'percent', required: true, placeholder: '6.0', span: 1, coach: { tip: 'Industrial tenant rep: 4–6% of aggregate base rent is standard.', example: 'Class A office: 5–6% · Industrial: 4–6% · Retail: 5–7%' } },
          { id: 'broker', label: 'CEG broker', type: 'select', required: true, options: ["Brady Erickson","Brian Lee","Brian Potratz","Curran Poganski","Diane Signorelli","Halie Aarestad","Jack Potratz","Jeff Salzbrun","Joseph Nickleson","Max Giblin","Mike Ruhland","Nicole Langer","Shannon Farrand"], span: 1 },
          { id: 'cobroker', label: 'Co-broker', type: 'select', options: ["None","Brady Erickson","Brian Lee","Brian Potratz","Curran Poganski","Diane Signorelli","Halie Aarestad","Jack Potratz","Jeff Salzbrun","Joseph Nickleson","Max Giblin","Mike Ruhland","Nicole Langer","Shannon Farrand","External"], span: 1 },
          { id: 'effectiveDate', label: 'Effective date', type: 'date', required: true, span: 1 },
        ],
      },
    ],
  },
  'list-lease': {
    title: 'Lease Listing Agreement',
    subtitle: 'Owner-side agreement to market available space',
    sections: [
      {
        id: 'property',
        title: 'Subject Property',
        icon: 'building-2',
        fields: [
          { id: 'propertyAddress', label: 'Property address', type: 'text', required: true, placeholder: '2400 Wacker Dr, Floor 14', span: 2 },
          { id: 'propertyCity', label: 'City', type: 'text', required: true, placeholder: 'Minneapolis', span: 1 },
          { id: 'propertyState', label: 'State', type: 'select', options: ['MN','WI','IA','ND','SD','Other'], required: true, span: 1 },
          { id: 'propertyZip', label: 'ZIP', type: 'text', required: true, placeholder: '55401', span: 1 },
          { id: 'propertyType', label: 'Property type', type: 'select', options: ['Industrial Warehouse','Industrial Flex','Office','Retail / Bar / Restaurant','Mixed Use','Land','Government / Public'], required: true, span: 1 },
          { id: 'totalSF', label: 'Total available SF', type: 'sf', required: true, placeholder: '14,200', span: 1 },
          { id: 'yearBuilt', label: 'Year built', type: 'number', placeholder: '2008', span: 1 },
        ],
      },
      {
        id: 'owner',
        title: 'Owner / Landlord',
        icon: 'briefcase',
        fields: [
          { id: 'ownerEntity', label: 'Owner entity', type: 'text', required: true, placeholder: 'Wacker Riverfront LLC', span: 2, help: 'Legal entity as it appears on title.' },
          { id: 'ownerContact', label: 'Primary contact', type: 'text', required: true, placeholder: 'Mark Allen', span: 1 },
          { id: 'ownerTitle', label: 'Title', type: 'text', placeholder: 'Managing Member', span: 1 },
          { id: 'ownerEmail', label: 'Email', type: 'email', required: true, placeholder: 'mark@wackerriver.com', span: 1 },
          { id: 'ownerPhone', label: 'Phone', type: 'tel', placeholder: '(612) 555-0188', span: 1 },
        ],
      },
      {
        id: 'terms',
        title: 'Listing Terms',
        icon: 'file-text',
        fields: [
          { id: 'askingRent', label: 'Asking rent', type: 'currency', suffix: 'PSF/yr', required: true, placeholder: '42.00', span: 1, coach: { tip: 'Class A office downtown Mpls: $38–46 PSF FSG is in range.', example: 'Industrial: $8–14 NNN · Office: $24–48 FSG · Retail: $18–36 NNN' } },
          { id: 'leaseType', label: 'Lease structure', type: 'select', options: ['Full-Service Gross','Modified Gross','NNN','Industrial Gross','Absolute Net'], required: true, span: 1 },
          { id: 'commission', label: 'Listing commission', type: 'percent', required: true, placeholder: '6.0', span: 1, coach: { tip: 'CEG standard for ERTL: 4–6% of aggregate base rent over initial term.' } },
          { id: 'cobroke', label: 'Co-broke commission', type: 'percent', placeholder: '3.0', span: 1, help: 'Paid to outside cooperating broker.' },
          { id: 'listingPeriod', label: 'Listing period', type: 'select', options: ['6 months','12 months','18 months','24 months','Custom'], required: true, span: 1 },
          { id: 'broker', label: 'CEG listing broker', type: 'select', options: ["Brady Erickson","Brian Lee","Brian Potratz","Curran Poganski","Diane Signorelli","Halie Aarestad","Jack Potratz","Jeff Salzbrun","Joseph Nickleson","Max Giblin","Mike Ruhland","Nicole Langer","Shannon Farrand"], required: true, span: 1 },
          { id: 'tiAllowance', label: 'TI allowance', type: 'currency', suffix: 'PSF', placeholder: '40.00', span: 1, help: 'Optional — leave blank if no TI offered.' },
          { id: 'effectiveDate', label: 'Effective date', type: 'date', required: true, span: 1 },
        ],
      },
    ],
  },
  'buyer-rep': {
    title: 'Buyer Representation Agreement',
    subtitle: 'Exclusive engagement to represent buyer in acquiring CRE',
    sections: [
      {
        id: 'client',
        title: 'Client (Buyer)',
        icon: 'key-round',
        fields: [
          { id: 'clientEntity', label: 'Buyer entity', type: 'text', required: true, placeholder: 'Stinski Holdings, LLC', span: 2 },
          { id: 'clientContact', label: 'Primary contact', type: 'text', required: true, placeholder: 'Dan Stinski', span: 1 },
          { id: 'clientEmail', label: 'Email', type: 'email', required: true, placeholder: 'dan@stinskiholdings.com', span: 1 },
        ],
      },
      {
        id: 'engagement',
        title: 'Acquisition Profile',
        icon: 'crosshair',
        fields: [
          { id: 'propertyType', label: 'Target property type', type: 'select', required: true, options: ['Industrial Warehouse','Industrial Flex','Office','Retail / Bar / Restaurant','Mixed Use','Land','Government / Public'], span: 1 },
          { id: 'targetMarket', label: 'Geographic market', type: 'text', required: true, placeholder: 'NE Minneapolis / I-94 corridor', span: 1 },
          { id: 'budgetLow', label: 'Budget — low', type: 'currency', suffix: 'USD', required: true, placeholder: '2,500,000', span: 1 },
          { id: 'budgetHigh', label: 'Budget — high', type: 'currency', suffix: 'USD', required: true, placeholder: '4,200,000', span: 1 },
        ],
      },
      {
        id: 'terms',
        title: 'Engagement Terms',
        icon: 'file-text',
        fields: [
          { id: 'termMonths', label: 'Engagement term', type: 'select', options: ['6 months','12 months','18 months','24 months'], required: true, span: 1 },
          { id: 'commissionPct', label: 'Commission', type: 'percent', required: true, placeholder: '3.0', span: 1, coach: { tip: 'Standard CEG buyer rep: 2–3% of purchase price, often paid by seller via co-op.' } },
          { id: 'broker', label: 'CEG broker', type: 'select', options: ["Brady Erickson","Brian Lee","Brian Potratz","Curran Poganski","Diane Signorelli","Halie Aarestad","Jack Potratz","Jeff Salzbrun","Joseph Nickleson","Max Giblin","Mike Ruhland","Nicole Langer","Shannon Farrand"], required: true, span: 1 },
          { id: 'effectiveDate', label: 'Effective date', type: 'date', required: true, span: 1 },
        ],
      },
    ],
  },
};

// Default fallback schema for the other doc types (so all picker cards lead somewhere usable)
const DEFAULT_SCHEMA = FORM_SCHEMAS['tenant-rep'];

// All three listing variants share the listing schema — fields cover both lease & sale dimensions
FORM_SCHEMAS['list-sale'] = {
  ...FORM_SCHEMAS['list-lease'],
  title: 'Sale Listing Agreement',
  subtitle: 'Owner-side agreement to market property for sale',
};
FORM_SCHEMAS['list-lease-sale'] = {
  ...FORM_SCHEMAS['list-lease'],
  title: 'Lease or Sale Listing Agreement',
  subtitle: 'Combined owner-side mandate — lease and sale at once',
};

function getSchema(docId) { return FORM_SCHEMAS[docId] || DEFAULT_SCHEMA; }

// ─── Draft templates (sectioned, with merge fields) ───────────────────────
// Each section: { id, title, body: (values) => JSX, aiNote?: string }
// Body uses values to render filled-in copy.

const DRAFT_TEMPLATES = {
  'tenant-rep': (v) => [
    {
      id: 'parties',
      title: 'Parties & Recitals',
      legalRef: '§ 1',
      body: (
        <>
          <p>
            This <strong>Exclusive Tenant Representation Agreement</strong> (this "Agreement") is entered into as of <Filled>{v.effectiveDate || '[Effective Date]'}</Filled> by and between <Filled>{v.clientEntity || '[Client Entity]'}</Filled> (the "Client" or "Tenant") and <strong>Commercial Equities Group, LLC</strong>, a Minnesota limited liability company doing business at 7900 International Drive, Suite 300, Edina, MN 55425 ("CEG" or "Broker").
          </p>
          <p>
            Tenant desires to engage CEG to identify, evaluate and negotiate the lease of commercial space described herein, and CEG agrees to perform such services on the terms set forth below.
          </p>
        </>
      ),
    },
    {
      id: 'scope',
      title: 'Scope of Representation',
      legalRef: '§ 2',
      body: (
        <>
          <p>
            CEG is hereby engaged on an <Filled>exclusive</Filled> basis to represent Tenant in connection with the identification and lease of <Filled>{v.propertyType || '[property type]'}</Filled> space ranging from approximately <Filled>{v.sfMin || '[min]'}</Filled> SF to <Filled>{v.sfMax || '[max]'}</Filled> SF within <Filled>{v.targetMarket || '[market]'}</Filled> (the "Engagement Area").
          </p>
          <p>
            CEG’s services shall include market survey, property tours, negotiation of letters of intent, support of definitive lease negotiations, and ordinary brokerage advisory consistent with CEG’s customary practice.
          </p>
        </>
      ),
    },
    {
      id: 'term',
      title: 'Term',
      legalRef: '§ 3',
      body: (
        <>
          <p>
            The term of this Agreement shall commence on <Filled>{v.effectiveDate || '[Effective Date]'}</Filled> and continue for <Filled>{v.termMonths || '12 months'}</Filled> thereafter (the "Term"), unless extended or earlier terminated as provided herein.
          </p>
          <p>
            CEG’s right to compensation shall survive the Term for any transaction with a Protected Party identified during the Term and consummated within one hundred eighty (180) days after expiration.
          </p>
        </>
      ),
      aiNote: 'Tail period (180 days) is CEG-standard. Edit only with Jeff’s approval.',
    },
    {
      id: 'compensation',
      title: 'Compensation',
      legalRef: '§ 4',
      body: (
        <>
          <p>
            Tenant’s landlord (or, failing that, Tenant) shall pay to CEG a commission equal to <Filled>{v.commissionPct ? v.commissionPct + '%' : '[6.0%]'}</Filled> of the aggregate base rent payable over the initial term of any lease executed pursuant to this Agreement, payable one-half (½) upon lease execution and one-half (½) upon Tenant’s occupancy.
          </p>
          <p>
            Where landlord publishes a commission schedule, CEG’s compensation shall not be less than the rate set forth above. Co-broker compensation, if any, is between CEG and the cooperating broker and shall not increase amounts due from Client.
          </p>
        </>
      ),
    },
    {
      id: 'confidentiality',
      title: 'Confidentiality & Conflicts',
      legalRef: '§ 5',
      body: (
        <>
          <p>
            CEG shall maintain the confidentiality of Tenant’s nonpublic business information. CEG may represent landlords in transactions unrelated to this Agreement and shall promptly disclose any actual or apparent conflict, allowing Client to elect to proceed with informed written consent or to engage independent representation.
          </p>
        </>
      ),
    },
    {
      id: 'termination',
      title: 'Termination',
      legalRef: '§ 6',
      body: (
        <>
          <p>
            Either party may terminate this Agreement upon thirty (30) days’ written notice. Termination shall not affect CEG’s right to compensation with respect to any transaction with a Protected Party as defined in § 3.
          </p>
        </>
      ),
      aiNote: 'Early-termination requires Jeff’s approval for anything shorter than 30 days.',
    },
    {
      id: 'signatures',
      title: 'Signatures',
      legalRef: '§ 7',
      body: (
        <>
          <p>The parties have executed this Agreement as of the date first written above.</p>
          <p>
            <strong>CLIENT — <Filled>{v.clientEntity || '[Client Entity]'}</Filled></strong><br/>
            By: ____________________________________<br/>
            Name: <Filled>{v.clientContact || '[Primary Contact]'}</Filled><br/>
            Title: <Filled>{v.clientTitle || '[Title]'}</Filled>
          </p>
          <p>
            <strong>BROKER — Commercial Equities Group, LLC</strong><br/>
            By: ____________________________________<br/>
            Name: <Filled>{v.broker || '[CEG Broker]'}</Filled><br/>
            Title: Broker | Investments
          </p>
        </>
      ),
    },
  ],
  'list-lease': (v) => [
    {
      id: 'parties',
      title: 'Parties & Property',
      legalRef: '§ 1',
      body: (
        <>
          <p>
            This <strong>Lease Listing Agreement</strong> (this "Agreement") is entered into as of <Filled>{v.effectiveDate || '[Effective Date]'}</Filled> by and between <Filled>{v.ownerEntity || '[Owner Entity]'}</Filled> ("Owner") and <strong>Commercial Equities Group, LLC</strong> ("CEG" or "Broker").
          </p>
          <p>
            Owner is the fee owner of the property commonly known as <Filled>{[v.propertyAddress, v.propertyCity, v.propertyState, v.propertyZip].filter(Boolean).join(', ') || '[Property Address]'}</Filled>, comprising approximately <Filled>{v.totalSF || '[SF]'}</Filled> SF of <Filled>{(v.propertyType || '[property type]').toLowerCase()}</Filled> space (the "Property").
          </p>
        </>
      ),
    },
    {
      id: 'grant',
      title: 'Exclusive Grant',
      legalRef: '§ 2',
      body: (
        <>
          <p>
            Owner hereby grants to CEG the <strong>exclusive right</strong> to market and lease the Property during the Term hereof. Owner shall refer to CEG all inquiries received concerning the Property and shall not engage another broker for any portion of the Property during the Term.
          </p>
        </>
      ),
    },
    {
      id: 'rent',
      title: 'Asking Rent & Structure',
      legalRef: '§ 3',
      body: (
        <>
          <p>
            The asking rent for the Property is <Filled>{v.askingRent ? '$' + v.askingRent + ' PSF/yr' : '[$ PSF/yr]'}</Filled> on a <Filled>{v.leaseType || '[Lease Structure]'}</Filled> basis. <Filled>{v.tiAllowance ? 'Owner shall offer a tenant improvement allowance of up to $' + v.tiAllowance + ' PSF, subject to lease negotiation.' : ''}</Filled>
          </p>
          <p>
            CEG may, in consultation with Owner, propose adjustments to asking rent based on market response during the Term.
          </p>
        </>
      ),
    },
    {
      id: 'term',
      title: 'Listing Period',
      legalRef: '§ 4',
      body: (
        <>
          <p>
            The Term of this Agreement shall commence on <Filled>{v.effectiveDate || '[Effective Date]'}</Filled> and continue for <Filled>{v.listingPeriod || '12 months'}</Filled> (the "Listing Period").
          </p>
          <p>
            CEG’s right to commission shall extend to any lease executed within one hundred eighty (180) days after expiration with any tenant first introduced to the Property by CEG.
          </p>
        </>
      ),
    },
    {
      id: 'commission',
      title: 'Commission',
      legalRef: '§ 5',
      body: (
        <>
          <p>
            Owner shall pay to CEG a commission equal to <Filled>{v.commission ? v.commission + '%' : '[6.0%]'}</Filled> of the aggregate base rent payable over the initial term of any lease executed pursuant to this Agreement. <Filled>{v.cobroke ? 'CEG shall offer cooperating brokers a co-broke commission of ' + v.cobroke + '% of aggregate base rent.' : ''}</Filled>
          </p>
          <p>
            Commission is payable one-half (½) upon lease execution and one-half (½) upon tenant occupancy.
          </p>
        </>
      ),
      aiNote: 'CEG-approved range: 4–6% total listing commission. Deviations require Jeff.',
    },
    {
      id: 'marketing',
      title: 'Marketing & Disclosure',
      legalRef: '§ 6',
      body: (
        <>
          <p>
            CEG shall market the Property using its customary channels including Crexi, LoopNet, CoStar, the CEG marketing program, and direct broker outreach. Owner authorizes use of property photography, signage, and listing data.
          </p>
        </>
      ),
    },
    {
      id: 'signatures',
      title: 'Signatures',
      legalRef: '§ 7',
      body: (
        <>
          <p>The parties have executed this Agreement as of the date first written above.</p>
          <p>
            <strong>OWNER — <Filled>{v.ownerEntity || '[Owner Entity]'}</Filled></strong><br/>
            By: ____________________________________<br/>
            Name: <Filled>{v.ownerContact || '[Primary Contact]'}</Filled><br/>
            Title: <Filled>{v.ownerTitle || '[Title]'}</Filled>
          </p>
          <p>
            <strong>BROKER — Commercial Equities Group, LLC</strong><br/>
            By: ____________________________________<br/>
            Name: <Filled>{v.broker || '[CEG Broker]'}</Filled><br/>
            Title: Broker | Investments
          </p>
        </>
      ),
    },
  ],
  'buyer-rep': (v) => [
    {
      id: 'parties',
      title: 'Parties & Recitals',
      legalRef: '§ 1',
      body: (
        <>
          <p>
            This <strong>Exclusive Buyer Representation Agreement</strong> is entered into as of <Filled>{v.effectiveDate || '[Effective Date]'}</Filled> by and between <Filled>{v.clientEntity || '[Buyer Entity]'}</Filled> ("Buyer") and <strong>Commercial Equities Group, LLC</strong> ("CEG").
          </p>
          <p>
            Buyer engages CEG on an exclusive basis to identify and assist in the acquisition of commercial real estate within the parameters defined herein.
          </p>
        </>
      ),
    },
    {
      id: 'scope',
      title: 'Acquisition Parameters',
      legalRef: '§ 2',
      body: (
        <>
          <p>
            CEG shall use commercially reasonable efforts to identify <Filled>{(v.propertyType || '[property type]').toLowerCase()}</Filled> opportunities within <Filled>{v.targetMarket || '[market]'}</Filled> priced between <Filled>{v.budgetLow ? '$' + v.budgetLow : '[low]'}</Filled> and <Filled>{v.budgetHigh ? '$' + v.budgetHigh : '[high]'}</Filled>.
          </p>
        </>
      ),
    },
    {
      id: 'term',
      title: 'Term',
      legalRef: '§ 3',
      body: (
        <p>
          The Term of this Agreement shall be <Filled>{v.termMonths || '12 months'}</Filled> commencing on <Filled>{v.effectiveDate || '[Effective Date]'}</Filled>. CEG’s right to commission shall survive for 180 days post-Term for any Protected Property introduced during the Term.
        </p>
      ),
    },
    {
      id: 'commission',
      title: 'Commission',
      legalRef: '§ 4',
      body: (
        <p>
          Compensation shall equal <Filled>{v.commissionPct ? v.commissionPct + '%' : '[3.0%]'}</Filled> of the gross purchase price. Where the seller pays a cooperating-broker commission, such amount shall be credited against the obligation owed by Buyer.
        </p>
      ),
      aiNote: 'Standard buyer-rep range: 2–3%. Outside this range routes to Halie for review.',
    },
    {
      id: 'signatures',
      title: 'Signatures',
      legalRef: '§ 5',
      body: (
        <>
          <p>The parties have executed this Agreement as of the date first written above.</p>
          <p>
            <strong>BUYER — <Filled>{v.clientEntity || '[Buyer Entity]'}</Filled></strong><br/>
            By: ____________________________________<br/>
            Name: <Filled>{v.clientContact || '[Primary Contact]'}</Filled>
          </p>
          <p>
            <strong>BROKER — Commercial Equities Group, LLC</strong><br/>
            By: ____________________________________<br/>
            Name: <Filled>{v.broker || '[CEG Broker]'}</Filled>
          </p>
        </>
      ),
    },
  ],
};

function Filled({ children }) {
  if (children == null || children === '' || (typeof children === 'string' && children.startsWith('['))) {
    return <em className="filled" style={{color:'var(--ink-400)', fontStyle:'italic', borderBottomStyle:'dashed', background:'transparent'}}>{children || '[—]'}</em>;
  }
  return <span className="filled">{children}</span>;
}

function getDraft(docId, values) {
  let fn = DRAFT_TEMPLATES[docId];
  if (!fn && docId && docId.startsWith('list-')) fn = DRAFT_TEMPLATES['list-lease'];
  if (!fn) fn = DRAFT_TEMPLATES['tenant-rep'];
  return fn(values || {});
}

// Defaults to prefill the form so prototype feels real
const DEMO_VALUES = {
  'tenant-rep': {
    clientEntity: 'Klear Industries, LLC',
    clientContact: 'Eric Whelden',
    clientTitle: 'COO',
    clientEmail: 'eric@klearindustries.com',
    clientPhone: '(612) 555-0142',
    propertyType: 'Industrial Warehouse',
    targetMarket: 'NE Minneapolis (Lowry to Broadway)',
    sfMin: '10,000',
    sfMax: '18,000',
    occupancyDate: '2026-09-01',
    maxRent: '12.50',
    termMonths: '12 months',
    commissionPct: '6.0',
    broker: 'Brian Potratz',
    cobroker: 'None',
    effectiveDate: '2026-05-15',
  },
  'list-lease': {
    propertyAddress: '2400 Wacker Dr, Floor 14',
    propertyCity: 'Minneapolis',
    propertyState: 'MN',
    propertyZip: '55401',
    propertyType: 'Office',
    totalSF: '14,200',
    yearBuilt: '2008',
    ownerEntity: 'Wacker Riverfront LLC',
    ownerContact: 'Mark Allen',
    ownerTitle: 'Managing Member',
    ownerEmail: 'mark@wackerriver.com',
    ownerPhone: '(612) 555-0188',
    askingRent: '42.00',
    leaseType: 'Full-Service Gross',
    commission: '6.0',
    cobroke: '3.0',
    listingPeriod: '12 months',
    broker: 'Jeff Salzbrun',
    tiAllowance: '40.00',
    effectiveDate: '2026-05-15',
  },
  'buyer-rep': {
    clientEntity: 'Stinski Holdings, LLC',
    clientContact: 'Dan Stinski',
    clientEmail: 'dan@stinskiholdings.com',
    propertyType: 'Industrial Warehouse',
    targetMarket: 'NE Minneapolis / I-94 corridor',
    budgetLow: '2,500,000',
    budgetHigh: '4,200,000',
    termMonths: '12 months',
    commissionPct: '3.0',
    broker: 'Brian Potratz',
    effectiveDate: '2026-05-15',
  },
};

Object.assign(window, {
  DOC_CATEGORIES, ALL_DOCS, DOC_BY_ID,
  RECENT_DRAFTS,
  FORM_SCHEMAS, getSchema,
  DRAFT_TEMPLATES, getDraft, Filled,
  DEMO_VALUES,
});
