<template>
  <div class="login-container">
    <!-- Animated Background -->
    <div class="animated-bg">
      <div class="floating-shape shape-1"></div>
      <div class="floating-shape shape-2"></div>
      <div class="floating-shape shape-3"></div>
      <div class="floating-shape shape-4"></div>
      <div class="floating-shape shape-5"></div>
      <div class="floating-shape shape-6"></div>
    </div>

    <!-- Login Form Container -->
    <div class="login-form-container">
      <!-- Logo Section -->
      <div class="logo-section">
        <div class="logo-icon">
          <img src="/images/animation/bubble.gif" alt="Doctors Hero" width="60" height="60" style="border-radius: 15px;">
        </div>
        <h2 class="logo-text">Doctors Hero</h2>
      </div>

      <!-- Login Form -->
      <div class="login-form-card">
        <h3 class="login-title">Login</h3>
        
        <form @submit.prevent="handleLogin" class="login-form">
          <!-- Email Field -->
          <div class="input-group">
            <label for="email" class="input-label">Email</label>
            <input 
              type="email" 
              id="email"
              v-model="form.email"
              :class="['form-input', { 'error': errors.email }]"
              placeholder="youremail@gmail.com"
              required
              :disabled="loading"
            >
            <span v-if="errors.email" class="error-message">{{ errors.email }}</span>
          </div>

          <!-- Password Field -->
          <div class="input-group">
            <label for="password" class="input-label">Password</label>
            <input 
              type="password" 
              id="password"
              v-model="form.password"
              :class="['form-input', { 'error': errors.password }]"
              placeholder="Password"
              required
              :disabled="loading"
            >
            <span v-if="errors.password" class="error-message">{{ errors.password }}</span>
          </div>

          <!-- Forgot Password Link -->
          <div class="forgot-password">
            <router-link to="/forgot-password" class="forgot-link">Forgot Password?</router-link>
          </div>

          <!-- Sign In Button -->
          <button 
            type="submit" 
            :class="['signin-btn', { 'loading': loading }]"
            :disabled="loading"
          >
            <span v-if="!loading">Sign in</span>
            <span v-else class="loading-spinner">
              <i class="fa fa-spinner fa-spin"></i> Signing in...
            </span>
          </button>

          <!-- Error Message -->
          <div v-if="errors.general" class="general-error">
            {{ errors.general }}
          </div>

          <!-- Social Login Divider -->
          <div class="social-divider">
            <span>or continue with</span>
          </div>

          <!-- Social Login Buttons -->
          <div class="social-buttons">
            <button type="button" class="social-btn google-btn" @click="loginWithGoogle">
              <i class="fab fa-google"></i>
            </button>
            <button type="button" class="social-btn github-btn" @click="loginWithGithub">
              <i class="fab fa-github"></i>
            </button>
            <button type="button" class="social-btn facebook-btn" @click="loginWithFacebook">
              <i class="fab fa-facebook-f"></i>
            </button>
          </div>

          <!-- Register Link -->
          <div class="register-link">
            <span>Don't have an account yet? </span>
            <router-link to="/register" class="register-text">Register for free</router-link>
          </div>
        </form>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { useRouter } from 'vue-router'
import axios from 'axios'

const router = useRouter()
const loading = ref(false)

// Form data
const form = reactive({
  email: '',
  password: ''
})

// Error handling
const errors = reactive({
  email: '',
  password: '',
  general: ''
})

// Clear errors when user types
const clearErrors = () => {
  errors.email = ''
  errors.password = ''
  errors.general = ''
}

// Handle form submission
const handleLogin = async () => {
  clearErrors()
  loading.value = true

  try {
    // Get CSRF token
    await axios.get('/sanctum/csrf-cookie')
    
    // Submit login request
    const response = await axios.post('/api/login', {
      email: form.email,
      password: form.password
    })

    // Handle successful login
    if (response.data.success) {
      const user = response.data.user
      
      // Store user data in localStorage (you might want to use a more secure method)
      localStorage.setItem('user', JSON.stringify(user))
      localStorage.setItem('token', response.data.token)
      
      // Redirect based on user role
      redirectToDashboard(user.role)
    }
  } catch (error) {
    console.error('Login error:', error)
    
    if (error.response?.status === 422) {
      // Validation errors
      const validationErrors = error.response.data.errors
      if (validationErrors.email) errors.email = validationErrors.email[0]
      if (validationErrors.password) errors.password = validationErrors.password[0]
    } else if (error.response?.status === 401) {
      // Authentication failed
      errors.general = 'Invalid email or password. Please try again.'
    } else {
      // General error
      errors.general = 'Login failed. Please try again later.'
    }
  } finally {
    loading.value = false
  }
}

// Role-based dashboard redirection to Laravel Blade views
const redirectToDashboard = (role) => {
  switch (role) {
    case 'doctor':
      window.location.href = '/doctor-dashboard'
      break
    case 'patient':
      window.location.href = '/patient-dashboard'
      break
    case 'receptionist':
      window.location.href = '/doctor-dashboard?limited=true' // Limited access to doctor panel
      break
    case 'hospital_admin':
      window.location.href = '/admin-dashboard'
      break
    case 'nurse':
      window.location.href = '/nurse-dashboard'
      break
    default:
      window.location.href = '/dashboard' // Default Breeze dashboard
  }
}

// Social login methods (placeholder implementations)
const loginWithGoogle = () => {
  console.log('Google login not implemented yet')
  // Implement Google OAuth
}

const loginWithGithub = () => {
  console.log('GitHub login not implemented yet')
  // Implement GitHub OAuth
}

const loginWithFacebook = () => {
  console.log('Facebook login not implemented yet')
  // Implement Facebook OAuth
}
</script>

<style scoped>
.login-container {
  min-height: 100vh;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
}

/* Animated Background */
.animated-bg {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  overflow: hidden;
  z-index: 1;
}

.floating-shape {
  position: absolute;
  background: rgba(255, 255, 255, 0.1);
  border-radius: 50%;
  animation: float 6s ease-in-out infinite;
}

.shape-1 {
  width: 80px;
  height: 80px;
  top: 10%;
  left: 10%;
  animation-delay: 0s;
}

.shape-2 {
  width: 120px;
  height: 120px;
  top: 20%;
  right: 15%;
  animation-delay: 1s;
}

.shape-3 {
  width: 60px;
  height: 60px;
  bottom: 30%;
  left: 20%;
  animation-delay: 2s;
}

.shape-4 {
  width: 100px;
  height: 100px;
  bottom: 20%;
  right: 25%;
  animation-delay: 3s;
}

.shape-5 {
  width: 140px;
  height: 140px;
  top: 50%;
  left: 5%;
  animation-delay: 4s;
}

.shape-6 {
  width: 90px;
  height: 90px;
  top: 70%;
  right: 10%;
  animation-delay: 5s;
}

@keyframes float {
  0%, 100% {
    transform: translateY(0px) rotate(0deg);
    opacity: 0.7;
  }
  33% {
    transform: translateY(-20px) rotate(120deg);
    opacity: 0.9;
  }
  66% {
    transform: translateY(20px) rotate(240deg);
    opacity: 0.8;
  }
}

/* Login Form Container */
.login-form-container {
  position: relative;
  z-index: 10;
  width: 100%;
  max-width: 420px;
  padding: 0 20px;
}

/* Logo Section */
.logo-section {
  text-align: center;
  margin-bottom: 30px;
}

.logo-icon {
  margin-bottom: 15px;
}

.logo-text {
  color: white;
  font-size: 24px;
  font-weight: 600;
  margin: 0;
  text-shadow: 0 2px 4px rgba(0, 0, 0, 0.3);
}

/* Login Form Card */
.login-form-card {
  background: rgba(255, 255, 255, 0.15);
  backdrop-filter: blur(20px);
  border: 1px solid rgba(255, 255, 255, 0.2);
  border-radius: 20px;
  padding: 40px 30px;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.3);
}

.login-title {
  color: white;
  font-size: 28px;
  font-weight: 700;
  text-align: center;
  margin: 0 0 30px 0;
  text-shadow: 0 2px 4px rgba(0, 0, 0, 0.3);
}

/* Form Styling */
.login-form {
  width: 100%;
}

.input-group {
  margin-bottom: 20px;
}

.input-label {
  display: block;
  color: rgba(255, 255, 255, 0.9);
  font-size: 14px;
  font-weight: 500;
  margin-bottom: 8px;
}

.form-input {
  width: 100%;
  padding: 15px 20px;
  background: rgba(255, 255, 255, 0.9);
  border: 1px solid rgba(255, 255, 255, 0.3);
  border-radius: 12px;
  font-size: 16px;
  color: #333;
  transition: all 0.3s ease;
  box-sizing: border-box;
}

.form-input:focus {
  outline: none;
  background: rgba(255, 255, 255, 0.95);
  border-color: #667eea;
  box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.3);
}

.form-input.error {
  border-color: #ff6b6b;
  background: rgba(255, 107, 107, 0.1);
}

.error-message {
  color: #ff6b6b;
  font-size: 12px;
  margin-top: 5px;
  display: block;
}

.general-error {
  background: rgba(255, 107, 107, 0.2);
  color: #ff6b6b;
  padding: 12px;
  border-radius: 8px;
  text-align: center;
  margin: 15px 0;
  font-size: 14px;
}

/* Forgot Password */
.forgot-password {
  text-align: right;
  margin-bottom: 25px;
}

.forgot-link {
  color: rgba(255, 255, 255, 0.8);
  font-size: 14px;
  text-decoration: none;
  transition: color 0.3s ease;
}

.forgot-link:hover {
  color: white;
  text-decoration: underline;
}

/* Sign In Button */
.signin-btn {
  width: 100%;
  padding: 15px;
  background: rgba(255, 255, 255, 0.2);
  backdrop-filter: blur(10px);
  border: 1px solid rgba(255, 255, 255, 0.3);
  border-radius: 12px;
  color: white;
  font-size: 16px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.3s ease;
  margin-bottom: 20px;
}

.signin-btn:hover:not(:disabled) {
  background: rgba(255, 255, 255, 0.3);
  transform: translateY(-2px);
  box-shadow: 0 5px 15px rgba(0, 0, 0, 0.3);
}

.signin-btn:disabled {
  opacity: 0.7;
  cursor: not-allowed;
}

.loading-spinner i {
  margin-right: 8px;
}

/* Social Login */
.social-divider {
  text-align: center;
  margin: 25px 0;
  position: relative;
}

.social-divider::before {
  content: '';
  position: absolute;
  top: 50%;
  left: 0;
  right: 0;
  height: 1px;
  background: rgba(255, 255, 255, 0.3);
}

.social-divider span {
  background: rgba(255, 255, 255, 0.15);
  padding: 0 15px;
  color: rgba(255, 255, 255, 0.8);
  font-size: 14px;
  position: relative;
}

.social-buttons {
  display: flex;
  gap: 15px;
  justify-content: center;
  margin-bottom: 25px;
}

.social-btn {
  width: 50px;
  height: 50px;
  border-radius: 12px;
  border: 1px solid rgba(255, 255, 255, 0.3);
  background: rgba(255, 255, 255, 0.1);
  color: white;
  font-size: 18px;
  cursor: pointer;
  transition: all 0.3s ease;
  display: flex;
  align-items: center;
  justify-content: center;
}

.social-btn:hover {
  background: rgba(255, 255, 255, 0.2);
  transform: translateY(-2px);
}

/* Register Link */
.register-link {
  text-align: center;
  color: rgba(255, 255, 255, 0.8);
  font-size: 14px;
}

.register-text {
  color: white;
  text-decoration: none;
  font-weight: 600;
  transition: color 0.3s ease;
}

.register-text:hover {
  text-decoration: underline;
}

/* Responsive Design */
@media (max-width: 480px) {
  .login-form-card {
    padding: 30px 20px;
    margin: 0 15px;
  }
  
  .logo-text {
    font-size: 20px;
  }
  
  .login-title {
    font-size: 24px;
  }
}
</style>