import { auth } from '../services/auth.js';
import { renderLogin } from '../components/Login.js';

export function renderLoginPage(onLoginSuccess) {
  const container = document.createElement('div');
  container.className = 'login-page';

  const loginComponent = renderLogin(async (email, password) => {
    if (await auth.login(email, password)) {
      if (onLoginSuccess) {
        onLoginSuccess();
      } else {
        window.location.hash = '#dashboard';
      }
    } else {
      const error = container.querySelector('#login-error');
      error.textContent = 'Credenciales inválidas';
    }
  });

  container.appendChild(loginComponent);

  const sessionExpiredMessage = sessionStorage.getItem('session-expired-message');
  if (sessionExpiredMessage) {
    const error = container.querySelector('#login-error');
    if (error) {
      error.textContent = sessionExpiredMessage;
    }
    sessionStorage.removeItem('session-expired-message');
  }

  return container;
}
