"""
Register page component for FastHTML application.

Provides a styled registration form using DaisyUI 5 components.
"""

from fasthtml.common import Div, Form, Input, Button, H1, P, A, Label


class RegisterPage:
  """
  Register page component with email and password fields.

  Attributes:
      error_message: Optional error message to display
      redirect_url: URL to redirect after successful registration
  """
  
  def __init__(self, error_message: str = None, redirect_url: str = "/login"):
    """
    Initialize register page.

    Args:
        error_message: Optional error message to display
        redirect_url: URL to redirect after successful registration (default: "/login")
    """
    self.error_message = error_message
    self.redirect_url = redirect_url
  
  def _render(self):
    """
    Render the register page HTML structure.

    Returns:
        FastHTML component tree for the register page
    """
    # Error alert component (only shown if error_message exists)
    error_alert = None
    if self.error_message:
      error_alert = Div(
        Div(
          self._svg_icon(),
          self.error_message,
          cls="flex items-center gap-2"
        ),
        role="alert",
        cls="alert alert-error mb-4",
        id="error-alert"
      )
    
    # Success message container (empty by default, filled by HTMX on success)
    success_alert = Div(id="success-alert")
    
    # Register form
    register_form = Form(
      # Hidden field for redirect URL
      Input(type="hidden", name="redirect_url", value=self.redirect_url),
      
      # Email input
      Label(
        Div("Email", cls="label-text"),
        cls="form-control w-full mb-4"
      )(
        Input(
          type="email",
          name="email",
          placeholder="[email protected]",
          required=True,
          cls="input input-bordered w-full"
        )
      ),
      
      # Password input
      Label(
        Div("Password", cls="label-text"),
        Div("Must be at least 8 characters", cls="label-text-alt text-base-content/60"),
        cls="form-control w-full mb-4"
      )(
        Input(
          type="password",
          name="password",
          placeholder="Create a password",
          required=True,
          minlength="8",
          cls="input input-bordered w-full"
        )
      ),
      
      # Confirm password input
      Label(
        Div("Confirm Password", cls="label-text"),
        cls="form-control w-full mb-4"
      )(
        Input(
          type="password",
          name="confirm_password",
          placeholder="Confirm your password",
          required=True,
          minlength="8",
          cls="input input-bordered w-full"
        )
      ),
      
      # Submit button
      Button(
        "Create Account",
        type="submit",
        cls="btn btn-primary w-full"
      ),
      
      method="post",
      action="/register",
      hx_post="/register",
      hx_target="#register-card",
      hx_swap="outerHTML"
    )
    
    # Main card container
    card = Div(
      Div(
        # Card title
        H1("Create Account", cls="text-3xl font-bold text-center mb-2"),
        P("Sign up for a new account", cls="text-center text-base-content/70 mb-6"),
        
        # Success alert
        success_alert,
        
        # Error alert
        error_alert if error_alert else Div(),
        
        # Register form
        register_form,
        
        # Login link
        Div(
          P(
            "Already have an account? ",
            A("Sign in", href="/login", cls="link link-primary"),
            cls="text-center text-sm"
          ),
          cls="mt-4"
        ),
        
        cls="card-body"
      ),
      cls="card bg-base-100 w-96 shadow-xl",
      id="register-card"
    )
    
    # Full page container
    return Div(
      card,
      cls="flex items-center justify-center min-h-screen bg-base-200"
    )
  
  def _svg_icon(self):
    """
    Helper method to create error icon SVG.

    Returns:
        SVG icon as NotStr
    """
    from fasthtml.common import NotStr
    return NotStr('''
            <svg xmlns="http://www.w3.org/2000/svg"
                 class="h-6 w-6 shrink-0 stroke-current"
                 fill="none"
                 viewBox="0 0 24 24">
                <path stroke-linecap="round"
                      stroke-linejoin="round"
                      stroke-width="2"
                      d="M10 14l2-2m0 0l2-2m-2 2l-2-2m2 2l2 2m7-2a9 9 0 11-18 0 9 9 0 0118 0z" />
            </svg>
        ''')
  
  def __ft__(self):
    """
    FastHTML rendering protocol method.

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


def create_success_message(message: str = "Account created successfully!"):
  """
  Create a success alert component for HTMX response.

  Args:
      message: Success message to display

  Returns:
      Success alert component
  """
  from fasthtml.common import NotStr
  
  return Div(
    Div(
      NotStr('''
                <svg xmlns="http://www.w3.org/2000/svg"
                     class="h-6 w-6 shrink-0 stroke-current"
                     fill="none"
                     viewBox="0 0 24 24">
                    <path stroke-linecap="round"
                          stroke-linejoin="round"
                          stroke-width="2"
                          d="M9 12l2 2 4-4m6 2a9 9 0 11-18 0 9 9 0 0118 0z" />
                </svg>
            '''),
      message,
      cls="flex items-center gap-2"
    ),
    role="alert",
    cls="alert alert-success mb-4"
  )
