import requests
import streamlit as st
import pandas as pd
from typing import Dict, List, Any
import os
from dotenv import load_dotenv
from pandas import json_normalize

load_dotenv()


class WarehouseAPI:
    def __init__(self):
        # self.base_url = "http://127.0.0.1:8000"
        
        self.base_url = os.getenv("API_BASE", "http://127.0.0.1:8000")
        self.headers = {}
        print("API_BASE =", self.base_url)

    # ---------------- INVENTORY ----------------
    def get_inventory(self) -> pd.DataFrame:
        response = requests.get(f"{self.base_url}/api/inventory", headers=self.headers)
        response.raise_for_status()
        return pd.DataFrame(response.json())

    # ---------------- PRODUCTS ----------------D
    def get_products(self) -> pd.DataFrame:
        response = requests.get(f"{self.base_url}/api/products", headers=self.headers)
        response.raise_for_status()
        return pd.DataFrame(response.json())

    # ---------------- WAREHOUSES ----------------
    def get_warehouses(self) -> pd.DataFrame:
        response = requests.get(f"{self.base_url}/api/warehouses", headers=self.headers)
        response.raise_for_status()
        return pd.DataFrame(response.json())
    
       # ---------------- ORDER ITEMS ----------------
    def get_order_items(self) -> pd.DataFrame:
        """Fetch order items (each row = one SKU per order)."""
        response = requests.get(f"{self.base_url}/api/order_items", headers=self.headers)
        response.raise_for_status()
        return pd.DataFrame(response.json())
    
       
    # ---------------- ORDERS ----------------
    def get_orders(self) -> pd.DataFrame:
        # response = requests.get(f"{self.base_url}/api/orders", headers=self.headers)
        # response.raise_for_status()
        # return pd.DataFrame(response.json())
        
        response = requests.get(f"{self.base_url}/api/orders", headers=self.headers)
        response.raise_for_status()
        data = response.json()
        return json_normalize(data)

    # ---------------- SPARE PARTS ----------------
    def get_spare_parts(self) -> pd.DataFrame:
        response = requests.get(f"{self.base_url}/api/spare_parts", headers=self.headers)
        response.raise_for_status()
        return pd.DataFrame(response.json())
    
 
    
    # ---------------- RECOMMENDATIONS ----------------
    def get_recommendations(self) -> List[Dict[str, Any]]:
        response = requests.get(f"{self.base_url}/api/recommendations", headers=self.headers)
        response.raise_for_status()
        return response.json()

    def approve_recommendation(self, rec_id: str) -> bool:
        response = requests.post(f"{self.base_url}/api/v1/recommendations/{rec_id}/approve", headers=self.headers)
        return response.status_code == 200
    
    
    def transfer_inventory(self, sku, from_wh, to_wh, quantity):
        payload = {
        "sku": sku,
        "from_wh": from_wh,
        "to_wh": to_wh,
        "quantity": quantity
    }
        return requests.post(f"{self.base_url}/transfer", json=payload).json()
