<template>
  <div class="min-h-screen w-full lg:grid lg:grid-cols-2">
    <!-- Left side - Register Form -->
    <div class="flex items-center justify-center py-12">
      <div class="mx-auto grid w-[350px] gap-6">
        <div class="grid gap-2 text-center">
          <h1 class="text-3xl font-bold">Create an account</h1>
          <p class="text-balance text-muted-foreground">
            Enter your information to create an account
          </p>
        </div>
        
        <form @submit.prevent="handleSubmit" class="grid gap-4">
          <div class="grid grid-cols-2 gap-4">
            <div class="grid gap-2">
              <Label for="first_name">First name</Label>
              <Input
                id="first_name"
                v-model="form.first_name"
                placeholder="John"
                required
                :disabled="isLoading"
              />
            </div>
            <div class="grid gap-2">
              <Label for="last_name">Last name</Label>
              <Input
                id="last_name"
                v-model="form.last_name"
                placeholder="Doe"
                required
                :disabled="isLoading"
              />
            </div>
          </div>
          
          <div class="grid gap-2">
            <Label for="email">Email</Label>
            <Input
              id="email"
              v-model="form.email"
              type="email"
              placeholder="m@example.com"
              required
              :disabled="isLoading"
            />
          </div>
          
          <div class="grid gap-2">
            <Label for="password">Password</Label>
            <Input
              id="password"
              v-model="form.password"
              type="password"
              required
              :disabled="isLoading"
            />
          </div>
          
          <div class="grid gap-2">
            <Label for="confirm_password">Confirm Password</Label>
            <Input
              id="confirm_password"
              v-model="confirmPassword"
              type="password"
              required
              :disabled="isLoading"
            />
          </div>
          
          <Button type="submit" class="w-full" :disabled="isLoading || !isFormValid">
            <Loader2 v-if="isLoading" class="mr-2 h-4 w-4 animate-spin" />
            Create account
          </Button>
          
          <Button variant="outline" class="w-full" type="button" @click="handleGoogleSignup">
            <svg class="mr-2 h-4 w-4" viewBox="0 0 24 24">
              <path fill="currentColor" 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="currentColor" 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="currentColor" 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="currentColor" 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>
            Sign up with Google
          </Button>
          
          <div v-if="error" class="text-sm text-destructive text-center">
            {{ error }}
          </div>
          
          <div v-if="successMessage" class="text-sm text-green-600 text-center">
            {{ successMessage }}
          </div>
        </form>
        
        <div class="mt-4 text-center text-sm">
          Already have an account?{" "}
          <router-link to="/login" class="underline">
            Sign in
          </router-link>
        </div>
      </div>
    </div>
    
    <!-- Right side - Image/Illustration -->
    <div class="hidden bg-muted lg:block">
      <div class="flex h-full items-center justify-center p-10">
        <div class="max-w-md text-center">
          <div class="mb-8">
            <!-- VFX Project Management Logo/Icon -->
            <div class="mx-auto mb-4 flex h-20 w-20 items-center justify-center rounded-full bg-primary">
              <svg class="h-10 w-10 text-primary-foreground" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 4.354a4 4 0 110 5.292M15 21H3v-1a6 6 0 0112 0v1zm0 0h6v-1a6 6 0 00-9-5.197m13.5-9a2.5 2.5 0 11-5 0 2.5 2.5 0 015 0z"/>
              </svg>
            </div>
            <h2 class="text-2xl font-bold">Join Our Team</h2>
            <p class="mt-2 text-muted-foreground">
              Start collaborating on amazing VFX and animation projects
            </p>
          </div>
          
          <div class="space-y-4 text-left">
            <div class="flex items-center space-x-3">
              <div class="flex h-8 w-8 items-center justify-center rounded-full bg-primary/10">
                <svg class="h-4 w-4 text-primary" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                  <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M13 10V3L4 14h7v7l9-11h-7z"/>
                </svg>
              </div>
              <span class="text-sm">Fast & Efficient Workflow</span>
            </div>
            
            <div class="flex items-center space-x-3">
              <div class="flex h-8 w-8 items-center justify-center rounded-full bg-primary/10">
                <svg class="h-4 w-4 text-primary" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                  <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 12l2 2 4-4m5.618-4.016A11.955 11.955 0 0112 2.944a11.955 11.955 0 01-8.618 3.04A12.02 12.02 0 003 9c0 5.591 3.824 10.29 9 11.622 5.176-1.332 9-6.03 9-11.622 0-1.042-.133-2.052-.382-3.016z"/>
                </svg>
              </div>
              <span class="text-sm">Secure & Reliable</span>
            </div>
            
            <div class="flex items-center space-x-3">
              <div class="flex h-8 w-8 items-center justify-center rounded-full bg-primary/10">
                <svg class="h-4 w-4 text-primary" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                  <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M7 4V2a1 1 0 011-1h8a1 1 0 011 1v2m-9 0h10m-10 0a2 2 0 00-2 2v14a2 2 0 002 2h10a2 2 0 002-2V6a2 2 0 00-2-2M9 12l2 2 4-4"/>
                </svg>
              </div>
              <span class="text-sm">Professional Tools</span>
            </div>
            
            <div class="flex items-center space-x-3">
              <div class="flex h-8 w-8 items-center justify-center rounded-full bg-primary/10">
                <svg class="h-4 w-4 text-primary" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                  <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M4.318 6.318a4.5 4.5 0 000 6.364L12 20.364l7.682-7.682a4.5 4.5 0 00-6.364-6.364L12 7.636l-1.318-1.318a4.5 4.5 0 00-6.364 0z"/>
                </svg>
              </div>
              <span class="text-sm">Creative Community</span>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { reactive, ref, computed } from 'vue'
import { useRouter } from 'vue-router'
import { Button } from '@/components/ui/button'
import { Input } from '@/components/ui/input'
import { Label } from '@/components/ui/label'
import { Loader2 } from 'lucide-vue-next'
import { useAuthStore } from '@/stores/auth'
import type { RegisterData } from '@/types/auth'

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

const form = reactive<RegisterData>({
  email: '',
  password: '',
  first_name: '',
  last_name: ''
})

const confirmPassword = ref('')
const successMessage = ref('')

const isLoading = computed(() => authStore.isLoading)
const error = computed(() => authStore.error)

const isFormValid = computed(() => {
  return form.email && 
         form.password && 
         form.first_name && 
         form.last_name && 
         form.password === confirmPassword.value
})

const handleSubmit = async () => {
  if (form.password !== confirmPassword.value) {
    authStore.error = 'Passwords do not match'
    return
  }
  
  try {
    authStore.clearError()
    await authStore.register(form)
    
    successMessage.value = 'Account created successfully! Please wait for admin approval before signing in.'
    
    // Redirect to login after a delay
    setTimeout(() => {
      router.push('/login')
    }, 3000)
  } catch (err) {
    // Error is handled by the store
    console.error('Registration error:', err)
  }
}

const handleGoogleSignup = () => {
  // TODO: Implement Google OAuth signup
  console.log('Google signup not implemented yet')
}
</script>