import os
from reportlab.lib.pagesizes import letter
from reportlab.lib import colors
from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle
from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer, Table, TableStyle, HRFlowable, KeepTogether

pdf_path = "/home/magdalena/eee/alunos/andre-veleiro-40pe/Memorial_Descritivo_Veleiro_40ft_Moradia_Andre.pdf"

doc = SimpleDocTemplate(
    pdf_path,
    pagesize=letter,
    rightMargin=40, leftMargin=40, topMargin=40, bottomMargin=40
)

styles = getSampleStyleSheet()

title_style = ParagraphStyle(
    'DocTitle',
    parent=styles['Heading1'],
    fontName='Helvetica-Bold',
    fontSize=20,
    leading=24,
    textColor=colors.HexColor('#0B2545'),
    spaceAfter=4,
    alignment=1
)

subtitle_style = ParagraphStyle(
    'DocSubtitle',
    parent=styles['Normal'],
    fontName='Helvetica',
    fontSize=11,
    leading=15,
    textColor=colors.HexColor('#134074'),
    spaceAfter=15,
    alignment=1
)

h1_style = ParagraphStyle(
    'SectionHeading',
    parent=styles['Heading2'],
    fontName='Helvetica-Bold',
    fontSize=13,
    leading=17,
    textColor=colors.HexColor('#0B2545'),
    spaceBefore=12,
    spaceAfter=6
)

body_style = ParagraphStyle(
    'BodyDark',
    parent=styles['Normal'],
    fontName='Helvetica',
    fontSize=9.5,
    leading=14,
    textColor=colors.HexColor('#1D2D44')
)

bullet_style = ParagraphStyle(
    'BulletText',
    parent=styles['Normal'],
    fontName='Helvetica',
    fontSize=9,
    leading=13.5,
    textColor=colors.HexColor('#1D2D44'),
    leftIndent=12,
    firstLineIndent=-8,
    spaceAfter=3
)

table_header_style = ParagraphStyle(
    'TableHeader',
    parent=styles['Normal'],
    fontName='Helvetica-Bold',
    fontSize=9,
    leading=11,
    textColor=colors.white
)

table_cell_style = ParagraphStyle(
    'TableCell',
    parent=styles['Normal'],
    fontName='Helvetica',
    fontSize=8.5,
    leading=11,
    textColor=colors.HexColor('#1D2D44')
)

story = []

# Title & Subtitle
story.append(Paragraph("MEMORIAL DESCRITIVO PRELIMINAR", title_style))
story.append(Paragraph("Veleiro de Cruzeiro & Moradia 40 pés (Ketch / Cockpit Central)<br/><b>Escola Brana & EEE | Aluno: Andre | Orientação: Profa. Magdalena</b>", subtitle_style))
story.append(HRFlowable(width="100%", thickness=1.5, color=colors.HexColor('#134074'), spaceBefore=0, spaceAfter=10))

# 1. Missão e Requisitos Gerais
story.append(Paragraph("1. Missão da Embarcação e Filosofia de Projeto", h1_style))
story.append(Paragraph(
    "A embarcação é concebida com a <b>missão primordial de moradia a bordo (liveaboard)</b> para uma família de 4 pessoas, "
    "aliando navegabilidade em mar aberto costeiro (NORMAM) a uma vivência autossuficiente, segura e confortável por longos períodos.",
    body_style
))
story.append(Spacer(1, 8))

# Tabela de Dimensões Principais
story.append(Paragraph("2. Características Principais e Geometria", h1_style))

data_dim = [
    [Paragraph("<b>Parâmetro</b>", table_header_style), Paragraph("<b>Valor Estimado / Especificação</b>", table_header_style)],
    [Paragraph("Comprimento Total (LOA)", table_cell_style), Paragraph("12,20 m (40,0 pés)", table_cell_style)],
    [Paragraph("Comprimento Linha d'Água (LWL)", table_cell_style), Paragraph("~10,90 m", table_cell_style)],
    [Paragraph("Boca Máxima (Bmax)", table_cell_style), Paragraph("3,85 m - 3,95 m", table_cell_style)],
    [Paragraph("Calado de Projeto (T)", table_cell_style), Paragraph("1,65 m - 1,75 m", table_cell_style)],
    [Paragraph("Tipo de Quilha / Leme", table_cell_style), Paragraph("Quilha Semi-Longa + Leme apoiado em Skeg estrutural", table_cell_style)],
    [Paragraph("Configuração de Convés", table_cell_style), Paragraph("Cockpit Central (Center Cockpit) + Popa aberta com solário", table_cell_style)],
    [Paragraph("Aparelho / Mastreação", table_cell_style), Paragraph("Ketch (Mastro Grande + Mastro de Mezena)", table_cell_style)],
    [Paragraph("Deslocamento Estimado", table_cell_style), Paragraph("9,5 t a 10,5 t (com lastro de ~3,5 t de chumbo/ferro)", table_cell_style)],
    [Paragraph("Material de Construção", table_cell_style), Paragraph("PRFV (Casco monolítico + Convés em Sandwich de Divinycell)", table_cell_style)],
    [Paragraph("Área de Navegação", table_cell_style), Paragraph("Mar Aberto - Costeira (até 20 milhas da costa - NORMAM)", table_cell_style)]
]

t_dim = Table(data_dim, colWidths=[200, 330])
t_dim.setStyle(TableStyle([
    ('BACKGROUND', (0,0), (-1,0), colors.HexColor('#0B2545')),
    ('ALIGN', (0,0), (-1,-1), 'LEFT'),
    ('VALIGN', (0,0), (-1,-1), 'MIDDLE'),
    ('BOTTOMPADDING', (0,0), (-1,-1), 4),
    ('TOPPADDING', (0,0), (-1,-1), 4),
    ('ROWBACKGROUNDS', (0,1), (-1,-1), [colors.HexColor('#EEF4F8'), colors.white]),
    ('GRID', (0,0), (-1,-1), 0.5, colors.HexColor('#8DA9C4'))
]))
story.append(t_dim)
story.append(Spacer(1, 10))

# 3. Arranjo Geral e Acomodações
story.append(Paragraph("3. Arranjo Interno e Distribuição de Espaço (2 Suítes)", h1_style))
story.append(Paragraph("• <b>Suíte Master de Popa:</b> Sob o convés de popa, cama de casal central/semi-ilha, roupeiros ventilados, mesa de apoio e banheiro privativo amplo com box de banho.", bullet_style))
story.append(Paragraph("• <b>Suíte de Proa (Família/Hóspedes):</b> Cama de casal em 'V' (ou 2 beliches largos), armários aéreos e banheiro privativo completo.", bullet_style))
story.append(Paragraph("• <b>Salão Social & Escritório:</b> Mesa de jantar em 'U' (acomoda 6 pessoas e converte em cama dupla extra), sofá de leitura e mesa de navegação estruturada para estação de trabalho (home-office).", bullet_style))
story.append(Paragraph("• <b>Cozinha em Corredor (Galley):</b> Posicionada a bombordo sob a lateral do cockpit. Máxima segurança no mar, pia dupla em inox, fogão 3 bocas com forno cardânico e refrigeração de alta capacidade (geladeira + freezer).", bullet_style))
story.append(Paragraph("• <b>Praça de Máquinas Central:</b> Localizada diretamente abaixo do cockpit central. Excelente acesso 360° para manutenção e isolamento acústico reforçado.", bullet_style))
story.append(Spacer(1, 8))

# 4. Autonomia e Sistemas de Bordo
story.append(Paragraph("4. Autonomia e Sistemas para Moradia Contínua", h1_style))
story.append(Paragraph("• <b>Água Doce:</b> Tanques estruturais integrados com capacidade de 400 a 500 L + linha para Dessalinizador (30 a 60 L/h).", bullet_style))
story.append(Paragraph("• <b>Combustível (Diesel):</b> 250 a 350 L para motor auxiliar de 45-55 HP, garantindo autonomia motora estendida.", bullet_style))
story.append(Paragraph("• <b>Energia Elétrica & Solar:</b> Banco LiFePO4 de 400 a 600 Ah com arranjo de painéis solares de 600 W a 900 W instalados na targa de popa e mastro de mezena.", bullet_style))
story.append(Paragraph("• <b>Saneamento:</b> Tanque de retenção de águas negras (Holding Tank) de 80 a 120 L com válvula de 3 vias e bomba maceradora.", bullet_style))
story.append(Paragraph("• <b>Conforto Térmico:</b> Excelente ventilação cruzada através de gaiutas de convés e vigias laterais com isolamento em espuma de PVC (Divinycell).", bullet_style))

doc.build(story)
print("PDF gerado com sucesso!")
