<template>
  <div class="min-h-screen bg-noctune-cream flex items-center justify-center p-8">
    <div class="w-full max-w-5xl bg-noctune-teal rounded-3xl border-4 border-black overflow-hidden flex shadow-2xl">
      <div class="w-1/2 p-12 relative">
        <div class="absolute top-8 right-12 grid grid-cols-4 gap-1">
          <span v-for="i in 16" :key="i" class="w-2 h-2 bg-black/40 rounded-full"></span>
        </div>
        <div class="absolute top-20 right-8 grid grid-cols-4 gap-1">
          <span v-for="i in 12" :key="i" class="w-2 h-2 bg-black/40 rounded-full"></span>
        </div>
        <div class="absolute top-16 left-20 text-black/60 text-4xl font-bold">+</div>
        <div class="absolute bottom-24 right-20 text-black/60 text-4xl font-bold">+</div>
        <div class="absolute bottom-12 left-12 grid grid-cols-3 gap-1">
          <span v-for="i in 12" :key="i" class="w-2 h-2 bg-black/40 rounded-full"></span>
        </div>
        <div class="flex flex-col items-center justify-center h-full">
          <img src="/logo_transparent.png" alt="Noctune" class="w-48 h-48 object-contain" loading="lazy" />
        </div>
      </div>

      <div class="w-1/2 bg-white rounded-3xl m-4 p-10">
        <h2 class="text-4xl font-black text-black mb-2">WELCOME!</h2>
        <p class="text-gray-600 mb-8">Login to continue</p>
        <p v-if="error" class="text-red-500 text-sm mb-4 font-medium">{{ error }}</p>

        <div class="mb-6">
          <label class="block text-xs font-bold text-gray-600 mb-2 tracking-wider">EMAIL/USERNAME</label>
          <div class="flex border-2 border-black rounded-lg overflow-hidden">
            <div class="bg-noctune-teal p-3 border-r-2 border-black">
              <svg class="w-6 h-6 text-black" fill="currentColor" viewBox="0 0 24 24"><path d="M12 12c2.21 0 4-1.79 4-4s-1.79-4-4-4-4 1.79-4 4 1.79 4 4 4zm0 2c-2.67 0-8 1.34-8 4v2h16v-2c0-2.66-5.33-4-8-4z"/></svg>
            </div>
            <input type="text" v-model="identifier" @keyup.enter="handleLogin" class="flex-1 px-4 py-3 focus:outline-none text-black" placeholder="Enter your email" />
          </div>
        </div>

        <div class="mb-4">
          <label class="block text-xs font-bold text-gray-600 mb-2 tracking-wider">PASSWORD</label>
          <div class="flex border-2 border-black rounded-lg overflow-hidden">
            <div class="bg-amber-200 p-3 border-r-2 border-black">
              <svg class="w-6 h-6 text-black" fill="currentColor" viewBox="0 0 24 24"><path d="M18 8h-1V6c0-2.76-2.24-5-5-5S7 3.24 7 6v2H6c-1.1 0-2 .9-2 2v10c0 1.1.9 2 2 2h12c1.1 0 2-.9 2-2V10c0-1.1-.9-2-2-2zm-6 9c-1.1 0-2-.9-2-2s.9-2 2-2 2 .9 2 2-.9 2-2 2zm3.1-9H8.9V6c0-1.71 1.39-3.1 3.1-3.1 1.71 0 3.1 1.39 3.1 3.1v2z"/></svg>
            </div>
            <input :type="showPassword ? 'text' : 'password'" v-model="password" @keyup.enter="handleLogin" class="flex-1 px-4 py-3 focus:outline-none text-black" placeholder="Enter your password" />
            <button @click="showPassword = !showPassword" class="px-4 text-gray-500 hover:text-gray-700">
              <svg v-if="!showPassword" class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M13.875 18.825A10.05 10.05 0 0112 19c-4.478 0-8.268-2.943-9.543-7a9.97 9.97 0 011.563-3.029m5.858.908a3 3 0 114.243 4.243M9.878 9.878l4.242 4.242M9.88 9.88l-3.29-3.29m7.532 7.532l3.29 3.29M3 3l3.59 3.59m0 0A9.953 9.953 0 0112 5c4.478 0 8.268 2.943 9.543 7a10.025 10.025 0 01-4.132 5.411m0 0L21 21"/></svg>
              <svg v-else class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M15 12a3 3 0 11-6 0 3 3 0 016 0z"/><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M2.458 12C3.732 7.943 7.523 5 12 5c4.478 0 8.268 2.943 9.542 7-1.274 4.057-5.064 7-9.542 7-4.477 0-8.268-2.943-9.542-7z"/></svg>
            </button>
          </div>
        </div>

        <div class="text-right mb-6">
          <a href="#" class="text-sm text-gray-600 hover:text-black">Forgot password?</a>
        </div>

        <button @click="handleLogin" :disabled="loading" class="w-full bg-noctune-orange border-2 border-black rounded-lg py-4 font-bold text-white text-xl flex items-center justify-center gap-3 hover:bg-orange-600 transition-colors disabled:opacity-50">
          <span v-if="loading" class="inline-block w-5 h-5 border-2 border-white border-t-transparent rounded-full animate-spin"></span>
          <template v-else>
            <div class="bg-white/20 rounded p-1">
              <svg class="w-5 h-5" fill="currentColor" viewBox="0 0 24 24"><path d="M12 4l-1.41 1.41L16.17 11H4v2h12.17l-5.58 5.59L12 20l8-8z"/></svg>
            </div>
            LOGIN
          </template>
        </button>

        <div class="flex items-center my-6">
          <div class="flex-1 border-t-2 border-gray-300"></div>
          <span class="px-4 bg-black text-white text-xs font-bold py-1 rounded">OR SIGN IN WITH</span>
          <div class="flex-1 border-t-2 border-gray-300"></div>
        </div>

        <button class="w-full border-2 border-gray-300 rounded-xl py-3 flex items-center justify-center gap-3 hover:bg-gray-50 transition-colors">
          <svg class="w-6 h-6" viewBox="0 0 24 24">
            <path fill="#4285F4" d="M22.56 12.25c0-.78-.07-1.53-.2-2.25H12v4.26h5.92c-.26 1.37-1.04 2.53-2.21 3.31v2.77h3.57c2.08-1.92 3.28-4.74 3.28-8.09z"/>
            <path fill="#34A853" d="M12 23c2.97 0 5.46-.98 7.28-2.66l-3.57-2.77c-.98.66-2.23 1.06-3.71 1.06-2.86 0-5.29-1.93-6.16-4.53H2.18v2.84C3.99 20.53 7.7 23 12 23z"/>
            <path fill="#FBBC05" d="M5.84 14.09c-.22-.66-.35-1.36-.35-2.09s.13-1.43.35-2.09V7.07H2.18C1.43 8.55 1 10.22 1 12s.43 3.45 1.18 4.93l2.85-2.22.81-.62z"/>
            <path fill="#EA4335" d="M12 5.38c1.62 0 3.06.56 4.21 1.64l3.15-3.15C17.45 2.09 14.97 1 12 1 7.7 1 3.99 3.47 2.18 7.07l3.66 2.84c.87-2.6 3.3-4.53 6.16-4.53z"/>
          </svg>
          <span class="font-bold text-gray-700">GOOGLE</span>
        </button>

        <p class="text-center mt-8 text-gray-600">
          Don&apos;t have an account yet?
          <router-link to="/register" class="text-noctune-orange font-bold hover:underline">Register now</router-link>
        </p>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import { useAuthStore } from '../stores/authStore.js'

const router = useRouter()
const authStore = useAuthStore()

const identifier = ref('')
const password = ref('')
const showPassword = ref(false)
const loading = ref(false)
const error = ref('')

async function handleLogin() {
  error.value = ''
  loading.value = true
  const result = await authStore.login(identifier.value, password.value)
  loading.value = false
  if (result.success) {
    router.push('/home')
  } else {
    error.value = result.message
  }
}
</script>
