import flet as ft
from pages.register_page import Register
from pages.forgot_password_page import ForgotPassword
from pages.two_factor_page import TwoFactorAuth
import requests
from config import API_BASE_URL

class Login:
    def __init__(self, page: ft.Page, auth):
        self.page = page
        self.auth = auth
        self.email = ft.TextField(label="Email")
        self.passwd = ft.TextField(label="Password", password=True, can_reveal_password=True)
        self.error_message = ft.Text("", color=ft.Colors.RED)

    def on_login_click(self, e):
        self.error_message.value = ""
        self.error_message.update()
        self.page.update()

        email = self.email.value.strip()
        password = self.passwd.value.strip()

        if not email or not password:
            self.error_message.value = "Please enter both email and password."
            self.error_message.update()
            return

        try:
            # Call login endpoint
            response = requests.post(
                f"{API_BASE_URL}/auth/login",
                json={"email": email, "password": password},
                timeout=15,
            )

            # Attempt to parse JSON safely
            resp_ct = response.headers.get("content-type", "")
            resp_json = None
            if "application/json" in resp_ct.lower():
                try:
                    resp_json = response.json()
                except Exception:
                    resp_json = None

            if response.status_code == 200 and resp_json is not None:
                access_token = resp_json.get("access_token", "")
                # Fetch user info (best-effort)
                ui = requests.get(
                    f"{API_BASE_URL}/auth/me",
                    headers={"Authorization": f"Bearer {access_token}"},
                    timeout=10,
                )

                if ui.status_code == 200:
                    try:
                        uj = ui.json()
                        logo_filename = uj.get("logo_filename", "")
                        if logo_filename:
                            self.page.client_storage.set("custom_logo", logo_filename)
                    except Exception:
                        pass

                # Proceed to 2FA
                self.auth.placeholder.content.clean()
                two_factor = TwoFactorAuth(self.page, email, self, self.auth)
                self.auth.placeholder.content = two_factor.build()
                self.auth.placeholder.update()

            else:
                # Build readable error message
                err_msg = None
                if resp_json and isinstance(resp_json, dict):
                    err_msg = resp_json.get("error") or resp_json.get("message")
                if not err_msg:
                    # fallback to raw text / status code
                    t = response.text.strip()
                    err_msg = t if t else f"HTTP {response.status_code} from server"

                self.error_message.value = f"Login failed: {err_msg}"
                self.error_message.update()

        except Exception as ex:
            self.error_message.value = f"Login error: {str(ex)}"
            self.error_message.update()

    def on_forgot_btn_click(self, e):
        self.auth.placeholder.content.clean()
        forgot_passwd = ForgotPassword(self.page, self.auth, self)
        self.auth.placeholder.content = forgot_passwd.build()
        self.auth.placeholder.update()
    
    def on_register_btn_click(self, e):
        print('Go to register')
        register = Register(self.page, self.auth, self)
        self.auth.placeholder.content.clean()
        self.auth.placeholder.content = register.build()
        self.auth.placeholder.update()

    def build(self):
        return ft.Column(
            [
                ft.Text(
                    "Login", 
                    size=30, 
                    weight="bold"
                ),
                self.email,
                self.passwd,
                ft.ElevatedButton(
                    "Login", 
                    width = 150,
                    on_click=self.on_login_click
                ),
                self.error_message,
                ft.Row(
                    [
                        ft.TextButton("Forgot Password?", on_click=self.on_forgot_btn_click),
                        ft.TextButton("Register", on_click=self.on_register_btn_click)
                    ],
                    alignment=ft.MainAxisAlignment.SPACE_BETWEEN
                )
            ],
            horizontal_alignment=ft.CrossAxisAlignment.CENTER,
            alignment=ft.MainAxisAlignment.CENTER,
            spacing=20,
            width=350
        )