import customtkinter as ctk
from components.auth.login import login_frame
from components.auth.register import register_frame
from components.shop.create_shop import create_shop_frame
from components.shop.view_shop import view_shop_frame
from components.product.create_product import product_frame
from components.admin.category import category_frame
from components.dashboard import dashboard_frame  # import the dashboard

API_URL = "http://127.0.0.1:8000"
access_token = None


def switch_frame(frame_name, token=None):
    global access_token
    if token:
        access_token = token

    frame = frames.get(frame_name, login)
    if hasattr(frame, "update_token"):
        frame.update_token(access_token)
    frame.tkraise()


ctk.set_appearance_mode("dark")
ctk.set_default_color_theme("blue")

root = ctk.CTk()
root.title("Shopping App")
root.geometry("1000x800")

# Create Frames
login = login_frame(root, switch_frame, API_URL)
register = register_frame(root, switch_frame, API_URL)
create_shop = create_shop_frame(root, switch_frame, API_URL, access_token)
view_shop = view_shop_frame(root, switch_frame, API_URL, access_token)
product = product_frame(root, switch_frame, API_URL, access_token)
category = category_frame(root, switch_frame, API_URL, access_token)
dashboard = dashboard_frame(root, switch_frame, API_URL, access_token)

frames = {
    "login": login,
    "register": register,
    "create_shop": create_shop,
    "create_product": product,
    "category": category,
    "view_shop": view_shop,
    "dashboard": dashboard,
}

for frame in frames.values():
    frame.place(relx=0, rely=0, relwidth=1, relheight=1)  # Adjusted height for full scaling

# Show the login frame first
switch_frame("login")

root.mainloop()