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

pdf_path = "/home/magdalena/eee/alunos/ricardo-teixeira/Tabela_Estudo_de_Similares_Preenchida_Ricardo_Teixeira.pdf"

doc = SimpleDocTemplate(
    pdf_path,
    pagesize=A4,
    leftMargin=24,
    rightMargin=24,
    topMargin=24,
    bottomMargin=24
)

styles = getSampleStyleSheet()

# Cores oficiais Brana
azul_escuro = colors.HexColor("#1A365D")
azul_medio = colors.HexColor("#2B6CB0")
cinza_linhas = colors.HexColor("#CBD5E0")
cinza_claro = colors.HexColor("#F7FAFC")
cinza_destaque = colors.HexColor("#EDF2F7")
texto_escuro = colors.HexColor("#1A202C")
laranja_brana = colors.HexColor("#DD6B20")

# Estilos de texto
style_title = ParagraphStyle(
    'DocTitle',
    parent=styles['Normal'],
    fontName='Helvetica-Bold',
    fontSize=13,
    leading=16,
    textColor=azul_escuro,
    alignment=0
)

style_subtitle = ParagraphStyle(
    'DocSubtitle',
    parent=styles['Normal'],
    fontName='Helvetica-Bold',
    fontSize=8,
    leading=10.5,
    textColor=azul_medio,
    alignment=0
)

style_notes = ParagraphStyle(
    'DocNotes',
    parent=styles['Normal'],
    fontName='Helvetica',
    fontSize=6.8,
    leading=8.8,
    textColor=colors.HexColor("#4A5568"),
    alignment=0
)

style_th = ParagraphStyle(
    'TableHead',
    parent=styles['Normal'],
    fontName='Helvetica-Bold',
    fontSize=7,
    leading=9,
    textColor=colors.white,
    alignment=1
)

style_th_param = ParagraphStyle(
    'TableHeadParam',
    parent=styles['Normal'],
    fontName='Helvetica-Bold',
    fontSize=7,
    leading=9,
    textColor=colors.white,
    alignment=0
)

style_cell_param = ParagraphStyle(
    'CellParam',
    parent=styles['Normal'],
    fontName='Helvetica-Bold',
    fontSize=6.8,
    leading=8.2,
    textColor=texto_escuro,
    alignment=0
)

style_cell_param_sub = ParagraphStyle(
    'CellParamSub',
    parent=styles['Normal'],
    fontName='Helvetica',
    fontSize=6.5,
    leading=7.8,
    textColor=texto_escuro,
    alignment=0
)

style_cell_unit = ParagraphStyle(
    'CellUnit',
    parent=styles['Normal'],
    fontName='Helvetica',
    fontSize=6.5,
    leading=8,
    textColor=colors.HexColor("#4A5568"),
    alignment=1
)

style_cell_val = ParagraphStyle(
    'CellVal',
    parent=styles['Normal'],
    fontName='Helvetica',
    fontSize=6.5,
    leading=8,
    textColor=texto_escuro,
    alignment=1
)

style_cell_val_name = ParagraphStyle(
    'CellValName',
    parent=styles['Normal'],
    fontName='Helvetica-Bold',
    fontSize=6.5,
    leading=8,
    textColor=azul_escuro,
    alignment=1
)

style_cell_razao = ParagraphStyle(
    'CellRazao',
    parent=styles['Normal'],
    fontName='Helvetica-Bold',
    fontSize=6.8,
    leading=8.2,
    textColor=azul_escuro,
    alignment=0
)

style_footer = ParagraphStyle(
    'FooterText',
    parent=styles['Normal'],
    fontName='Helvetica',
    fontSize=6.2,
    leading=7.8,
    textColor=colors.HexColor("#718096"),
    alignment=1
)

elements = []

# Cabeçalho com logo/texto Brana
header_table_data = [
    [
        Paragraph("<b>ESTUDO DE SIMILARES</b>", style_title),
        Paragraph("<font color='#DD6B20'><b>brana</b></font><br/><font size=5.5 color='#718096'>PROJETOS NAVAIS</font>", ParagraphStyle('Logo', fontName='Helvetica-Bold', fontSize=11, leading=12, alignment=2))
    ],
    [
        Paragraph("Tabela comparativa de embarcações &nbsp;|&nbsp; Projetista: <b>Ricardo Teixeira</b> &nbsp;|&nbsp; Unidades em SI (métrico)", style_subtitle),
        Paragraph("", style_subtitle)
    ],
    [
        Paragraph("<b>DLR</b> = Δ [t.long.] / (0,01 × LWL [pés])³ &nbsp;&nbsp;&nbsp;•&nbsp;&nbsp;&nbsp; <i>Rigor de fidelidade Brana: células com traço (—) = dados não publicados na fonte oficial primária.</i>", style_notes),
        Paragraph("", style_notes)
    ]
]

t_head = Table(header_table_data, colWidths=[430, 117])
t_head.setStyle(TableStyle([
    ('VALIGN', (0,0), (-1,-1), 'TOP'),
    ('BOTTOMPADDING', (0,0), (-1,-1), 1),
    ('TOPPADDING', (0,0), (-1,-1), 1),
    ('LEFTPADDING', (0,0), (-1,-1), 0),
    ('RIGHTPADDING', (0,0), (-1,-1), 0),
]))
elements.append(t_head)
elements.append(Spacer(1, 3))

# Largura total = 547 pt
col_w = [163, 28, 59, 59, 59, 59, 59, 59]

def cell(text, bold=False):
    st = style_cell_val_name if bold else style_cell_val
    return Paragraph(str(text), st)

rows = [
    [
        Paragraph("<b>PARÂMETRO</b>", style_th_param),
        Paragraph("<b>UNID.</b>", style_th),
        Paragraph("<b>1</b>", style_th),
        Paragraph("<b>2</b>", style_th),
        Paragraph("<b>3</b>", style_th),
        Paragraph("<b>4</b>", style_th),
        Paragraph("<b>5</b>", style_th),
        Paragraph("<b>6</b>", style_th),
    ],
    # Linha 1: Embarcações
    [
        Paragraph("Embarcação (estaleiro / modelo)", style_cell_param),
        Paragraph("", style_cell_unit),
        cell("Sombra da Lua<br/>(Simão 1923)", True),
        cell("Vendaval II<br/>(1947)", True),
        cell("Joia Rara<br/>(Aristides)", True),
        cell("É da Vida<br/>(c. 1920)", True),
        cell("Bote Cacilheiro<br/>(Tejo - Port.)", True),
        cell("Saveiro Aveiro<br/>(Ovar - Port.)", True),
    ],
    # Linha 2: LOA
    [Paragraph("Comprimento total (LOA)", style_cell_param), Paragraph("m", style_cell_unit), cell("12,50"), cell("14,00"), cell("7,00"), cell("12,20"), cell("9,50"), cell("8,50")],
    # Linha 3: LWL
    [Paragraph("Comprimento na linha d'água (LWL)", style_cell_param), Paragraph("m", style_cell_unit), cell("—"), cell("—"), cell("—"), cell("—"), cell("—"), cell("—")],
    # Linha 4: BOA
    [Paragraph("Boca máxima (BOA)", style_cell_param), Paragraph("m", style_cell_unit), cell("4,00"), cell("4,60"), cell("—"), cell("3,90"), cell("2,80"), cell("2,40")],
    # Linha 5: Calado
    [Paragraph("Calado", style_cell_param), Paragraph("m", style_cell_unit), cell("0,60"), cell("—"), cell("—"), cell("0,60"), cell("0,50"), cell("—")],
    # Linha 6: Deslocamento
    [Paragraph("Deslocamento", style_cell_param), Paragraph("kg", style_cell_unit), cell("—"), cell("—"), cell("—"), cell("—"), cell("—"), cell("—")],
    # Linha 7: Deadrise
    [Paragraph("Ângulo de V do fundo (deadrise)", style_cell_param), Paragraph("graus", style_cell_unit), cell("—"), cell("—"), cell("—"), cell("—"), cell("—"), cell("—")],
    # Linha 8: Potência instalada
    [Paragraph("Potência instalada", style_cell_param), Paragraph("HP", style_cell_unit), cell("—"), cell("—"), cell("—"), cell("—"), cell("—"), cell("—")],
    # Linha 9: Motorização
    [Paragraph("Motorização (marca / modelo / qtd.)", style_cell_param), Paragraph("—", style_cell_unit), cell("Diesel aux."), cell("Motor aux."), cell("Vela de içar"), cell("Diesel aux."), cell("Remos aux."), cell("Varas / remo")],
    # Linha 10: Área vélica
    [Paragraph("Área vélica (se veleiro)", style_cell_param), Paragraph("m²", style_cell_unit), cell("—"), cell("—"), cell("—"), cell("—"), cell("—"), cell("—")],
    # Linha 11: Combustível
    [Paragraph("Combustível", style_cell_param), Paragraph("L", style_cell_unit), cell("—"), cell("—"), cell("—"), cell("—"), cell("—"), cell("—")],
    # Linha 12: Água doce
    [Paragraph("Água doce", style_cell_param), Paragraph("L", style_cell_unit), cell("—"), cell("—"), cell("—"), cell("—"), cell("—"), cell("—")],
    # Linha 13: Vel. máxima
    [Paragraph("Velocidade máxima", style_cell_param), Paragraph("nós", style_cell_unit), cell("—"), cell("—"), cell("—"), cell("—"), cell("—"), cell("—")],
    # Linha 14: Vel. cruzeiro
    [Paragraph("Velocidade de cruzeiro", style_cell_param), Paragraph("nós", style_cell_unit), cell("—"), cell("—"), cell("—"), cell("—"), cell("—"), cell("—")],
    # Linha 15: Autonomia
    [Paragraph("Autonomia", style_cell_param), Paragraph("MN", style_cell_unit), cell("—"), cell("—"), cell("—"), cell("—"), cell("—"), cell("—")],
    # Linha 16: Seção RAZÕES
    [Paragraph("<b>RAZÕES — calcular</b>", style_cell_razao), Paragraph("", style_cell_unit), "", "", "", "", "", ""],
    # Linha 17: LOA / BOA
    [Paragraph("LOA / BOA", style_cell_param_sub), Paragraph("—", style_cell_unit), cell("3,12"), cell("3,04"), cell("—"), cell("3,13"), cell("3,39"), cell("3,54")],
    # Linha 18: DLR
    [Paragraph("Razão desloc.-comprimento (DLR)", style_cell_param_sub), Paragraph("—", style_cell_unit), cell("—"), cell("—"), cell("—"), cell("—"), cell("—"), cell("—")],
    # Linha 19: Deslocamento / Potência
    [Paragraph("Deslocamento / Potência", style_cell_param_sub), Paragraph("kg/HP", style_cell_unit), cell("—"), cell("—"), cell("—"), cell("—"), cell("—"), cell("—")],
]

table_style_list = [
    ('BACKGROUND', (0,0), (-1,0), azul_escuro),
    ('VALIGN', (0,0), (-1,-1), 'MIDDLE'),
    ('GRID', (0,0), (-1,-1), 0.5, cinza_linhas),
    ('TOPPADDING', (0,0), (-1,-1), 4.8),
    ('BOTTOMPADDING', (0,0), (-1,-1), 4.8),
    ('LEFTPADDING', (0,0), (0,-1), 4),
    ('RIGHTPADDING', (0,0), (-1,-1), 1),
    ('LEFTPADDING', (1,0), (-1,-1), 1),
    
    # Linha 1: Embarcação
    ('TOPPADDING', (0,1), (-1,1), 5),
    ('BOTTOMPADDING', (0,1), (-1,1), 5),
    ('BACKGROUND', (0,1), (-1,1), colors.HexColor("#F0F4F8")),
    
    # Linha 16: Seção RAZÕES
    ('BACKGROUND', (0,16), (-1,16), cinza_destaque),
    ('SPAN', (0,16), (1,16)),
    
    # Linha 17: LOA / BOA
    ('BACKGROUND', (2,17), (-1,17), colors.HexColor("#EBF8FF")),
    
    # Alternância de linhas
    ('BACKGROUND', (0,3), (-1,3), cinza_claro),
    ('BACKGROUND', (0,5), (-1,5), cinza_claro),
    ('BACKGROUND', (0,7), (-1,7), cinza_claro),
    ('BACKGROUND', (0,9), (-1,9), cinza_claro),
    ('BACKGROUND', (0,11), (-1,11), cinza_claro),
    ('BACKGROUND', (0,13), (-1,13), cinza_claro),
    ('BACKGROUND', (0,15), (-1,15), cinza_claro),
]

t_main = Table(rows, colWidths=col_w)
t_main.setStyle(TableStyle(table_style_list))
elements.append(t_main)

elements.append(Spacer(1, 5))

footer_text = "O conteúdo deste material é de uso exclusivo dos alunos matriculados nos cursos do Brana. Dados estritamente documentados por fontes primárias (IPHAN, Associação Viva Saveiro, UFRB, Revista da Armada e ANGE). Lacunas registradas com traço (—)."
elements.append(Paragraph(footer_text, style_footer))

doc.build(elements)
print("PDF com rigor estrito gerado com sucesso!")
