import streamlit as st
from bank import Bank

# ---------------- PAGE CONFIG ----------------
st.set_page_config(
    page_title="🏦 Smart Bank",
    page_icon="🏦",
    layout="wide"
)

# ---------------- CUSTOM CSS ----------------
st.markdown("""
<style>
.main {
    padding-top: 1rem;
}

.header-box {
    padding: 20px;
    border-radius: 15px;
    background: linear-gradient(90deg, #1f77b4, #4CAF50);
    color: white;
    text-align: center;
    margin-bottom: 20px;
}

.stButton > button {
    width: 100%;
    border-radius: 12px;
    height: 3em;
    font-size: 16px;
    font-weight: bold;
}

.stTextInput > div > div > input,
.stNumberInput > div > div > input {
    border-radius: 10px;
}

.card {
    padding: 20px;
    border-radius: 15px;
    background-color: #f8f9fa;
    box-shadow: 0px 4px 10px rgba(0,0,0,0.1);
}
</style>
""", unsafe_allow_html=True)

# ---------------- HEADER ----------------
st.markdown("""
<div class="header-box">
    <h1>🏦 Smart Bank Management System</h1>
    <p>Create Account • Deposit • Withdraw • Manage Accounts</p>
</div>
""", unsafe_allow_html=True)

# ---------------- SIDEBAR ----------------
st.sidebar.title("🏦 Bank Menu")

menu = st.sidebar.selectbox(
    "Choose Action",
    [
        "Create Account",
        "Deposit",
        "Withdraw",
        "Show Details",
        "Update Info",
        "Delete Account"
    ]
)

# ---------------- CREATE ACCOUNT ----------------
if menu == "Create Account":

    st.subheader("🆕 Create New Account")

    col1, col2 = st.columns(2)

    with col1:
        name = st.text_input("👤 Your Name")

    with col2:
        age = st.number_input("🎂 Your Age", min_value=18, step=1)

    email = st.text_input("📧 Your Email")
    pin = st.text_input("🔐 4-Digit PIN", type="password")

    if st.button("Create Account"):

        if not name or not email or not pin:
            st.warning("⚠️ Fill all fields")

        elif not pin.isdigit() or len(pin) != 4:
            st.error("PIN must be exactly 4 digits")

        else:
            user, msg = Bank.create_account(
                name,
                int(age),
                email,
                int(pin)
            )

            st.success(msg)
            st.balloons()

            if user:
                st.info(
                    f"🏦 Your Account Number: {user['accountNo.']}"
                )

# ---------------- DEPOSIT ----------------
elif menu == "Deposit":

    st.subheader("💰 Deposit Money")

    acc_no = st.text_input("Account Number")
    pin = st.text_input("PIN", type="password")

    amount = st.number_input(
        "Deposit Amount",
        min_value=1,
        step=100
    )

    if st.button("Deposit"):

        if not pin:
            st.error("Enter PIN")

        else:
            success, msg = Bank.deposit(
                acc_no,
                int(pin),
                int(amount)
            )

            if success:
                st.success(msg)
            else:
                st.error(msg)

# ---------------- WITHDRAW ----------------
elif menu == "Withdraw":

    st.subheader("💸 Withdraw Money")

    acc_no = st.text_input("Account Number")
    pin = st.text_input("PIN", type="password")

    amount = st.number_input(
        "Withdraw Amount",
        min_value=1,
        step=100
    )

    if st.button("Withdraw"):

        if not pin:
            st.error("Enter PIN")

        else:
            success, msg = Bank.withdraw(
                acc_no,
                int(pin),
                int(amount)
            )

            if success:
                st.success(msg)
            else:
                st.error(msg)

# ---------------- SHOW DETAILS ----------------
elif menu == "Show Details":

    st.subheader("👤 Account Details")

    acc_no = st.text_input("Account Number")
    pin = st.text_input("PIN", type="password")

    if st.button("Show Details"):

        if not pin:
            st.error("Enter PIN")

        else:
            user = Bank.find_user(
                acc_no,
                int(pin)
            )

            if user:

                st.markdown("### 📋 Account Information")

                col1, col2 = st.columns(2)

                with col1:
                    st.metric("👤 Name", user["name"])
                    st.metric("🎂 Age", user["age"])

                with col2:
                    st.metric(
                        "💰 Balance",
                        f"₹ {user['balance']}"
                    )
                    st.metric(
                        "🏦 Account No",
                        user["accountNo."]
                    )

                st.write("📧 Email :", user["email"])

            else:
                st.error("No account found")

# ---------------- UPDATE INFO ----------------
elif menu == "Update Info":

    st.subheader("✏️ Update Information")

    acc_no = st.text_input("Account Number")
    pin = st.text_input(
        "Current PIN",
        type="password"
    )

    name = st.text_input("New Name")
    email = st.text_input("New Email")
    new_pin = st.text_input(
        "New PIN",
        type="password"
    )

    if st.button("Update Details"):

        if not pin:
            st.error("Enter Current PIN")

        else:
            success, msg = Bank.update_user(
                acc_no,
                int(pin),
                name,
                email,
                new_pin
            )

            if success:
                st.success(msg)
            else:
                st.error(msg)

# ---------------- DELETE ACCOUNT ----------------
elif menu == "Delete Account":

    st.subheader("🗑️ Delete Account")

    acc_no = st.text_input("Account Number")
    pin = st.text_input(
        "PIN",
        type="password"
    )

    if st.button("Delete Account"):

        if not pin:
            st.error("Enter PIN")

        else:
            success, msg = Bank.delete_user(
                acc_no,
                int(pin)
            )

            if success:
                st.success(msg)
            else:
                st.error(msg)