<template>
  <AppLayout>
    <div class="max-w-4xl mx-auto">
      <div class="card">
        <div class="flex items-center space-x-4 mb-6">
          <div class="relative">
            <div class="w-20 h-20 bg-gradient-to-br from-primary-500 to-primary-700 rounded-full flex items-center justify-center text-white text-2xl font-bold">
              {{ userInitials }}
            </div>
            <!-- <button class="absolute bottom-0 right-0 p-1 bg-white rounded-full shadow-md hover:shadow-lg transition-shadow">
              <svg class="w-4 h-4 text-gray-600" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M3 9a2 2 0 012-2h.93a2 2 0 001.664-.89l.812-1.22A2 2 0 0110.07 4h3.86a2 2 0 011.664.89l.812 1.22A2 2 0 0018.07 7H19a2 2 0 012 2v9a2 2 0 01-2 2H5a2 2 0 01-2-2V9z" />
                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M15 13a3 3 0 11-6 0 3 3 0 016 0z" />
              </svg>
            </button> -->
          </div>
          <div>
            <h1 class="text-2xl font-bold text-gray-900">{{ t('profile.title') }}</h1>
            <p class="text-gray-500">{{ t('profile.subtitle') }}</p>
          </div>
        </div>

        <form @submit.prevent="saveProfile" class="space-y-6">
          <div class="grid grid-cols-1 md:grid-cols-2 gap-6">
            <div>
              <label class="block text-sm font-medium text-gray-700 mb-1">{{ t('common.username') }}</label>
              <input v-model="userStore.login" type="text" disabled class="input-field bg-gray-100" />
            </div>
            <div>
              <label class="block text-sm font-medium text-gray-700 mb-1">{{ t('common.role') }}</label>
              <div class="relative">
                <input :value="userStore.role === 'admin' ? t('app.administrator') : t('app.user')" type="text" disabled class="input-field bg-gray-100" />
                <div class="absolute right-3 top-2.5">
                  <span class="px-2 py-0.5 text-xs rounded-full" :class="userStore.role === 'admin' ? 'bg-purple-100 text-purple-700' : 'bg-gray-100 text-gray-600'">
                    {{ userStore.role === 'admin' ? 'Admin' : 'User' }}
                  </span>
                </div>
              </div>
            </div>
          </div>

          <div>
            <label class="block text-sm font-medium text-gray-700 mb-1">{{ t('common.email') }}</label>
            <input v-model="form.email" type="email" required class="input-field" />
          </div>

          <div>
            <label class="block text-sm font-medium text-gray-700 mb-1">{{ t('profile.newPassword') }}</label>
            <input v-model="form.password" type="password" class="input-field" autocomplete="new-password" />
            <p class="text-xs text-gray-500 mt-1">{{ t('common.leavePasswordBlank') }}</p>
          </div>

          <div>
            <label class="block text-sm font-medium text-gray-700 mb-1">{{ t('profile.confirmPassword') }}</label>
            <input v-model="form.confirmPassword" type="password" class="input-field" :class="{ 'border-red-300': passwordMismatch }" />
            <p v-if="passwordMismatch" class="text-xs text-red-600 mt-1">Passwords do not match</p>
          </div>

          <div>
            <label class="block text-sm font-medium text-gray-700 mb-1">{{ t('app.language') }}</label>
            <select v-model="form.language" class="input-field">
              <option value="en">English</option>
              <option value="ru">Русский</option>
            </select>
          </div>

          <div class="pt-4 border-t">
            <div class="flex justify-end space-x-3">
              <button type="button" @click="resetForm" class="btn-secondary">{{ t('settings.reset') }}</button>
              <button type="submit" :disabled="loading" class="btn-primary flex items-center gap-2">
                <svg v-if="loading" class="animate-spin h-4 w-4 text-white" fill="none" viewBox="0 0 24 24">
                  <circle class="opacity-25" cx="12" cy="12" r="10" stroke="currentColor" stroke-width="4"></circle>
                  <path class="opacity-75" fill="currentColor" d="M4 12a8 8 0 018-8V0C5.373 0 0 5.373 0 12h4zm2 5.291A7.962 7.962 0 014 12H0c0 3.042 1.135 5.824 3 7.938l3-2.647z"></path>
                </svg>
                {{ t('settings.save') }}
              </button>
            </div>
          </div>
        </form>
      </div>
    </div>
  </AppLayout>
</template>

<script setup lang="ts">
import { ref, reactive, computed, onMounted } from 'vue';
import { useUserStore } from '@/stores/user';
import { useI18n } from 'vue-i18n';
import AppLayout from '@/components/Layout/AppLayout.vue';
import {useNotification} from "@/composables/useNotification";

const { showNotification } = useNotification();
const userStore = useUserStore();
const { t, locale } = useI18n();

const form = reactive({
  email: '',
  password: '',
  confirmPassword: '',
  language: 'en'
});

const loading = ref(false);

const userInitials = computed(() => (userStore.login[0] || 'U').toUpperCase());
const passwordMismatch = computed(() => !!form.password && form.password !== form.confirmPassword);

function resetForm() {
  form.email = userStore.email;
  form.password = '';
  form.confirmPassword = '';
  form.language = userStore.language;
}

async function saveProfile() {
  if (form.password && form.password !== form.confirmPassword) {
    showNotification('profile.passwordsMismatch', 'error');
    return;
  }

  loading.value = true;
  const updates: any = {
    email: form.email,
    language: form.language
  };
  if (form.password) updates.password = form.password;

  const ok = await userStore.updateProfile(updates);
  loading.value = false;

  if (ok) {
    locale.value = form.language;
    showNotification('profile.updateSuccess', 'success');
    resetForm();
  } else {
    showNotification('profile.updateError', 'error');
  }
}

onMounted(() => {
  form.email = userStore.email;
  form.language = userStore.language;
});
</script>
