# app.py
import streamlit as st
import math
import matplotlib.pyplot as plt
import numpy as np

# Page Configuration with Icon
st.set_page_config(
    page_title="🔧 Pipe Sizing Helper",
    page_icon="💧",
    layout="centered"
)

# Custom CSS for Styling
st.markdown("""
    <style>
        .title {
            text-align: center;
            font-size: 2.5em;
            font-weight: bold;
            color: #4CAF50;
        }
        .subtitle {
            text-align: center;
            font-size: 1.2em;
            color: #555;
        }
        .result {
            text-align: center;
            font-size: 1.3em;
            font-weight: bold;
            color: #2196F3;
            margin-top: 20px;
        }
        .footer {
            text-align: center;
            margin-top: 50px;
            font-size: 0.9em;
            color: #777;
        }
        .icon {
            text-align: center;
            font-size: 4em;
            color: #FF9800;
        }
        .button {
            text-align: center;
            margin-top: 30px;
        }
    </style>
""", unsafe_allow_html=True)

# Header with Icon
st.markdown("<div class='icon'>💧</div>", unsafe_allow_html=True)
st.markdown("<h1 class='title'>🔧 Pipe Sizing Helper</h1>", unsafe_allow_html=True)
st.markdown("<p class='subtitle'>Calculate optimal pipe size based on flow rate and velocity</p>", unsafe_allow_html=True)

# User Inputs
st.subheader("💡 Input Parameters")
flow_rate = st.number_input("🌊 Enter Flow Rate (m³/s):", min_value=0.0, format="%.4f")
velocity = st.number_input("💨 Enter Permissible Velocity (m/s):", min_value=0.1, format="%.2f")

# Button for Calculation and Visualization
st.markdown("<div class='button'>", unsafe_allow_html=True)
if st.button("🔄 Generate Pipe Diameter and Visualize"):
    if velocity > 0 and flow_rate > 0:
        # Formula: Diameter = sqrt((4 * Flow Rate) / (π * Velocity))
        diameter = math.sqrt((4 * flow_rate) / (math.pi * velocity))
        st.markdown(f"<p class='result'>✅ Recommended Pipe Diameter: {diameter:.2f} meters</p>", unsafe_allow_html=True)
        
        # Visualization
        st.subheader("📊 Visualization: Pipe Diameter vs Flow Rate and Velocity")
        flow_rates = np.linspace(0.1, flow_rate * 2, 100)
        velocities = np.linspace(0.1, velocity * 2, 100)
        diameters = [math.sqrt((4 * fr) / (math.pi * velocity)) for fr in flow_rates]

        # Plot
        fig, ax = plt.subplots()
        ax.plot(flow_rates, diameters, label='Pipe Diameter (m)', lw=2)
        ax.scatter(flow_rate, diameter, color='red', zorder=5, label='Your Input')
        ax.set_xlabel('Flow Rate (m³/s)')
        ax.set_ylabel('Pipe Diameter (m)')
        ax.set_title('Pipe Diameter vs Flow Rate')
        ax.legend()
        ax.grid(True)

        st.pyplot(fig)
    else:
        st.warning("⚠️ Please enter valid positive values for flow rate and velocity.")
st.markdown("</div>", unsafe_allow_html=True)

# Footer
st.markdown("<p class='footer'>💻 <b>Developed by ChatGPT</b></p>", unsafe_allow_html=True)