from fasthtml.components import *


class LoginPage:
  def __init__(self, settings_manager, error_message=None, success_message=None):
    self.settings_manager = settings_manager
    self.error_message = error_message
    self.success_message = success_message
  
  def render(self):
    message_alert = None
    if self.error_message:
      message_alert = Div(
        P(self.error_message, cls="text-sm"),
        cls="bg-error border border-red-400 text-red-700 px-4 py-3 rounded mb-4"
      )
    elif self.success_message:
      message_alert = Div(
        P(self.success_message, cls="text-sm"),
        cls="bg-success border border-green-400 text-green-700 px-4 py-3 rounded mb-4"
      )
    
    return Div(
      # Page title
      H1("Sign In", cls="text-3xl font-bold text-center mb-6"),
      
      # Login Form
      Div(
        # Message alert
        message_alert if message_alert else "",
        
        # Email login form
        Form(
          # Email field
          Div(
            Label("Email", For="email", cls="block text-sm font-medium text-gray-700 mb-1"),
            Input(
              type="email",
              id="email",
              name="email",
              placeholder="you@example.com",
              required=True,
              cls="w-full px-3 py-2 border rounded-md focus:outline-none focus:ring-2"
            ),
            cls="mb-4"
          ),
          
          # Password field
          Div(
            Label("Password", For="password", cls="block text-sm font-medium text-gray-700 mb-1"),
            Input(
              type="password",
              id="password",
              name="password",
              placeholder="Your password",
              required=True,
              cls="w-full px-3 py-2 border rounded-md focus:outline-none focus:ring-2"
            ),
            cls="mb-6"
          ),
          
          # Submit button
          Button(
            "Sign In",
            type="submit",
            cls="btn w-full font-bold py-2 px-4 rounded"
          ),
          
          action=ROUTE_ROOT + Routes.LoginByEmail,
          method="post",
          cls="mb-6"
        ),
        
        # Registration link
        Div(
          P(
            "Don't have an account? ",
            A("Register here", href="/register", cls="text-blue-600 hover:underline"),
            cls="text-sm text-gray-600 text-center"
          )
        ),
        
        cls="p-8 rounded-lg shadow-2xl max-w-md mx-auto"
      )
    
    )
