#!/usr/bin/env python3
"""Create the Takeover Pool Vision Doc in Word format with clean, readable tables."""

from docx import Document
from docx.shared import Inches, Pt, RGBColor
from docx.enum.text import WD_ALIGN_PARAGRAPH
from docx.enum.table import WD_TABLE_ALIGNMENT
from docx.oxml.ns import qn

doc = Document()

# ---- Styles ----
style = doc.styles['Normal']
font = style.font
font.name = 'Calibri'
font.size = Pt(11)

def add_heading_styled(text, level=1):
    h = doc.add_heading(text, level=level)
    for run in h.runs:
        run.font.color.rgb = RGBColor(0xC0, 0x39, 0x2B)  # Red
    return h

def add_table_styled(headers, rows):
    table = doc.add_table(rows=1 + len(rows), cols=len(headers))
    table.style = 'Light Grid Accent 1'
    table.alignment = WD_TABLE_ALIGNMENT.CENTER
    
    # Header row
    for i, header in enumerate(headers):
        cell = table.rows[0].cells[i]
        cell.text = header
        for paragraph in cell.paragraphs:
            paragraph.alignment = WD_ALIGN_PARAGRAPH.CENTER
            for run in paragraph.runs:
                run.bold = True
                run.font.size = Pt(10)
    
    # Data rows
    for r_idx, row in enumerate(rows):
        for c_idx, val in enumerate(row):
            cell = table.rows[r_idx + 1].cells[c_idx]
            cell.text = str(val)
            for paragraph in cell.paragraphs:
                paragraph.alignment = WD_ALIGN_PARAGRAPH.CENTER
                for run in paragraph.runs:
                    run.font.size = Pt(10)
    
    doc.add_paragraph()  # spacing
    return table

# ============================================================
# TITLE
# ============================================================
title = doc.add_heading('LOBSTER TAKEOVER POOL 2026', level=0)
for run in title.runs:
    run.font.color.rgb = RGBColor(0xC0, 0x39, 0x2B)
title.alignment = WD_ALIGN_PARAGRAPH.CENTER

subtitle = doc.add_paragraph('FIFA World Cup 2026 — Capture Pool')
subtitle.alignment = WD_ALIGN_PARAGRAPH.CENTER
subtitle.runs[0].font.size = Pt(14)
subtitle.runs[0].font.color.rgb = RGBColor(0x7F, 0x8C, 0x8D)

doc.add_paragraph()

# ============================================================
# THE CONCEPT
# ============================================================
add_heading_styled('THE CONCEPT', 1)
doc.add_paragraph(
    'A chaos-driven team ownership pool for the 2026 FIFA World Cup. '
    'Unlike the Survivor Pool (where you pick teams), in the Takeover Pool '
    'you are ASSIGNED teams randomly — and ownership can change hands every '
    'single match based on the point spread. You could start with Morocco '
    'and end up owning Brazil by the Final.'
)
doc.add_paragraph(
    'The person who OWNS the World Cup Champion at the final whistle wins the pot. '
    "It doesn't matter if you started with that team or captured them in the semifinal."
)

# ============================================================
# HOW IT WORKS
# ============================================================
add_heading_styled('HOW IT WORKS — STEP BY STEP', 1)

# STEP 1
add_heading_styled('Step 1: The Random Draft', 2)
doc.add_paragraph(
    'Before the tournament starts, all 48 teams are assigned to players via random draw. '
    'The number of teams each player gets depends on how many people are in the pool.'
)

add_table_styled(
    ['Players', 'Teams Per Player', 'Example'],
    [
        ['48', '1 team each', 'Perfect — one team, one owner'],
        ['24', '2 teams each', 'Good size — manageable portfolio'],
        ['16', '3 teams each', 'Every game matters to you'],
        ['12', '4 teams each', 'You own a mini-empire'],
    ]
)

doc.add_paragraph(
    'The draw is random and public. No picking, no strategy, no complaining. '
    'You get who you get. That\'s the game.'
)

# STEP 2
add_heading_styled('Step 2: Group Stage — Takeover Rules', 2)
doc.add_paragraph(
    'The 2026 World Cup has 12 groups of 4 teams. Each group plays round-robin '
    '(3 games per team). Here is how takeover works during group stage:'
)

p = doc.add_paragraph()
p.add_run('Every match has a SPREAD').bold = True
p = doc.add_paragraph(
    'The spread is the expected goal difference. For example, if Brazil plays '
    'Costa Rica, Brazil might be -2.5 (meaning Brazil is expected to win by 3+ goals). '
    'Spreads are set using Vegas lines or FIFA rankings.'
)

p = doc.add_paragraph()
p.add_run('The Takeover Mechanic:').bold = True

doc.add_paragraph('If the FAVORED team COVERS the spread → No takeover. Both teams stay with their current owners.', style='List Bullet')
doc.add_paragraph('If the FAVORED team FAILS to cover the spread → The underdog\'s owner CAPTURES the favored team.', style='List Bullet')
doc.add_paragraph('If the game is a DRAW → No takeover. Both teams stay put.', style='List Bullet')

doc.add_paragraph()
p = doc.add_paragraph()
p.add_run('Example:').bold = True
doc.add_paragraph(
    'Brazil (-2.5) vs Costa Rica (+2.5)\n'
    '• Brazil wins 4-0 → Brazil covered (-2.5). No takeover. Both stay.\n'
    '• Brazil wins 1-0 → Brazil did NOT cover (-2.5). Costa Rica\'s owner CAPTURES Brazil.\n'
    '• Draw 0-0 → No takeover.'
)

p = doc.add_paragraph()
p.add_run('KEY RULE: A player CAN own multiple teams.').bold = True
doc.add_paragraph(
    'If you start with Costa Rica and Morocco, and both pull off upsets against '
    'favored teams, you now own 4 teams. By the knockout stage, one player could '
    'own 5+ teams while another has been stripped down to 1. That\'s the chaos.'
)

# STEP 3
add_heading_styled('Step 3: Knockout Stage — Takeover + Elimination', 2)
doc.add_paragraph(
    'Starting at the Round of 32, teams are eliminated when they lose. '
    'Takeover still applies via the spread:'
)

doc.add_paragraph('Favored team COVERS the spread and WINS → Winner stays with the favored team\'s owner. Losing team is eliminated.', style='List Bullet')
doc.add_paragraph('Favored team WINS but FAILS to cover → The underdog\'s owner CAPTURES the winning team. Losing team eliminated.', style='List Bullet')
doc.add_paragraph('Underdog WINS outright → Underdog\'s owner keeps their team AND captures any other teams the favored team\'s owner had (optional: empire collapse rule).', style='List Bullet')

doc.add_paragraph()
p = doc.add_paragraph()
p.add_run('Example — Quarterfinal:').bold = True
doc.add_paragraph(
    'Player A owns Brazil (-1.5) | Player B owns France (+1.5)\n'
    '• Brazil wins 2-0 → Covers -1.5. Player A keeps Brazil. France is eliminated.\n'
    '• Brazil wins 1-0 → Does NOT cover -1.5. Player B CAPTURES Brazil from Player A. France is eliminated. Player B now owns Brazil going forward.\n'
    '• France wins 2-1 → Outright upset. Player B keeps France. Brazil is eliminated.'
)

# STEP 4
add_heading_styled('Step 4: The Empire Effect — Owning Multiple Teams', 2)
doc.add_paragraph(
    'This is what makes the Takeover Pool completely different from any other pool:'
)

add_table_styled(
    ['Scenario', 'What Happens', 'Result'],
    [
        ['You own 3 teams in knockout', 'More chances to survive deeper into the bracket', 'Advantage — but you can also lose them'],
        ['Two of YOUR teams play each other', 'You\'re guaranteed to advance one team', 'No takeover possible (you own both)'],
        ['You own 0 teams', 'You\'re done — no team, no shot', 'Eliminated from the pool'],
        ['One player owns 6 teams', 'They dominate but if a key team gets captured, it hurts', 'High reward, high risk'],
        ['Final: both teams owned by same player', 'That player wins no matter what', 'Automatic champion'],
    ]
)

p = doc.add_paragraph()
p.add_run('CAN A PLAYER END UP WITH 5+ TEAMS?').bold = True
doc.add_paragraph(
    'Absolutely. Here\'s a realistic scenario:\n\n'
    '• You start with 2 teams (24-player pool)\n'
    '• Your underdog pulls an upset in group stage → you capture the favorite = 3 teams\n'
    '• Another upset in group stage → 4 teams\n'
    '• In the Round of 32, your team beats a favored opponent but doesn\'t cover → the other owner captures your team. You\'re back to 3.\n'
    '• Your other team pulls another spread upset in the Round of 16 → back to 4 teams\n\n'
    'The portfolio grows and shrinks throughout the tournament. It\'s like a stock portfolio — '
    'some days you\'re up, some days you\'re down. But all you need is to OWN the champion at the end.'
)

# ============================================================
# COMPLETE RULES
# ============================================================
add_heading_styled('COMPLETE RULES — DETAILED', 1)

rules = [
    ('1. Entry & Assignment', 
     'Entry fee: $50. Teams assigned via random draw before the tournament. '
     'Draw is conducted live (video call or in-person) or recorded and shared. '
     'Number of teams per player depends on total pool size (see table above).'),
    ('2. Spread Source',
     'Spreads for each match will be posted by the pool admin at least 24 hours before kickoff. '
     'Source: Vegas/sportsbook consensus lines when available, FIFA ranking-based formula for lesser matches. '
     'Spreads are in HALF-GOAL increments (e.g., -1.5, -2.5) to eliminate pushes.'),
    ('3. Group Stage Takeover',
     'If the favored team fails to cover the spread, the underdog\'s owner captures the favored team. '
     'Both teams remain in the tournament. The capturing owner now controls both teams. '
     'If the game is a draw, no takeover occurs regardless of spread.'),
    ('4. Knockout Stage Takeover',
     'Same spread mechanic applies. The losing team is eliminated from the tournament (and the pool). '
     'The winning team goes to whichever owner\'s side covered the spread. '
     'Penalties: If a match goes to penalties, the team that wins the shootout is considered the winner. '
     'Spread is applied to regulation/extra time score only.'),
    ('5. Multiple Team Ownership',
     'Players CAN and WILL own multiple teams. There is no cap on how many teams one player can own. '
     'If two teams owned by the same player face each other, no takeover occurs — the winning team stays with that owner.'),
    ('6. Elimination from the Pool',
     'A player is eliminated from the pool when they own ZERO teams. '
     'This happens when all their teams have been eliminated from the tournament OR captured by other players. '
     'There is no buy-back in the Takeover Pool.'),
    ('7. Winning',
     'The player who OWNS the World Cup Champion when the final whistle blows wins the entire pot. '
     'If the champion was captured mid-tournament, the capturing owner wins — not the original owner.'),
    ('8. Consolation Prize (Optional)',
     'Runner-up owner (owns the team that lost the Final) gets their entry fee refunded. '
     'This is optional and can be decided by the group before the tournament.'),
    ('9. Trading (Optional Advanced Rule)',
     'Players may trade teams with each other at any time BEFORE a match kicks off. '
     'All trades must be reported to the admin and posted publicly. '
     'This adds a negotiation/strategy layer. Enable or disable by group vote.'),
    ('10. Disputes',
     'Admin\'s ruling is final. Spreads are locked 1 hour before kickoff. No changes after lock.'),
]

for title_text, body_text in rules:
    p = doc.add_paragraph()
    p.add_run(title_text).bold = True
    doc.add_paragraph(body_text)

# ============================================================
# MONEY
# ============================================================
add_heading_styled('THE MONEY', 1)

add_table_styled(
    ['Scenario', 'Players', 'Entry Fee', 'Total Pot', 'Winner Gets'],
    [
        ['Small', '12', '$50', '$600', '$600'],
        ['Medium', '24', '$50', '$1,200', '$1,200'],
        ['Target', '48', '$50', '$2,400', '$2,400'],
        ['With Runner-Up Prize', '48', '$50', '$2,400', '$2,350 (runner-up gets $50 back)'],
    ]
)

# ============================================================
# COMBO WITH SURVIVOR
# ============================================================
add_heading_styled('COMBO — TWO POOLS, ONE WEBSITE', 1)

add_table_styled(
    ['Pool', 'Entry Fee', 'Type', 'Skill vs Luck', 'Engagement Level'],
    [
        ['Survivor Pool', '$100', 'Pick teams to win each round', 'High skill / strategy', 'Check in each round'],
        ['Takeover Pool', '$50', 'Teams assigned, ownership shifts', 'High luck / chaos', 'Every single game matters'],
        ['COMBO (Both)', '$140 (discount)', 'Best of both worlds', 'Mixed', 'Maximum entertainment'],
    ]
)

doc.add_paragraph(
    'Players can enter one or both pools. Same website, same login, separate leaderboards. '
    'The Takeover Pool keeps people engaged even when the Survivor Pool gets quiet between rounds.'
)

# ============================================================
# SURVIVOR vs TAKEOVER COMPARISON
# ============================================================
add_heading_styled('SURVIVOR POOL vs TAKEOVER POOL — SIDE BY SIDE', 1)

add_table_styled(
    ['Feature', 'Survivor Pool', 'Takeover Pool'],
    [
        ['How you get teams', 'YOU pick each round', 'Random draft — assigned'],
        ['Decisions to make', '7 picks across tournament', '0 after draft (unless trading)'],
        ['Can you be eliminated?', 'Yes — one wrong pick', 'Yes — when you own 0 teams'],
        ['Stay engaged?', 'Only when your round is active', 'EVERY game affects ownership'],
        ['Strategy', 'Which team to burn, when to save', 'Pray your underdogs upset favorites'],
        ['Multiple teams?', 'No — one pick per round', 'Yes — can own 5+ teams'],
        ['Buy-back?', 'Yes ($150 Lobster Trap)', 'No — once you\'re out, you\'re out'],
        ['Entry fee', '$100', '$50'],
        ['Best for', 'Strategic thinkers', 'Chaos lovers'],
        ['Payout', 'Last survivor(s) split pot', 'Owner of champion wins all'],
    ]
)

# ============================================================
# TIMELINE
# ============================================================
add_heading_styled('TIMELINE', 1)

add_table_styled(
    ['When', 'What'],
    [
        ['Now – April', 'Finalize rules, build the app'],
        ['May 1', 'Website goes live — enrollment opens'],
        ['May – June 10', 'Sign-ups for both pools'],
        ['June 8-10', 'Random draft for Takeover Pool (live or recorded)'],
        ['June 10', 'Survivor Pool group stage picks lock'],
        ['June 11', 'World Cup kicks off — both pools are LIVE'],
        ['~July 1', 'Group stage ends — Survivor eliminations + Takeover ownership snapshot'],
        ['~July 1-3', 'Lobster Trap buy-back window (Survivor only)'],
        ['~July 3', 'Knockout stage begins'],
        ['July 19', 'FINAL — Champions crowned in both pools'],
    ]
)

# ============================================================
# FOOTER
# ============================================================
doc.add_paragraph()
p = doc.add_paragraph('Built by Rob Lobster for Joe Lynch')
p.alignment = WD_ALIGN_PARAGRAPH.CENTER
p.runs[0].font.italic = True
p.runs[0].font.color.rgb = RGBColor(0x7F, 0x8C, 0x8D)

p = doc.add_paragraph('March 29, 2026')
p.alignment = WD_ALIGN_PARAGRAPH.CENTER
p.runs[0].font.italic = True
p.runs[0].font.color.rgb = RGBColor(0x7F, 0x8C, 0x8D)

# Save
output_path = '/Users/joemac/.openclaw/workspace/projects/lobster-takeover-pool-vision.docx'
doc.save(output_path)
print(f'Saved to {output_path}')
