"""
Welcome page component for FastHTML application.

Provides a protected welcome page that displays user information.
This page is only accessible to authenticated users.
"""

from fasthtml.common import Div, H1, H2, P, Button, A
from typing import Optional, Dict, Any


class WelcomePage:
  """
  Welcome page component for authenticated users.

  Attributes:
      user_info: Dictionary containing user information (email, id, etc.)
      redirect_url: URL for logout redirect
  """
  
  def __init__(self, user_info: Optional[Dict[str, Any]] = None, redirect_url: str = "/login"):
    """
    Initialize welcome page.

    Args:
        user_info: Dictionary containing user information
        redirect_url: URL to redirect after logout (default: "/login")
    """
    self.user_info = user_info or {}
    self.redirect_url = redirect_url
  
  def _render(self):
    """
    Render the welcome page HTML structure.

    Returns:
        FastHTML component tree for the welcome page
    """
    # Extract user info
    email = self.user_info.get('email', 'Guest')
    user_id = self.user_info.get('id', 'N/A')
    
    # Hero section
    hero = Div(
      Div(
        # Welcome message
        H1(
          f"Welcome back, {email}!",
          cls="text-5xl font-bold"
        ),
        
        P(
          "You have successfully logged in to your account.",
          cls="py-6 text-lg"
        ),
        
        # Action buttons
        Div(
          Button(
            "Logout",
            cls="btn btn-primary",
            hx_post="/logout",
            hx_target="body",
            hx_swap="outerHTML"
          ),
          cls="flex gap-2"
        ),
        
        cls="max-w-md"
      ),
      cls="hero min-h-screen bg-base-200"
    )
    
    # User info card
    info_card = Div(
      Div(
        H2("Your Information", cls="card-title mb-4"),
        
        # User details
        Div(
          self._info_row("Email", email),
          self._info_row("User ID", str(user_id)),
          cls="space-y-2"
        ),
        
        cls="card-body"
      ),
      cls="card bg-base-100 shadow-xl w-full max-w-md"
    )
    
    # Main container
    return Div(
      # Hero section
      hero,
      
      # Info card (positioned absolutely over hero)
      Div(
        info_card,
        cls="absolute top-20 right-20 hidden lg:block"
      ),
      
      cls="relative"
    )
  
  def _info_row(self, label: str, value: str):
    """
    Create an information row for displaying user details.

    Args:
        label: Label for the information
        value: Value to display

    Returns:
        Div containing label and value
    """
    return Div(
      Div(
        P(label, cls="font-semibold text-sm text-base-content/70"),
        P(value, cls="text-base"),
        cls="flex flex-col"
      ),
      cls="py-2 border-b border-base-300 last:border-b-0"
    )
  
  def __ft__(self):
    """
    FastHTML rendering protocol method.

    Returns:
        Rendered welcome page component
    """
    return self._render()


class SimpleWelcomePage:
  """
  Simpler version of welcome page without user info card.
  Useful for quick prototyping or minimal designs.
  """
  
  def __init__(self, user_email: str = "Guest"):
    """
    Initialize simple welcome page.

    Args:
        user_email: User's email address
    """
    self.user_email = user_email
  
  def _render(self):
    """
    Render the simple welcome page.

    Returns:
        FastHTML component tree
    """
    return Div(
      Div(
        Div(
          H1(f"Welcome, {self.user_email}!", cls="text-4xl font-bold mb-4"),
          P("You are now logged in.", cls="mb-6 text-lg"),
          
          Button(
            "Logout",
            cls="btn btn-primary",
            hx_post="/logout",
            hx_target="body",
            hx_swap="outerHTML"
          ),
          
          cls="text-center"
        ),
        cls="hero-content"
      ),
      cls="hero min-h-screen bg-base-200"
    )
  
  def __ft__(self):
    """
    FastHTML rendering protocol method.

    Returns:
        Rendered simple welcome page
    """
    return self._render()