<template>
  <div class="auth-page">
    <div class="auth-container">
      <div class="auth-card">
        <div class="card">
          <div class="card-header text-center">
            <h1 class="auth-title">Регистрация</h1>
          </div>

          <div class="card-body">
            <form class="auth-form" @submit="onSubmit">
              <!-- Поле email -->
              <div class="form-group">
                <label for="register-email" class="form-label">Email</label>
                <input
                id="register-email" 
                v-model="email" 
                type="email" 
                class="form-input"
                :class="{ 'error': errors.email }" 
                placeholder="your@email.com" 
                autocomplete="email" 
                required>
                <span v-if="errors.email" class="error-message">
                  {{ errors.email }}
                </span>
              </div>

              <!-- Поле имени -->
              <div class="form-group">
                <label for="register-first-name" class="form-label">Имя</label>
                <input 
                id="register-first-name" 
                v-model="firstName" 
                type="text" 
                class="form-input"
                :class="{ 'error': errors.first_name }" 
                placeholder="Ваше имя" 
                autocomplete="given-name" 
                required>
                <span v-if="errors.first_name" class="error-message">
                  {{ errors.first_name }}
                </span>
              </div>

              <!-- Поле фамилии -->
              <div class="form-group">
                <label for="register-last-name" class="form-label">Фамилия</label>
                <input 
                id="register-last-name" 
                v-model="lastName" 
                type="text" 
                class="form-input"
                :class="{ 'error': errors.last_name }" 
                placeholder="Ваша фамилия" 
                autocomplete="family-name" 
                required>
                <span v-if="errors.last_name" class="error-message">
                  {{ errors.last_name }}
                </span>
              </div>

              <!-- Поле пароля -->
              <div class="form-group">
                <label for="register-password" class="form-label">Пароль</label>
                <input 
                id="register-password" 
                v-model="password" 
                type="password" 
                class="form-input"
                :class="{ 'error': errors.password }" 
                placeholder="Минимум 6 символов" 
                autocomplete="new-password"
                required>
                <span v-if="errors.password" class="error-message">
                  {{ errors.password }}
                </span>
              </div>

              <!-- Подтверждение пароля -->
              <div class="form-group">
                <label for="register-password-confirm" class="form-label">Подтверждение пароля</label>
                <input 
                id="register-password-confirm" 
                v-model="passwordConfirm" 
                type="password" 
                class="form-input"
                :class="{ 'error': errors.passwordConfirm }" 
                placeholder="Повторите пароль"
                autocomplete="new-password" 
                required>
                <span v-if="errors.passwordConfirm" class="error-message">
                  {{ errors.passwordConfirm }}
                </span>
              </div>

              <button type="submit" class="btn btn-primary auth-button" :disabled="isSubmitting || auth.loading.value">
                <span v-if="isSubmitting || auth.loading.value">Регистрация...</span>
                <span v-else>Зарегистрироваться</span>
              </button>
            </form>
          </div>

          <div class="card-footer text-center">
            <p class="auth-footer-text">
              Уже есть аккаунт?
              <NuxtLink to="/auth/login" class="auth-link">
                Войдите
              </NuxtLink>
            </p>
            <NuxtLink to="/" class="auth-link home-link">
              ← На главную
            </NuxtLink>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { useField, useForm } from 'vee-validate'
import { registerSchema } from '~/schemas/auth'
import type { RegisterForm } from '~/types/auth'

definePageMeta({
  middleware: 'auth',
  layout: 'auth'
})

const auth = useAuth()
const { handleSubmit, errors, isSubmitting } = useForm({
  validationSchema: registerSchema
})

const { value: email } = useField('email')
const { value: firstName } = useField('first_name')
const { value: lastName } = useField('last_name')
const { value: password } = useField('password')
const { value: passwordConfirm } = useField('passwordConfirm')

const onSubmit = handleSubmit(async (values) => {
  try {
    await auth.register(values as RegisterForm)
    await navigateTo('/profile')
    // eslint-disable-next-line @typescript-eslint/no-explicit-any
  } catch (error: any) {
    console.error('Registration error:', error)

    if (error.status === 422) {
      alert('Неверные данные для регистрации')
    } else if (error.status === 409) {
      alert('Пользователь с таким email уже существует')
    } else {
      alert('Произошла ошибка при регистрации. Попробуйте позже.')
    }
  }
})
</script>

<style scoped>
/* Ваши существующие стили остаются без изменений */
.auth-page {
  min-height: 100vh;
  background: var(--bg-secondary);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: var(--space-md);
}

.auth-container {
  width: 100%;
  max-width: 400px;
}

.auth-card {
  width: 100%;
}

.card {
  background: var(--bg-primary);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-md);
  border: 1px solid var(--border-light);
  overflow: hidden;
}

.card-header {
  padding: var(--space-xl) var(--space-xl) 0;
}

.card-body {
  padding: var(--space-lg) var(--space-xl);
}

.card-footer {
  padding: 0 var(--space-xl) var(--space-xl);
  border-top: 1px solid var(--border-light);
  background: var(--bg-secondary);
}

.auth-title {
  font-family: var(--font-heading);
  font-size: var(--text-2xl);
  font-weight: var(--font-semibold);
  color: var(--text-primary);
  margin-bottom: var(--space-sm);
}

.auth-form {
  display: flex;
  flex-direction: column;
  gap: var(--space-md);
}

.form-group {
  display: flex;
  flex-direction: column;
  gap: var(--space-xs);
}

.form-label {
  font-weight: var(--font-medium);
  color: var(--text-secondary);
  font-size: var(--text-sm);
}

.form-input {
  width: 100%;
  padding: var(--space-sm) var(--space-md);
  border: 1px solid var(--border-medium);
  border-radius: var(--radius-md);
  font-size: var(--text-base);
  background: var(--bg-primary);
  color: var(--text-primary);
  transition: all 0.2s;
}

.form-input:focus {
  outline: none;
  border-color: var(--primary-500);
  box-shadow: 0 0 0 3px rgb(14 165 233 / 0.1);
}

.form-input.error {
  border-color: var(--error-500);
  box-shadow: 0 0 0 3px rgb(239 68 68 / 0.1);
}

.error-message {
  color: var(--error-500);
  font-size: var(--text-sm);
  margin-top: var(--space-xs);
  display: block;
}

.auth-button {
  width: 100%;
  margin-top: var(--space-sm);
  padding: var(--space-sm) var(--space-md);
  font-size: var(--text-base);
}

.auth-button:disabled {
  opacity: 0.6;
  cursor: not-allowed;
}

.auth-footer-text {
  color: var(--text-secondary);
  margin-bottom: var(--space-md);
  font-size: var(--text-sm);
}

.auth-link {
  color: var(--primary-600);
  font-weight: var(--font-medium);
  text-decoration: none;
  font-size: var(--text-sm);
  transition: color 0.2s;
}

.auth-link:hover {
  color: var(--primary-700);
  text-decoration: underline;
}

.home-link {
  display: inline-block;
  margin-top: var(--space-sm);
  font-size: var(--text-sm);
}

/* Адаптивность для мобильных устройств */
@media (max-width: 640px) {
  .auth-container {
    max-width: 100%;
  }

  .card-header {
    padding: var(--space-lg) var(--space-lg) 0;
  }

  .card-body {
    padding: var(--space-md) var(--space-lg);
  }

  .card-footer {
    padding: 0 var(--space-lg) var(--space-lg);
  }

  .auth-title {
    font-size: var(--text-xl);
  }
}

/* Для очень маленьких экранов */
@media (max-width: 380px) {
  .auth-page {
    padding: var(--space-sm);
  }

  .card-header {
    padding: var(--space-md) var(--space-md) 0;
  }

  .card-body {
    padding: var(--space-sm) var(--space-md);
  }

  .card-footer {
    padding: 0 var(--space-md) var(--space-md);
  }
}
</style>