from graphviz import Digraph

# Criar o diagrama de entidade-relacionamento
dot = Digraph(comment='ERD - E-Rubble MVP')
dot.attr(rankdir='LR', fontsize='10')

# Tabelas principais
tables = {
    "clientes": ["id", "tipo_pessoa", "cpf_cnpj", "nome_razao_social", "email_principal"],
    "enderecos_cliente": ["id", "cliente_id", "tipo_endereco", "cep", "logradouro"],
    "contatos_cliente": ["id", "cliente_id", "tipo_contato", "nome", "email"],
    "trucks": ["id", "placa", "modelo", "capacidade_m3", "motorista_atual_id"],
    "truck_maintenance": ["id", "id_truck", "tipo", "fornecedor_id", "valor_custo"],
    "insurance_policies": ["id", "tipo", "id_relacionado", "seguradora", "valor_total"],
    "bank_accounts": ["id", "banco_nome", "agencia", "numero_conta"],
    "alerts": ["id", "titulo", "descricao", "tipo", "data_alerta"],
    "truck_refuels": ["id", "id_truck", "data_abastecimento", "litros", "valor_total"]
}

# Adicionar as tabelas e seus campos
for table, fields in tables.items():
    label = f"{table}|{'|'.join(fields)}"
    dot.node(table, label=label, shape='record')

# Relacionamentos (chaves estrangeiras)
relations = [
    ("enderecos_cliente", "clientes"),
    ("contatos_cliente", "clientes"),
    ("truck_maintenance", "trucks"),
    ("truck_maintenance", "suppliers"),
    ("truck_refuels", "trucks"),
    ("truck_refuels", "employees"),
    ("insurance_policies", "trucks"),
    ("trucks", "employees"),
]

# Adicionar relacionamentos
for child, parent in relations:
    dot.edge(child, parent)

dot.render('/mnt/data/e_rubble_erd', format='png', cleanup=False)
'/mnt/data/e_rubble_erd.png'
