<script setup lang="ts">
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import { useAuthStore } from '@/stores/auth'
import { useI18n } from 'vue-i18n'

const emit = defineEmits(['toggle'])
const { t } = useI18n()
const router = useRouter()
const authStore = useAuthStore()

const email = ref('')
const password = ref('')
const keepSession = ref(true)
const isLoading = ref(false)
const errorMessage = ref('')
const showPassword = ref(false)

const handleLogin = async () => {
  if (!email.value || !password.value) return
  
  isLoading.value = true
  errorMessage.value = ''

  try {
    await authStore.login(email.value.trim().toLowerCase(), password.value, keepSession.value)
    
    const role = (authStore.role || 'PASSENGER').toUpperCase()
    if (role === 'ADMIN') router.push('/admin')
    else if (role === 'DRIVER') router.push('/driver')
    else if (role === 'PROMOTER') router.push('/promoter')
    else router.push('/map')

  } catch (error: any) {
    console.error('SIB | Error Login:', error)
    if (error.message?.includes('Invalid login credentials')) {
      errorMessage.value = t('auth.invalidCreds')
    } else {
      errorMessage.value = error.message || t('common.error')
    }
  } finally {
    isLoading.value = false
  }
}
</script>

<template>
  <div class="login-form-content">
    <form @submit.prevent="handleLogin" class="premium-form">
      <!-- Welcome Text (Mobile-ish) -->
      <div class="welcome-header">
        <h2>{{ t('auth.loginTab') }}</h2>
        <p>Bienvenido de vuelta a SIB</p>
      </div>

      <!-- Input Group -->
      <div class="input-group">
        <div class="field-container">
          <label>{{ t('auth.emailLabel') }}</label>
          <div class="input-inner">
            <span class="material-icons i-icon">mail_outline</span>
            <input
              v-model="email"
              type="email"
              placeholder="tu@correo.com"
              required
            />
          </div>
        </div>

        <div class="field-container">
          <div class="label-row">
            <label>{{ t('auth.passLabel') }}</label>
          </div>
          <div class="input-inner">
            <span class="material-icons i-icon">lock_outline</span>
            <input
              v-model="password"
              :type="showPassword ? 'text' : 'password'"
              placeholder="••••••••"
              required
            />
            <button type="button" class="eye-btn" @click="showPassword = !showPassword">
              <span class="material-icons">{{ showPassword ? 'visibility_off' : 'visibility' }}</span>
            </button>
          </div>
        </div>
      </div>

      <div class="form-options">
        <label class="custom-checkbox">
          <input type="checkbox" v-model="keepSession" />
          <span class="checkmark"></span>
          <span class="cb-text">{{ t('auth.keepSession') }}</span>
        </label>
      </div>

      <!-- Error UI -->
      <Transition name="shake">
        <div v-if="errorMessage" class="error-toast">
          <span class="material-icons">warning_amber</span>
          <span>{{ errorMessage }}</span>
        </div>
      </Transition>

      <button type="submit" class="submit-action" :disabled="isLoading">
        <span v-if="isLoading" class="loader-white"></span>
        <span v-else>{{ t('auth.loginTab') }}</span>
        <span v-if="!isLoading" class="material-icons">arrow_forward</span>
      </button>

      <div class="switch-footer">
        <span>{{ t('auth.noAccount') }}</span>
        <button type="button" @click="emit('toggle')" class="text-link">
          {{ t('auth.registerHere') }}
        </button>
      </div>
    </form>
  </div>
</template>

<style scoped>
.login-form-content {
  padding: 1.5rem 2rem;
}

.premium-form {
  display: flex;
  flex-direction: column;
  gap: 1.5rem;
}

.welcome-header {
  margin-bottom: 0.5rem;
}

.welcome-header h2 {
  font-size: 1.5rem;
  font-weight: 800;
  margin: 0;
  color: #fff;
}

.welcome-header p {
  font-size: 0.9rem;
  color: rgba(255,255,255,0.5);
  margin: 0.25rem 0 0;
}

.input-group {
  display: flex;
  flex-direction: column;
  gap: 1.25rem;
}

.field-container {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}

.field-container label {
  font-size: 0.8rem;
  font-weight: 700;
  color: rgba(255,255,255,0.6);
  text-transform: uppercase;
  letter-spacing: 0.05em;
  padding-left: 0.25rem;
}

.input-inner {
  position: relative;
  display: flex;
  align-items: center;
}

.i-icon {
  position: absolute;
  left: 1rem;
  font-size: 1.25rem;
  color: rgba(255,255,255,0.4);
}

.input-inner input {
  width: 100%;
  background: rgba(255,255,255,0.05);
  border: 1.5px solid rgba(255,255,255,0.1);
  border-radius: 1rem;
  padding: 1rem 1rem 1rem 3rem;
  color: #fff;
  font-family: inherit;
  font-size: 1rem;
  transition: all 0.2s;
}

.input-inner input:focus {
  outline: none;
  background: rgba(255,255,255,0.08);
  border-color: var(--active-color);
  box-shadow: 0 0 0 4px rgba(254, 231, 21, 0.1);
}

.eye-btn {
  position: absolute;
  right: 1rem;
  background: none;
  border: none;
  color: rgba(255,255,255,0.4);
  cursor: pointer;
  display: flex;
}

/* Checkbox */
.custom-checkbox {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  cursor: pointer;
  user-select: none;
}

.custom-checkbox input {
  display: none;
}

.checkmark {
  width: 1.25rem;
  height: 1.25rem;
  background: rgba(255,255,255,0.05);
  border: 1.5px solid rgba(255,255,255,0.1);
  border-radius: 6px;
  position: relative;
  transition: all 0.2s;
}

.custom-checkbox input:checked + .checkmark {
  background: var(--active-color);
  border-color: var(--active-color);
}

.checkmark:after {
  content: "";
  position: absolute;
  left: 6px;
  top: 2px;
  width: 5px;
  height: 10px;
  border: solid #000;
  border-width: 0 2px 2px 0;
  transform: rotate(45deg);
  display: none;
}

.custom-checkbox input:checked + .checkmark:after {
  display: block;
}

.cb-text {
  font-size: 0.9rem;
  color: rgba(255,255,255,0.7);
}

/* Submit Action */
.submit-action {
  width: 100%;
  padding: 1.1rem;
  background: var(--active-color);
  color: #101820;
  border: none;
  border-radius: 1.25rem;
  font-size: 1.1rem;
  font-weight: 800;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 0.75rem;
  transition: all 0.3s cubic-bezier(0.175, 0.885, 0.32, 1.275);
}

.submit-action:hover:not(:disabled) {
  transform: scale(1.02);
  box-shadow: 0 10px 20px rgba(254, 231, 21, 0.2);
}

.submit-action:active:not(:disabled) {
  transform: scale(0.98);
}

.submit-action:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.error-toast {
  background: rgba(239, 68, 68, 0.15);
  color: #fb7185;
  padding: 0.875rem 1rem;
  border-radius: 1rem;
  font-size: 0.9rem;
  font-weight: 600;
  display: flex;
  align-items: center;
  gap: 0.75rem;
  border: 1px solid rgba(239, 68, 68, 0.2);
}

.switch-footer {
  text-align: center;
  font-size: 0.9rem;
  color: rgba(255,255,255,0.5);
}

.text-link {
  background: none;
  border: none;
  color: var(--active-color);
  font-weight: 800;
  padding: 0;
  margin-left: 0.5rem;
  cursor: pointer;
  text-decoration: underline;
}

.loader-white {
  width: 1.5rem;
  height: 1.5rem;
  border: 3px solid rgba(0,0,0,0.1);
  border-top-color: #000;
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}

@keyframes spin { to { transform: rotate(360deg); } }

.shake-enter-active {
  animation: shake 0.4s cubic-bezier(.36,.07,.19,.97) both;
}

@keyframes shake {
  10%, 90% { transform: translate3d(-1px, 0, 0); }
  20%, 80% { transform: translate3d(2px, 0, 0); }
  30%, 50%, 70% { transform: translate3d(-4px, 0, 0); }
  40%, 60% { transform: translate3d(4px, 0, 0); }
}
</style>
