#!/usr/bin/env python3
"""Build Joe Lynch CEO Compensation Justification memo."""

import sys
sys.path.insert(0, '/Users/joemac/.openclaw/workspace')

from docx import Document
from docx.shared import Pt, RGBColor, Inches, Cm
from docx.enum.text import WD_ALIGN_PARAGRAPH
from docx.enum.style import WD_STYLE_TYPE
from docx.oxml.ns import qn
from docx.oxml import OxmlElement
import copy

OUTPUT_PATH = '/Users/joemac/.openclaw/workspace/projects/joe-compensation-justification.docx'

# Colors
NAVY = RGBColor(0x00, 0x2B, 0x5C)
GOLD = RGBColor(0xC5, 0x9E, 0x3C)
BLACK = RGBColor(0x1A, 0x1A, 0x1A)
DARK_GRAY = RGBColor(0x40, 0x40, 0x40)

def set_font(run, name='Calibri', size=11, bold=False, color=None, italic=False):
    run.font.name = name
    run.font.size = Pt(size)
    run.font.bold = bold
    run.font.italic = italic
    if color:
        run.font.color.rgb = color

def add_heading(doc, text, level=1, space_before=14, space_after=4):
    para = doc.add_paragraph()
    para.paragraph_format.space_before = Pt(space_before)
    para.paragraph_format.space_after = Pt(space_after)
    para.paragraph_format.keep_with_next = True
    run = para.add_run(text.upper())
    run.font.name = 'Calibri'
    run.font.size = Pt(11) if level == 2 else Pt(12)
    run.font.bold = True
    run.font.color.rgb = NAVY
    # Add gold bottom border via XML
    pPr = para._p.get_or_add_pPr()
    pBdr = OxmlElement('w:pBdr')
    bottom = OxmlElement('w:bottom')
    bottom.set(qn('w:val'), 'single')
    bottom.set(qn('w:sz'), '4')
    bottom.set(qn('w:space'), '1')
    bottom.set(qn('w:color'), 'C59E3C')
    pBdr.append(bottom)
    pPr.append(pBdr)
    return para

def add_body(doc, text, space_before=0, space_after=6, indent=False):
    para = doc.add_paragraph()
    para.paragraph_format.space_before = Pt(space_before)
    para.paragraph_format.space_after = Pt(space_after)
    if indent:
        para.paragraph_format.left_indent = Inches(0.25)
    run = para.add_run(text)
    run.font.name = 'Calibri'
    run.font.size = Pt(10.5)
    run.font.color.rgb = BLACK
    return para

def add_bullet(doc, text, bold_prefix=None):
    para = doc.add_paragraph(style='List Bullet')
    para.paragraph_format.space_before = Pt(2)
    para.paragraph_format.space_after = Pt(4)
    para.paragraph_format.left_indent = Inches(0.35)
    if bold_prefix:
        r1 = para.add_run(bold_prefix)
        r1.font.name = 'Calibri'
        r1.font.size = Pt(10.5)
        r1.font.bold = True
        r1.font.color.rgb = NAVY
        r2 = para.add_run(text)
        r2.font.name = 'Calibri'
        r2.font.size = Pt(10.5)
        r2.font.color.rgb = BLACK
    else:
        run = para.add_run(text)
        run.font.name = 'Calibri'
        run.font.size = Pt(10.5)
        run.font.color.rgb = BLACK
    return para

def add_page_numbers(doc):
    """Add page numbers to the footer."""
    section = doc.sections[0]
    footer = section.footer
    para = footer.paragraphs[0]
    para.alignment = WD_ALIGN_PARAGRAPH.CENTER
    run = para.add_run()
    run.font.name = 'Calibri'
    run.font.size = Pt(9)
    run.font.color.rgb = DARK_GRAY
    # Add field for page number
    fldChar1 = OxmlElement('w:fldChar')
    fldChar1.set(qn('w:fldCharType'), 'begin')
    instrText = OxmlElement('w:instrText')
    instrText.text = 'PAGE'
    fldChar2 = OxmlElement('w:fldChar')
    fldChar2.set(qn('w:fldCharType'), 'end')
    run._r.append(fldChar1)
    run._r.append(instrText)
    run._r.append(fldChar2)


doc = Document()

# Page margins
section = doc.sections[0]
section.top_margin = Inches(1.0)
section.bottom_margin = Inches(1.0)
section.left_margin = Inches(1.15)
section.right_margin = Inches(1.15)

# ── HEADER LOGO BAR (navy rectangle effect via paragraph shading) ──
header_para = doc.add_paragraph()
header_para.paragraph_format.space_before = Pt(0)
header_para.paragraph_format.space_after = Pt(2)
pPr = header_para._p.get_or_add_pPr()
shd = OxmlElement('w:shd')
shd.set(qn('w:val'), 'clear')
shd.set(qn('w:color'), 'auto')
shd.set(qn('w:fill'), '002B5C')
pPr.append(shd)
r = header_para.add_run('  TUCKERTON GROUP  ·  TLC II LLC')
r.font.name = 'Calibri'
r.font.size = Pt(14)
r.font.bold = True
r.font.color.rgb = RGBColor(0xFF, 0xFF, 0xFF)

# Gold accent line
accent_para = doc.add_paragraph()
accent_para.paragraph_format.space_before = Pt(0)
accent_para.paragraph_format.space_after = Pt(12)
pPr2 = accent_para._p.get_or_add_pPr()
shd2 = OxmlElement('w:shd')
shd2.set(qn('w:val'), 'clear')
shd2.set(qn('w:color'), 'auto')
shd2.set(qn('w:fill'), 'C59E3C')
pPr2.append(shd2)
accent_para.add_run('  ')

# ── MEMO HEADER ──
memo_title = doc.add_paragraph()
memo_title.paragraph_format.space_after = Pt(12)
r = memo_title.add_run('MEMORANDUM')
r.font.name = 'Calibri'
r.font.size = Pt(18)
r.font.bold = True
r.font.color.rgb = NAVY

def memo_line(doc, label, value):
    para = doc.add_paragraph()
    para.paragraph_format.space_before = Pt(0)
    para.paragraph_format.space_after = Pt(3)
    r1 = para.add_run(label + '  ')
    r1.font.name = 'Calibri'
    r1.font.size = Pt(10.5)
    r1.font.bold = True
    r1.font.color.rgb = NAVY
    r2 = para.add_run(value)
    r2.font.name = 'Calibri'
    r2.font.size = Pt(10.5)
    r2.font.color.rgb = BLACK

memo_line(doc, 'TO:', 'Robert MacArthur, CPA')
memo_line(doc, 'FROM:', 'Joseph Lynch, Managing Member — TLC II LLC')
memo_line(doc, 'RE:', 'Justification for Managing Member Compensation — Tuckerton Group')
memo_line(doc, 'DATE:', 'April 2026')
memo_line(doc, 'CLASSIFICATION:', 'Confidential')

# Divider
div = doc.add_paragraph()
div.paragraph_format.space_before = Pt(10)
div.paragraph_format.space_after = Pt(10)
pPr3 = div._p.get_or_add_pPr()
pBdr = OxmlElement('w:pBdr')
bottom = OxmlElement('w:bottom')
bottom.set(qn('w:val'), 'single')
bottom.set(qn('w:sz'), '8')
bottom.set(qn('w:space'), '1')
bottom.set(qn('w:color'), '002B5C')
pBdr.append(bottom)
pPr3.append(pBdr)

# ── SECTION 1: EXECUTIVE SUMMARY ──
add_heading(doc, 'I.  Executive Summary')
add_body(doc, (
    'My current management fee of $175,000 annually, while appropriate during the stabilization '
    'and turnaround phase that began in July 2022, no longer reflects the market value of services '
    'rendered, the scope of responsibilities now carried, or the financial outcomes delivered to the '
    'members of TLC II LLC.'
), space_after=8)
add_body(doc, (
    'The purpose of this memorandum is to document a compensation adjustment that accurately '
    'reflects CEO-equivalent responsibilities across a multi-entity operating enterprise — and to '
    'establish a W-2 salary structure that supports meaningful, tax-advantaged retirement plan '
    'contributions going forward.'
), space_after=8)

# ── SECTION 2: THE ROLE ──
add_heading(doc, 'II.  The Role: Not a Store Manager — A CEO')
add_body(doc, (
    'As Managing Member of TLC II LLC, I serve as the sole executive of a multi-entity holding '
    'company with no co-executive, no CFO on payroll, and no COO. I carry the full weight of '
    'every C-suite function across the following operating entities:'
), space_after=8)

add_bullet(doc, 'Tuckerton Lumber Company', bold_prefix='')
# rewrite with proper bold prefix
doc.paragraphs[-1].clear()
p = doc.paragraphs[-1]
r1 = p.add_run('Tuckerton Lumber Company  ')
r1.font.name = 'Calibri'; r1.font.size = Pt(10.5); r1.font.bold = True; r1.font.color.rgb = NAVY
r2 = p.add_run('— A 90+ year-old legacy business operating from two locations (Tuckerton and Surf City), carrying the Benjamin Moore brand, running the Epicor ERP platform, and generating approximately $16–17M in annual revenue.')
r2.font.name = 'Calibri'; r2.font.size = Pt(10.5); r2.font.color.rgb = BLACK

add_bullet(doc, 'Surfbox Portable Storage', bold_prefix='')
doc.paragraphs[-1].clear()
p = doc.paragraphs[-1]
r1 = p.add_run('Surfbox Portable Storage  ')
r1.font.name = 'Calibri'; r1.font.size = Pt(10.5); r1.font.bold = True; r1.font.color.rgb = NAVY
r2 = p.add_run('— Built from zero to approaching $1M in annual revenue. I own all operations, customer relationships, pricing strategy, and growth direction.')
r2.font.name = 'Calibri'; r2.font.size = Pt(10.5); r2.font.color.rgb = BLACK

add_bullet(doc, 'TLC 539 LLC  — Commercial real estate holdings.', bold_prefix='')
add_bullet(doc, 'TLC Land Holding LLC  — Strategic land assets.', bold_prefix='')
add_bullet(doc, 'TLC II LLC  — Parent entity; 50% ownership with minority shareholders present.', bold_prefix='')

add_body(doc, (
    'Across these entities, I function simultaneously as Chief Executive Officer, Chief Financial '
    'Officer, Chief Operating Officer, HR Director, Chief Strategy Officer, Legal Liaison, and Real '
    'Estate Developer. This is not a hyperbole — it is the operational reality of running a lean, '
    'privately held enterprise with no redundant executive overhead.'
), space_before=8, space_after=8)

# ── SECTION 3: WHAT HAS BEEN DELIVERED ──
add_heading(doc, 'III.  What Has Been Delivered')
add_body(doc, 'Over the past four years, the following outcomes have been achieved:', space_after=6)

bullets_s3 = [
    ('Preserved and stabilized a 90-year-old institution ', 'through post-COVID lumber market volatility — protecting jobs, supplier relationships, and community standing.'),
    ('Built Surfbox Portable Storage from concept to nearly $1M in revenue ', '— a new profit center created from scratch with no outside capital.'),
    ('Grew members\' equity from ~$1.4M to $1.5M+ ', 'while distributing over $212,000 to shareholders.'),
    ('Delivered $737K+ in operating income on $16.5M in revenue ', '— holding the line through one of the most volatile cost environments in the industry\'s history.'),
    ('Strategically positioned real estate assets ', 'that have appreciated meaningfully since acquisition.'),
    ('Resolved complex HR, zoning compliance, and capital allocation matters ', 'without reliance on outside consultants — saving the business material professional fees.'),
]

for bold_part, rest in bullets_s3:
    para = doc.add_paragraph(style='List Bullet')
    para.paragraph_format.space_before = Pt(2)
    para.paragraph_format.space_after = Pt(5)
    para.paragraph_format.left_indent = Inches(0.35)
    r1 = para.add_run(bold_part)
    r1.font.name = 'Calibri'; r1.font.size = Pt(10.5); r1.font.bold = True; r1.font.color.rgb = NAVY
    r2 = para.add_run(rest)
    r2.font.name = 'Calibri'; r2.font.size = Pt(10.5); r2.font.color.rgb = BLACK

# ── SECTION 4: MARKET COMPARABLES ──
add_heading(doc, 'IV.  Market Comparables')
add_body(doc, (
    'Compensation benchmarks for privately held businesses of comparable scale and complexity '
    'support the following ranges for a CEO/Managing Member role:'
), space_after=8)

comps = [
    ('Independent Hardware & Lumber Dealer CEO', '(~$15–20M revenue, NRHA Retail Compensation Study)', '$225,000 – $325,000'),
    ('Regional Portable Storage Company CEO', '($1M–$5M revenue, industry composite)', '$175,000 – $250,000'),
    ('Multi-Entity LLC Managing Member', '(real estate + operations, market composite)', '$200,000 – $300,000'),
    ('General Manager / CEO, Private Company', '(BLS / Robert Half, comparable revenue)', '$220,000 – $280,000'),
]

for role, source, range_str in comps:
    para = doc.add_paragraph()
    para.paragraph_format.space_before = Pt(3)
    para.paragraph_format.space_after = Pt(5)
    para.paragraph_format.left_indent = Inches(0.2)
    r1 = para.add_run(role + '  ')
    r1.font.name = 'Calibri'; r1.font.size = Pt(10.5); r1.font.bold = True; r1.font.color.rgb = NAVY
    r2 = para.add_run(source + '\n')
    r2.font.name = 'Calibri'; r2.font.size = Pt(10); r2.font.italic = True; r2.font.color.rgb = DARK_GRAY
    r3 = para.add_run('    Market Range:  ')
    r3.font.name = 'Calibri'; r3.font.size = Pt(10.5); r3.font.bold = False; r3.font.color.rgb = BLACK
    r4 = para.add_run(range_str)
    r4.font.name = 'Calibri'; r4.font.size = Pt(10.5); r4.font.bold = True; r4.font.color.rgb = GOLD

add_body(doc, (
    'At $175,000, my current management fee represents approximately 60–70% of the market '
    'rate for a CEO of comparable scope and complexity. The adjustment proposed below closes '
    'that gap in a measured, financially defensible way.'
), space_before=10, space_after=8)

# ── SECTION 5: PROPOSED COMPENSATION ──
add_heading(doc, 'V.  Proposed Compensation Structure')
add_body(doc, 'The following structure is proposed, effective upon formalization:', space_after=8)

prop_items = [
    ('Year 1 — $225,000 W-2 Salary:', ' Managing Member compensation, replacing the current management fee structure. This reflects the lower bound of market rate and remains well within the financial capacity of the business.'),
    ('Year 2 Target — $250,000+:', ' Subject to performance benchmarks, including Surfbox revenue milestones and TLC operating margin targets.'),
    ('Annual Performance Bonus:', ' Discretionary bonus tied to Surfbox growth milestones and TLC II LLC margin performance. Structure and triggers to be defined collaboratively.'),
    ('W-2 Structure — Retirement Benefit:', ' Converting from a management fee to W-2 compensation unlocks meaningful retirement plan contributions — including a Solo 401(k) or Defined Benefit Plan — generating significant tax efficiency and long-term wealth building for the entity and its Managing Member. Estimated annual retirement contribution capacity: $62,000+.'),
]

for bold_label, rest in prop_items:
    para = doc.add_paragraph(style='List Bullet')
    para.paragraph_format.space_before = Pt(2)
    para.paragraph_format.space_after = Pt(6)
    para.paragraph_format.left_indent = Inches(0.35)
    r1 = para.add_run(bold_label)
    r1.font.name = 'Calibri'; r1.font.size = Pt(10.5); r1.font.bold = True; r1.font.color.rgb = NAVY
    r2 = para.add_run(rest)
    r2.font.name = 'Calibri'; r2.font.size = Pt(10.5); r2.font.color.rgb = BLACK

# ── SECTION 6: CLOSING ──
add_heading(doc, 'VI.  Conclusion')
add_body(doc, (
    'The adjustment proposed in this memorandum is modest relative to market, financially '
    'supportable by the business at its current performance level, and an appropriate '
    'recognition of the value created through four years of active stewardship.'
), space_after=8)
add_body(doc, (
    'This memo is intended to provide the documentation necessary to formalize the '
    'compensation structure going forward, and to support any related planning work on '
    'the retirement and tax strategy side.'
), space_after=20)

# Signature block
sig_para = doc.add_paragraph()
sig_para.paragraph_format.space_before = Pt(10)
sig_para.paragraph_format.space_after = Pt(4)
r = sig_para.add_run('Respectfully submitted,')
r.font.name = 'Calibri'; r.font.size = Pt(10.5); r.font.color.rgb = BLACK

sig_name = doc.add_paragraph()
sig_name.paragraph_format.space_before = Pt(16)
sig_name.paragraph_format.space_after = Pt(2)
r = sig_name.add_run('Joseph Lynch')
r.font.name = 'Calibri'; r.font.size = Pt(12); r.font.bold = True; r.font.color.rgb = NAVY

sig_title = doc.add_paragraph()
sig_title.paragraph_format.space_before = Pt(0)
sig_title.paragraph_format.space_after = Pt(2)
r = sig_title.add_run('Managing Member, TLC II LLC')
r.font.name = 'Calibri'; r.font.size = Pt(10.5); r.font.color.rgb = BLACK

sig_entities = doc.add_paragraph()
sig_entities.paragraph_format.space_before = Pt(0)
sig_entities.paragraph_format.space_after = Pt(2)
r = sig_entities.add_run('Tuckerton Lumber Company  |  Surfbox Portable Storage')
r.font.name = 'Calibri'; r.font.size = Pt(10); r.font.italic = True; r.font.color.rgb = DARK_GRAY

# Page numbers in footer
add_page_numbers(doc)

doc.save(OUTPUT_PATH)
print(f"Document saved: {OUTPUT_PATH}")

# ── SEND EMAIL ──
from scripts.send_email import send_email

body = """Joe,

Please find attached the CEO Compensation Justification memo prepared for Robert MacArthur, CPA.

This document makes the case for adjusting your managing member compensation to $225,000 (Year 1) — positioning it as CEO-equivalent pay for a multi-entity holding company, supported by market comparables and the financial outcomes you've delivered.

Key points:
• Current $175K = ~60-70% of market rate for comparable CEO scope
• Proposed Year 1: $225,000 W-2 salary
• W-2 structure unlocks ~$62K/year in Roth 401(k) contributions
• Simple format per MacArthur's feedback — no data overload

Let me know if any adjustments are needed before sending to MacArthur.

— Rob 🦞
"""

result = send_email(
    to='jlynch@tlcnj.com',
    subject='CEO Compensation Justification — Tuckerton Group (Draft for MacArthur)',
    body=body,
    attachment_path=OUTPUT_PATH
)
print(f"Email sent! ID: {result['id']}")
