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, Image, PageBreak

pdf_path = "/home/magdalena/eee/alunos/andre-veleiro-40pe/Memorial_Descritivo_e_Desenhos_Veleiro_40ft_Andre.pdf"
img_path = "/home/magdalena/eee/alunos/andre-veleiro-40pe/desenho_tecnico_veleiro_40ft_ketch.png"

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

styles = getSampleStyleSheet()

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

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

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

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

bullet_style = ParagraphStyle(
    'BulletText',
    parent=styles['Normal'],
    fontName='Helvetica',
    fontSize=8.5,
    leading=12.5,
    textColor=colors.HexColor('#1D2D44'),
    leftIndent=10,
    firstLineIndent=-6,
    spaceAfter=2
)

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

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

story = []

# Capa / Título
story.append(Paragraph("MEMORIAL DESCRITIVO & PLANTA DE ARRANJO GERAL", 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=8))

# 1. Missão
story.append(Paragraph("1. Missão da Embarcação e Filosofia de Projeto", h1_style))
story.append(Paragraph(
    "Embarcação projetada especificamente com foco em <b>moradia a bordo (liveaboard) para família de 4 pessoas</b>, "
    "conciliando habitabilidade, conforto térmico, máxima segurança em mar aberto costeiro (NORMAM), autonomia prolongada e facilidade de manobra com mastreação fracionada Ketch.",
    body_style
))
story.append(Spacer(1, 6))

# Tabela de Dimensões
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 + Popa aberta com solário e plataforma", table_cell_style)],
    [Paragraph("Aparelho / Mastreação", table_cell_style), Paragraph("Ketch (Vela Grande ~36 m², Mezena ~16 m², Genoa ~42 m²)", table_cell_style)],
    [Paragraph("Deslocamento Total Estimado", table_cell_style), Paragraph("9,5 t a 10,5 t (Lastro de ~3,5 t de chumbo na quilha)", table_cell_style)],
    [Paragraph("Material Construtivo", 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 - NORMAM)", table_cell_style)]
]
t_dim = Table(data_dim, colWidths=[180, 360])
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), 3),
    ('TOPPADDING', (0,0), (-1,-1), 3),
    ('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, 8))

# 3. Arranjo Geral e Acomodações
story.append(Paragraph("3. Acomodações Internas & Autonomia de Moradia", h1_style))
story.append(Paragraph("• <b>Suíte Master de Popa:</b> Cama de casal centralizada sob o convés de popa, roupeiros amplos com ventilação e banheiro privativo com box de banho.", bullet_style))
story.append(Paragraph("• <b>Suíte de Proa (Família):</b> Cama de casal em 'V' com gavetões e banheiro privativo completo a estibordo.", bullet_style))
story.append(Paragraph("• <b>Salão Social & Escritório:</b> Mesa de refeições em 'U' a bombordo, sofá e mesa de navegação estruturada para estação de home-office.", bullet_style))
story.append(Paragraph("• <b>Cozinha em Corredor (Galley):</b> A bombordo sob a lateral do cockpit, com pia dupla, fogão com forno cardânico e refrigerador/freezer de grande capacidade.", bullet_style))
story.append(Paragraph("• <b>Autonomia Técnica:</b> Água Doce (400-500 L + dessalinizador), Diesel (250-350 L), Banco Solar LiFePO4 (400-600 Ah com 600-900W de painéis).", bullet_style))

# Página 2: Desenho Técnico Completo
story.append(PageBreak())
story.append(Paragraph("4. Desenho Técnico Preliminar: Perfil Vélico e Arranjo Geral", h1_style))
story.append(Paragraph("Desenho esquemático gerado a partir das especificações de projeto e esboços conceituais do aluno:", body_style))
story.append(Spacer(1, 10))

# Inserir a imagem gerada
img = Image(img_path, width=540, height=370)
story.append(img)

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