import flet as ft
from pages.auth.auth import Auth
from pages.home.home import Home
from admin.dashboard import Dashboard
from pages.categories.category import Category
from pages.products.product import ProductPage
from pages.profile.profilepage import ProfilePage
from pages.shopping_cart.cart import Cart
from pages.shopping_cart.peload_card import PreloadCard
from pages.shopping_cart.payment_redirect import PaymentRedirect
from pages.details.about_us import AboutUS
from pages.details.terms_and_conditions import TermsAndConditions
from pages.details.cancel_policy import CancelPolicy
from pages.details.confidentialty_policy import ConfidentialtyPolicy
from pages.details.delivery_policy import DeliveryPolicys
from pages.details.gdpr_policy import GDPR

import os
os.environ["FLET_SECRET_KEY"] = os.urandom(12).hex()

from dotenv import load_dotenv
load_dotenv()

def main(page: ft.Page):
    page.title = "Taina Gustului - Condimente, nuci și fructe uscate în Craiova"
    page.theme_mode = ft.ThemeMode.LIGHT
    page.theme = ft.Theme(color_scheme=ft.ColorScheme(primary=ft.Colors.BROWN))
    page.vertical_alignment = ft.MainAxisAlignment.CENTER
    page.horizontal_alignment = ft.CrossAxisAlignment.CENTER
    page.padding = 0

    def route_change(route):
        page.controls.clear()

        if route == "/auth":
            login = Auth(page)
            page.add(login.build())
            page.update()
            return

        if route in ("/home", "/", None):
            home = Home(page)
            page.add(home.build())
            page.update()
            return
        
        if route  == "/admin":
            if not page.client_storage.get("is_authenticated"):
               page.go("/auth")
               return
            dashbaord = Dashboard(page)
            page.add(dashbaord.build())
            page.update()
            return
        
        if 'categorie' in route:
            category = Category(page)
            page.add(category.build())
            page.update()
            return
        
        if 'produs' in route:
            produs = ProductPage(page)
            page.add(produs.build())
            page.update()
            return
        
        if route == "/profil":
            profile = ProfilePage(page)
            page.add(profile.build())
            page.update()
            return
        
        if route == "/cos":
            cart = Cart(page)
            page.add(cart.build())
            page.update()
            return

        if route == "/pre_load_cos":
            preload = PreloadCard(page)
            page.add(preload.build())
            page.update()
            return
        
        if route == '/payment/redirect':
            redirect = PaymentRedirect(page)
            page.add(redirect.build())
            page.update()
            return
        
        if route == '/about_us':
            about_us = AboutUS(page)
            page.add(about_us.build())
            page.update()
            return
        
        if route == '/termeni_si_conditii':
            termeni_si_conditii = TermsAndConditions(page)
            page.add(termeni_si_conditii.build())
            page.update()
            return
        
        if route == '/politica_de_anulare_comanda':
            politica_de_anulare_comanda = CancelPolicy(page)
            page.add(politica_de_anulare_comanda.build())
            page.update()
            return
        
        if route == '/politica_de_confidentialitate':
            politica_de_confidentialitate = ConfidentialtyPolicy(page)
            page.add(politica_de_confidentialitate.build())
            page.update()
            return

        if route == '/politica_de_livrare_comanda':
            politica_de_livrare_comanda = DeliveryPolicys(page)
            page.add(politica_de_livrare_comanda.build())
            page.update()
            return

        if route == '/gdpr':
            gdpr = GDPR(page)
            page.add(gdpr.build())
            page.update()
            return

        # 5) Fallback 404
        page.add(ft.Text("404: Page not found"))
        page.update()

    page.on_route_change = lambda _: route_change(page.route)
    page.go("/")

ft.app(
    target=main,
    assets_dir="assets",
    upload_dir="uploads",
    view=ft.WEB_BROWSER,
    port=8080,
    host="0.0.0.0"
)