import streamlit as st
import pandas as pd

inventory = st.session_state["inventory"]
orders = st.session_state["orders"]
product = st.session_state["product"]
spare_parts = st.session_state["spare_parts"]

st.title("Search Inventory & Orders")
tabs = st.tabs(["Inventory", "Orders", "Products", "Spare Parts"])

with tabs[0]:
    search_text = st.text_input("Search Inventory")
    df = inventory.copy()
    df_display = df.rename(columns={
    "product_id": "Product ID",
    "warehouse_id": "Warehouse",
    "q_on_hand": "On Hand",
    "q_reserved": "Reserved",
    "last_update": "Last Updated",
    "condition": "Condition",
    "sku": "SKU",
    "q_in_transit": "In Transit",
    "reorder_point": "Reorder Point",
    "location": "Location",
    "safety_stock": "Safety Stock",
    "inventory_id": "Inventory ID"
})

    
    if search_text:
        mask = df_display.astype(str).apply(lambda col: col.str.contains(search_text, case=False, na=False))
        df_display = df_display[mask.any(axis=1)]
    st.dataframe(df_display)
    
with tabs[1]:
    search_text = st.text_input("Search Orders")
    df = orders.copy()
    df_display = df.rename(columns={
    "order_id": "Order ID",
    "order_date": "Order Date",
    "customer_id": "Customer ID",
    "customer_name": "Customer Name",
    "customer_location": "Customer Location",
    "order_total": "Order Total",
    "items": "Items",
    "assigned_warehouse_id": "Warehouse",
    "status": "Status",
    "payment_method": "Payment Method",
    "shipping_address": "Shipment Address",
    "estimated_delivery": "Estimated Delivery"
})
    if search_text:
        mask = df_display.astype(str).apply(lambda col: col.str.contains(search_text, case=False, na=False))
        df_display = df_display[mask.any(axis=1)]
    st.dataframe(df_display)
    
with tabs[2]:
    search_text = st.text_input("Search Products")
    df = product.copy()
    df_display = df.rename(columns={
    "product_name": "Product Name",
    "sku": "SKU",
    "category": "Category",
    "base_price": "Base Price",
    "model_num": "Model Number",
    "description": "Description",
    "weight": "Weight",
    "dimensions": "Dimensions",
    "brand": "Brand",
})
    if search_text:
        mask = df_display.astype(str).apply(lambda col: col.str.contains(search_text, case=False, na=False))
        df_display = df_display[mask.any(axis=1)]
    st.dataframe(df_display)
    
with tabs[3]:
    search_text = st.text_input("Search Spare Parts")
    df = spare_parts.copy()
    df_display = df.rename(columns={
    "part_id": "Part ID",
    "main_product_model": "Product ID",
    "part_name": "Part Name",
    "part_sku": "SKU",
    "part_price": "Part Price",
    "availability": "Availability",
    "estimate_shipment_days": "Estimated Shipment Days",
    "part_supplier": "Supplier",
    "part_num": "Part Number",
    "compatibility_notes": "Compatibility Notes",
})
    if search_text:
        mask = df_display.astype(str).apply(lambda col: col.str.contains(search_text, case=False, na=False))
        df_display = df_display[mask.any(axis=1)]
    st.dataframe(df_display)    
    
    



