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

pdf_path = "/home/magdalena/eee/alunos/alberico-de-carvalho-santiago/Projeto_Catamara_Pesca_Alberico.pdf"

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

styles = getSampleStyleSheet()

primary_color = colors.HexColor("#0D3B66")   # Deep Ocean Blue
secondary_color = colors.HexColor("#0081A7") # Teal Marine
accent_color = colors.HexColor("#F4A261")    # Warm Ochre
dark_neutral = colors.HexColor("#264653")    # Deep Teal Charcoal
light_neutral = colors.HexColor("#F8F9FA")   # Soft Off-white
border_color = colors.HexColor("#CBD5E1")

title_style = ParagraphStyle(
    'DocTitle',
    parent=styles['Heading1'],
    fontName='Helvetica-Bold',
    fontSize=18,
    leading=22,
    textColor=primary_color,
    alignment=1,
    spaceAfter=4
)

subtitle_style = ParagraphStyle(
    'DocSubtitle',
    parent=styles['Normal'],
    fontName='Helvetica',
    fontSize=10,
    leading=13,
    textColor=secondary_color,
    alignment=1,
    spaceAfter=12
)

section_heading = ParagraphStyle(
    'SectionHeading',
    parent=styles['Heading2'],
    fontName='Helvetica-Bold',
    fontSize=11.5,
    leading=14,
    textColor=primary_color,
    spaceBefore=8,
    spaceAfter=4
)

body_style = ParagraphStyle(
    'BodyDark',
    parent=styles['Normal'],
    fontName='Helvetica',
    fontSize=8.5,
    leading=11.5,
    textColor=dark_neutral,
    spaceAfter=4
)

bullet_style = ParagraphStyle(
    'BulletDark',
    parent=body_style,
    leftIndent=12,
    firstLineIndent=-8,
    spaceAfter=3
)

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

table_body_style = ParagraphStyle(
    'TableBody',
    parent=styles['Normal'],
    fontName='Helvetica',
    fontSize=8,
    leading=10,
    textColor=dark_neutral
)

table_body_bold = ParagraphStyle(
    'TableBodyBold',
    parent=styles['Normal'],
    fontName='Helvetica-Bold',
    fontSize=8,
    leading=10,
    textColor=primary_color
)

story = []

# Title & Subtitle
story.append(Paragraph("MEMORIAL DESCRITIVO PRELIMINAR DE PROJETO", title_style))
story.append(Paragraph("<b>Catamarã Pesqueiro a Motor (PRFV) — Espinhel de Fundo (400 m)</b><br/><b>Aluno:</b> Alberico de Carvalho Santiago | <b>Instituição:</b> Brana / Estaleiro Escola Embrião (EEE)", subtitle_style))
story.append(HRFlowable(width="100%", thickness=1.5, color=secondary_color, spaceAfter=8))

# 1. Missão e Requisitos
story.append(Paragraph("1. Requisitos de Missão e Operação", section_heading))
story.append(Paragraph(
    "Projeto conceitual de embarcação tipo <b>Catamarã a Motor</b> em fibra de vidro (PRFV), concebido para pesca profissional com <b>espinhel de fundo</b> na borda da plataforma / talude continental (~400 metros de profundidade), operando a partir de <b>Salvador - BA</b> com autonomia para até <b>5 dias de mar</b> e tripulação de <b>4 pescadores</b>.",
    body_style
))

req_data = [
    [Paragraph("Parâmetro", table_header_style), Paragraph("Especificação", table_header_style), Paragraph("Justificativa de Engenharia", table_header_style)],
    [Paragraph("Tipo de Casco", table_body_bold), Paragraph("Catamarã (Multicasco)", table_body_style), Paragraph("Altíssima estabilidade inicial transversal, segurança e grande área plana de convés.", table_body_style)],
    [Paragraph("Arte de Pesca", table_body_bold), Paragraph("Espinhel de fundo (400 m)", table_body_style), Paragraph("Operação de linha pesada exigindo convés livre e base para guincho recolhedor.", table_body_style)],
    [Paragraph("Autonomia / Tripulação", table_body_bold), Paragraph("3 a 5 dias / 4 pessoas", table_body_style), Paragraph("Dimensionamento de 4 beliches, cozinha, sanitário, água doce e mantimentos.", table_body_style)],
    [Paragraph("Motorização Alvo", table_body_bold), Paragraph("~50 HP (ex: 2x 25 HP / 2x 30 HP)", table_body_style), Paragraph("Cascos finos garantem deslocamento altamente eficiente com baixíssimo consumo.", table_body_style)],
    [Paragraph("Material Construtivo", table_body_bold), Paragraph("Fibra de Vidro (PRFV)", table_body_style), Paragraph("Leveza, resistência estrutural, durabilidade e fácil assepsia dos porões.", table_body_style)],
    [Paragraph("Capacidade de Porão", table_body_bold), Paragraph("1,0 ton peixe + 1,2 ton gelo", table_body_style), Paragraph("Porões térmicos isolados com poliuretano (PUR) de alta densidade.", table_body_style)]
]

t_req = Table(req_data, colWidths=[1.5*inch, 2.2*inch, 3.6*inch])
t_req.setStyle(TableStyle([
    ('BACKGROUND', (0,0), (-1,0), primary_color),
    ('ALIGN', (0,0), (-1,-1), 'LEFT'),
    ('VALIGN', (0,0), (-1,-1), 'MIDDLE'),
    ('GRID', (0,0), (-1,-1), 0.5, border_color),
    ('ROWBACKGROUNDS', (0,1), (-1,-1), [light_neutral, colors.white]),
    ('TOPPADDING', (0,0), (-1,-1), 3),
    ('BOTTOMPADDING', (0,0), (-1,-1), 3),
]))
story.append(t_req)
story.append(Spacer(1, 6))

# 2. Dimensões Principais
story.append(Paragraph("2. Dimensões Principais Preliminares", section_heading))

dim_data = [
    [Paragraph("Dimensão", table_header_style), Paragraph("Símbolo", table_header_style), Paragraph("Valor", table_header_style), Paragraph("Descrição / Comportamento", table_header_style)],
    [Paragraph("Comprimento Total", table_body_bold), Paragraph("LOA", table_body_style), Paragraph("10,20 m (33,5 ft)", table_body_style), Paragraph("Comprimento ótimo para mar aberto e eficiência com 50 HP.", table_body_style)],
    [Paragraph("Comprimento na Linha d'Água", table_body_bold), Paragraph("LWL", table_body_style), Paragraph("9,60 m", table_body_style), Paragraph("Velocidade de cruzeiro de deslocamento entre 7,0 e 8,0 nós.", table_body_style)],
    [Paragraph("Boca Total (Largura)", table_body_bold), Paragraph("B", table_body_style), Paragraph("4,00 m", table_body_style), Paragraph("Proporção B/LOA ~ 0,39, garantindo momento restaurador elevado.", table_body_style)],
    [Paragraph("Boca de Cada Semi-Casco", table_body_bold), Paragraph("b", table_body_style), Paragraph("0,95 m", table_body_style), Paragraph("Relação L/b ~ 10,1 (casco fino, baixa geração de ondas).", table_body_style)],
    [Paragraph("Pontal Moldado", table_body_bold), Paragraph("D", table_body_style), Paragraph("1,60 m", table_body_style), Paragraph("Volume interno nos cascos e altura segura da borda livre.", table_body_style)],
    [Paragraph("Calado de Projeto", table_body_bold), Paragraph("T", table_body_style), Paragraph("0,70 m", table_body_style), Paragraph("Baixo calado, facilidade de atracação e navegação costeira segura.", table_body_style)],
    [Paragraph("Vão Livre do Túnel", table_body_bold), Paragraph("Hc", table_body_style), Paragraph("0,65 m", table_body_style), Paragraph("Espaço livre entre água e ponte central para evitar batida de onda (slamming).", table_body_style)],
    [Paragraph("Deslocamento Carregado", table_body_bold), Paragraph("Δ", table_body_style), Paragraph("~5,2 toneladas", table_body_style), Paragraph("Peso leve estrutural + 4 tripulantes + carga total + tanques cheios.", table_body_style)]
]

t_dim = Table(dim_data, colWidths=[1.7*inch, 0.7*inch, 1.4*inch, 3.5*inch])
t_dim.setStyle(TableStyle([
    ('BACKGROUND', (0,0), (-1,0), primary_color),
    ('ALIGN', (0,0), (-1,-1), 'LEFT'),
    ('VALIGN', (0,0), (-1,-1), 'MIDDLE'),
    ('GRID', (0,0), (-1,-1), 0.5, border_color),
    ('ROWBACKGROUNDS', (0,1), (-1,-1), [light_neutral, colors.white]),
    ('TOPPADDING', (0,0), (-1,-1), 3),
    ('BOTTOMPADDING', (0,0), (-1,-1), 3),
]))
story.append(t_dim)
story.append(Spacer(1, 6))

# 3. Arranjo Geral e Acomodações
story.append(Paragraph("3. Arranjo Geral, Conforto e Praça de Pesca", section_heading))
story.append(Paragraph("• <b>Acomodações para 4 Tripulantes:</b> Distribuição de 2 beliches em cada casco ou cabine integrada à vante da ponte central, garantindo descanso digno e ventilação adequada.", bullet_style))
story.append(Paragraph("• <b>Cozinha e Sanitário:</b> Módulo compacto na casaria central com fogão a gás, pia de água doce e sanitário marítimo fechado com tanque de águas negras conforme NORMAM.", bullet_style))
story.append(Paragraph("• <b>Praça de Trabalho (Convés de Ré):</b> Amplo convés de popa (>16 m²) totalmente desimpedido, com piso antiderrapante, roletes de borda para linha-mãe de espinhel e mesa de manuseio.", bullet_style))
story.append(Paragraph("• <b>Porões de Pescado Isolados:</b> Instalados próximos ao centro de gravidade (LCG) nos cascos, isolados com PUR 80 mm e revestimento higiênico em gelcoat.", bullet_style))
story.append(Spacer(1, 4))

# 4. Propulsão, Consumo e Tanques
story.append(Paragraph("4. Propulsão, Autonomia e Balanço de Consumo", section_heading))
story.append(Paragraph("• <b>Configuração Recomendada:</b> 2x Motores de 25 HP a 30 HP (Diesel ou Popa 4 Tempos) instalados um em cada popa. Permite redundância de segurança e manobrabilidade excepcional por rotação diferencial.", bullet_style))
story.append(Paragraph("• <b>Consumo de Combustível:</b> Regime de cruzeiro (~7,5 nós) com consumo total combinado de ~4,5 a 5,5 L/h.", bullet_style))
story.append(Paragraph("• <b>Tanque de Combustível:</b> <b>400 Litros</b> (fornece ~80 horas de operação, cobrindo com folga os 5 dias de faina + 30% de reserva).", bullet_style))
story.append(Paragraph("• <b>Tanque de Água Doce:</b> <b>350 Litros</b> (~17,5 L/dia/pessoa, suficiente para consumo, preparo de alimentos e higiene).", bullet_style))

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