import json
from reportlab.lib import colors
from reportlab.lib.pagesizes import A4
from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer, Table, TableStyle, HRFlowable
from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle
from reportlab.lib.units import cm

def gerar_pdf_memoria_pesos_grupo2(pdf_path):
    doc = SimpleDocTemplate(
        pdf_path,
        pagesize=A4,
        leftMargin=1.5*cm,
        rightMargin=1.5*cm,
        topMargin=1.5*cm,
        bottomMargin=1.5*cm
    )
    styles = getSampleStyleSheet()
    azul_brana = colors.HexColor('#002B49')
    cinza_fundo = colors.HexColor('#F4F6F8')
    cinza_borda = colors.HexColor('#D1D5DB')
    texto_escuro = colors.HexColor('#1F2937')
    
    title_style = ParagraphStyle('DocTitle', parent=styles['Heading1'], fontName='Helvetica-Bold', fontSize=16, leading=20, textColor=azul_brana, spaceAfter=2)
    sub_style = ParagraphStyle('DocSub', parent=styles['Normal'], fontName='Helvetica', fontSize=8.5, leading=12, textColor=colors.HexColor('#4B5563'))
    h2_style = ParagraphStyle('DocH2', parent=styles['Heading2'], fontName='Helvetica-Bold', fontSize=11, leading=14, textColor=azul_brana, spaceBefore=8, spaceAfter=4)
    body_style = ParagraphStyle('DocBody', parent=styles['Normal'], fontName='Helvetica', fontSize=8, leading=11, textColor=texto_escuro)
    table_hdr = ParagraphStyle('TableHdr', parent=styles['Normal'], fontName='Helvetica-Bold', fontSize=7.5, leading=9, textColor=colors.white, alignment=1)
    table_cell = ParagraphStyle('TableCell', parent=styles['Normal'], fontName='Helvetica', fontSize=7.5, leading=9.5, textColor=texto_escuro)
    table_cell_center = ParagraphStyle('TableCellC', parent=styles['Normal'], fontName='Helvetica', fontSize=7.5, leading=9.5, textColor=texto_escuro, alignment=1)
    table_cell_bold = ParagraphStyle('TableCellB', parent=styles['Normal'], fontName='Helvetica-Bold', fontSize=7.5, leading=9.5, textColor=azul_brana, alignment=1)

    story = []
    
    story.append(Paragraph("MEMÓRIA DE CÁLCULO: PESOS, ÁREAS E CENTROS DE GRAVIDADE", title_style))
    story.append(Paragraph("<b>Embarcação:</b> Saveiro Tesouro da Baía (12,00 m) | <b>Grupo 2:</b> Carlos Silva, Cauã e Alessandra Feitosa", sub_style))
    story.append(Paragraph("<b>Metodologia:</b> ABS Guide for Offshore Yachts + Regra Sn de Dave Gerr + Tabela de Cotas V4", sub_style))
    story.append(Spacer(1, 0.2*cm))
    story.append(HRFlowable(width="100%", thickness=1.5, color=azul_brana, spaceBefore=2, spaceAfter=6))
    
    # Seção 1
    story.append(Paragraph("1. Áreas de Superfície e Escantilhão do Chapeamento", h2_style))
    story.append(Paragraph("A geometria do casco foi integrada a partir da Tabela de Cotas V4 (11 balizas). As espessuras foram dimensionadas pela carga hidrostática e impacto do ABS:", body_style))
    story.append(Spacer(1, 0.2*cm))
    
    chapa_data = [
        [Paragraph("<b>Região do Casco</b>", table_hdr), Paragraph("<b>Área Real (m²)</b>", table_hdr), Paragraph("<b>Espessura Adotada</b>", table_hdr), Paragraph("<b>Densidade (kg/m³)</b>", table_hdr), Paragraph("<b>Massa (kg)</b>", table_hdr), Paragraph("<b>LCG da Popa (m)</b>", table_hdr), Paragraph("<b>VCG da Base (m)</b>", table_hdr)],
        [Paragraph("Fundo do Casco", table_cell), Paragraph("36,00 m²", table_cell_center), Paragraph("25,0 mm (Tabuado)", table_cell_center), Paragraph("650 kg/m³", table_cell_center), Paragraph("585 kg", table_cell_center), Paragraph("5,10 m", table_cell_center), Paragraph("0,42 m", table_cell_center)],
        [Paragraph("Costado (Bordos)", table_cell), Paragraph("34,10 m²", table_cell_center), Paragraph("28,0 mm (Tabuado)", table_cell_center), Paragraph("650 kg/m³", table_cell_center), Paragraph("620 kg", table_cell_center), Paragraph("5,55 m", table_cell_center), Paragraph("1,65 m", table_cell_center)],
        [Paragraph("Espelho de Popa", table_cell), Paragraph("7,12 m²", table_cell_center), Paragraph("16,0 mm (Madeira)", table_cell_center), Paragraph("650 kg/m³", table_cell_center), Paragraph("74 kg", table_cell_center), Paragraph("0,08 m", table_cell_center), Paragraph("1,80 m", table_cell_center)],
        [Paragraph("Convés e Trincaniz", table_cell), Paragraph("18,75 m²", table_cell_center), Paragraph("16,0 mm (Madeira)", table_cell_center), Paragraph("650 kg/m³", table_cell_center), Paragraph("195 kg", table_cell_center), Paragraph("5,80 m", table_cell_center), Paragraph("2,15 m", table_cell_center)],
        [Paragraph("<b>SUBTOTAL CHAPEAMENTO</b>", table_cell), Paragraph("<b>95,97 m²</b>", table_cell_center), Paragraph("—", table_cell_center), Paragraph("—", table_cell_center), Paragraph("<b>1.474 kg</b>", table_cell_bold), Paragraph("<b>5,32 m</b>", table_cell_bold), Paragraph("<b>1,25 m</b>", table_cell_bold)]
    ]
    t1 = Table(chapa_data, colWidths=[3.8*cm, 2.3*cm, 3.2*cm, 2.4*cm, 2.0*cm, 2.2*cm, 2.1*cm])
    t1.setStyle(TableStyle([
        ('BACKGROUND', (0,0), (-1,0), azul_brana),
        ('GRID', (0,0), (-1,-1), 0.5, cinza_borda),
        ('ROWBACKGROUNDS', (0,1), (-1,-1), [colors.white, cinza_fundo]),
        ('TOPPADDING', (0,0), (-1,-1), 2.5),
        ('BOTTOMPADDING', (0,0), (-1,-1), 2.5),
    ]))
    story.append(t1)
    story.append(Spacer(1, 0.3*cm))
    
    # Seção 2
    story.append(Paragraph("2. Reforços Estruturais e Anteparas de Colisão", h2_style))
    ref_data = [
        [Paragraph("<b>Componente Estrutural</b>", table_hdr), Paragraph("<b>Especificação do Perfil</b>", table_hdr), Paragraph("<b>Qtd. / Espaçamento</b>", table_hdr), Paragraph("<b>Massa (kg)</b>", table_hdr), Paragraph("<b>LCG (m)</b>", table_hdr), Paragraph("<b>VCG (m)</b>", table_hdr)],
        [Paragraph("Longitudinais do Fundo", table_cell), Paragraph("Sarrafos 19 × 63 mm (Massaranduba)", table_cell), Paragraph("6 por bordo (a cada 228 mm)", table_cell_center), Paragraph("268 kg", table_cell_center), Paragraph("5,20 m", table_cell_center), Paragraph("0,35 m", table_cell_center)],
        [Paragraph("Cavernas do Costado", table_cell), Paragraph("Alma 25×175 c/ chapuz 50×25 mm", table_cell), Paragraph("30 cavernas (a cada 387 mm)", table_cell_center), Paragraph("542 kg", table_cell_center), Paragraph("5,45 m", table_cell_center), Paragraph("1,55 m", table_cell_center)],
        [Paragraph("Hastilhas e Quilha Maciça", table_cell), Paragraph("Alma 32×200 c/ chapuz 100×32 mm", table_cell), Paragraph("Quilha corrida + 30 hastilhas", table_cell_center), Paragraph("485 kg", table_cell_center), Paragraph("5,15 m", table_cell_center), Paragraph("0,25 m", table_cell_center)],
        [Paragraph("Vaus do Convés", table_cell), Paragraph("Alma 32×250 c/ chapuz 100×32 mm", table_cell), Paragraph("30 vaus transversais", table_cell_center), Paragraph("522 kg", table_cell_center), Paragraph("5,75 m", table_cell_center), Paragraph("2,20 m", table_cell_center)],
        [Paragraph("Antepara Colisão Vante", table_cell), Paragraph("Chapa estanque 28 mm (Baliza 28)", table_cell), Paragraph("1 unidade a 10,84 m do espelho", table_cell_center), Paragraph("42 kg", table_cell_center), Paragraph("10,84 m", table_cell_center), Paragraph("1,75 m", table_cell_center)],
        [Paragraph("Antepara Estanque Popa", table_cell), Paragraph("Chapa estanque 28 mm (Baliza 3)", table_cell), Paragraph("1 unidade a 1,16 m do espelho", table_cell_center), Paragraph("132 kg", table_cell_center), Paragraph("1,16 m", table_cell_center), Paragraph("1,45 m", table_cell_center)],
        [Paragraph("<b>ESTRUTURA NUA TOTAL</b>", table_cell), Paragraph("<b>Chapa + Reforços + Anteparas</b>", table_cell), Paragraph("<b>Margem Construção (+12%)</b>", table_cell_center), Paragraph("<b>3.881 kg</b>", table_cell_bold), Paragraph("<b>5,40 m</b>", table_cell_bold), Paragraph("<b>1,35 m</b>", table_cell_bold)]
    ]
    t2 = Table(ref_data, colWidths=[3.8*cm, 4.4*cm, 4.2*cm, 1.8*cm, 2.0*cm, 2.0*cm])
    t2.setStyle(TableStyle([
        ('BACKGROUND', (0,0), (-1,0), azul_brana),
        ('GRID', (0,0), (-1,-1), 0.5, cinza_borda),
        ('ROWBACKGROUNDS', (0,1), (-1,-1), [colors.white, cinza_fundo]),
        ('TOPPADDING', (0,0), (-1,-1), 2.5),
        ('BOTTOMPADDING', (0,0), (-1,-1), 2.5),
    ]))
    story.append(t2)
    story.append(Spacer(1, 0.3*cm))
    
    # Seção 3
    story.append(Paragraph("3. Quadro Geral de Pesos e Equilíbrio (Carga Plena - 14.500 kg)", h2_style))
    quadro_data = [
        [Paragraph("<b>Item / Grupo de Carga</b>", table_hdr), Paragraph("<b>Massa (kg)</b>", table_hdr), Paragraph("<b>% Δ</b>", table_hdr), Paragraph("<b>LCG (m)</b>", table_hdr), Paragraph("<b>TCG (m)</b>", table_hdr), Paragraph("<b>VCG (m)</b>", table_hdr), Paragraph("<b>Momento Long. (kgf·m)</b>", table_hdr)],
        [Paragraph("1. Estrutura de Madeira c/ Margem", table_cell), Paragraph("3.881 kg", table_cell_center), Paragraph("26,8%", table_cell_center), Paragraph("5,40 m", table_cell_center), Paragraph("0,00 m", table_cell_center), Paragraph("1,35 m", table_cell_center), Paragraph("20.957 kgf·m", table_cell_center)],
        [Paragraph("2. Lastro de Fundo (Chumbo/Ferro)", table_cell), Paragraph("4.200 kg", table_cell_center), Paragraph("29,0%", table_cell_center), Paragraph("5,20 m", table_cell_center), Paragraph("0,00 m", table_cell_center), Paragraph("0,20 m", table_cell_center), Paragraph("21.840 kgf·m", table_cell_center)],
        [Paragraph("3. Mastro, Verga e Vela de Pena", table_cell), Paragraph("550 kg", table_cell_center), Paragraph("3,8%", table_cell_center), Paragraph("6,50 m", table_cell_center), Paragraph("0,00 m", table_cell_center), Paragraph("6,20 m", table_cell_center), Paragraph("3.575 kgf·m", table_cell_center)],
        [Paragraph("4. Motor Diesel Auxiliar (40 HP)", table_cell), Paragraph("380 kg", table_cell_center), Paragraph("2,6%", table_cell_center), Paragraph("3,20 m", table_cell_center), Paragraph("0,00 m", table_cell_center), Paragraph("0,75 m", table_cell_center), Paragraph("1.216 kgf·m", table_cell_center)],
        [Paragraph("5. Tanques (300 L Diesel + 400 L Água)", table_cell), Paragraph("660 kg", table_cell_center), Paragraph("4,5%", table_cell_center), Paragraph("5,30 m", table_cell_center), Paragraph("0,00 m", table_cell_center), Paragraph("0,57 m", table_cell_center), Paragraph("3.498 kgf·m", table_cell_center)],
        [Paragraph("6. Mobília, Cozinha e Equipamentos", table_cell), Paragraph("650 kg", table_cell_center), Paragraph("4,5%", table_cell_center), Paragraph("5,50 m", table_cell_center), Paragraph("0,00 m", table_cell_center), Paragraph("1,40 m", table_cell_center), Paragraph("3.575 kgf·m", table_cell_center)],
        [Paragraph("7. Tripulação / Passageiros (12 pess.)", table_cell), Paragraph("960 kg", table_cell_center), Paragraph("6,6%", table_cell_center), Paragraph("5,10 m", table_cell_center), Paragraph("0,00 m", table_cell_center), Paragraph("1,90 m", table_cell_center), Paragraph("4.896 kgf·m", table_cell_center)],
        [Paragraph("8. Carga Útil Comercial / Rancho", table_cell), Paragraph("3.219 kg", table_cell_center), Paragraph("22,2%", table_cell_center), Paragraph("5,30 m", table_cell_center), Paragraph("0,00 m", table_cell_center), Paragraph("0,85 m", table_cell_center), Paragraph("17.060 kgf·m", table_cell_center)],
        [Paragraph("<b>TOTAL GERAL (DESLOCAMENTO)</b>", table_cell), Paragraph("<b>14.500 kg</b>", table_cell_bold), Paragraph("<b>100%</b>", table_cell_bold), Paragraph("<b>5,30 m</b>", table_cell_bold), Paragraph("<b>0,00 m</b>", table_cell_bold), Paragraph("<b>1,07 m</b>", table_cell_bold), Paragraph("<b>76.617 kgf·m</b>", table_cell_bold)]
    ]
    t3 = Table(quadro_data, colWidths=[4.2*cm, 2.0*cm, 1.4*cm, 2.0*cm, 2.0*cm, 2.0*cm, 3.4*cm])
    t3.setStyle(TableStyle([
        ('BACKGROUND', (0,0), (-1,0), azul_brana),
        ('GRID', (0,0), (-1,-1), 0.5, cinza_borda),
        ('ROWBACKGROUNDS', (0,1), (-1,-1), [colors.white, cinza_fundo]),
        ('TOPPADDING', (0,0), (-1,-1), 2),
        ('BOTTOMPADDING', (0,0), (-1,-1), 2),
    ]))
    story.append(t3)
    story.append(Spacer(1, 0.3*cm))
    
    concl_txt = (
        "<b>Conclusão Hidrostática e Estabilidade:</b><br/>"
        "• <b>Trim:</b> O Centro de Gravidade Longitudinal ($LCG = 5,30\text{ m}$) coincide perfeitamente com o Centro de Carena da Tabela V4 ($LCB = 5,32\text{ m}$), garantindo <b>Trim Nulo ($0,0^\circ$)</b> em calado de projeto ($922\text{ mm}$).<br/>"
        "• <b>Estabilidade Transversal ($GM_t$):</b> Com $KG = 1,07\text{ m}$ e $KM = 2,30\text{ m}$, a Altura Metacêntrica resultante é de <b>$GM_t = 1,23\text{ m}$</b>, conferindo altíssima rigidez à lona e cumprimento com folga dos critérios de estabilidade intacta da NORMAM-211."
    )
    story.append(Paragraph(concl_txt, body_style))
    story.append(Spacer(1, 0.2*cm))
    story.append(HRFlowable(width="100%", thickness=0.8, color=cinza_borda, spaceBefore=2, spaceAfter=4))
    story.append(Paragraph("<i>Documento técnico oficial emitido pela Brana Projetos Navais e Estaleiro Escola Embrião.</i>", ParagraphStyle('Rod2', parent=body_style, fontSize=7, textColor=colors.HexColor('#6B7280'), alignment=1)))
    
    doc.build(story)

if __name__ == '__main__':
    gerar_pdf_memoria_pesos_grupo2('/home/magdalena/eee/alunos/carlos-silva/grupo-2-tesouro-da-baia/Memoria_Calculo_Pesos_Tesouro_da_Baia.pdf')
