<script setup lang="ts">
import { ref, onMounted } from 'vue'
import { useI18n } from 'vue-i18n'
import { useAuthStore } from '@/stores/auth'
import { api } from '@/api'

const { t } = useI18n()
const authStore = useAuthStore()

interface User {
  id: string
  email: string
  role: string
  first_name: string
  last_name: string
  phone?: string
  active: boolean
}

const users = ref<User[]>([])
const loading = ref(true)
const showCreateModal = ref(false)
const newUser = ref({
  email: '',
  password: '',
  first_name: '',
  last_name: '',
  phone: '',
  role: 'mitarbeiter' as const
})

onMounted(async () => {
  await loadUsers()
})

async function loadUsers() {
  loading.value = true
  try {
    const res = await api.get<{ users: User[] }>('/users')
    users.value = res.data.users
  } catch (e) {
    console.error(e)
  } finally {
    loading.value = false
  }
}

async function createUser() {
  try {
    await api.post('/users', newUser.value)
    showCreateModal.value = false
    newUser.value = { email: '', password: '', first_name: '', last_name: '', phone: '', role: 'mitarbeiter' }
    await loadUsers()
  } catch (e) {
    alert(e instanceof Error ? e.message : t('messages.error'))
  }
}

async function toggleActive(user: User) {
  try {
    if (user.active) {
      await api.delete(`/users/${user.id}`)
    } else {
      await api.put(`/users/${user.id}`, { active: true })
    }
    await loadUsers()
  } catch (e) {
    alert(e instanceof Error ? e.message : t('messages.error'))
  }
}

function getRoleBadge(role: string) {
  const badges: Record<string, string> = {
    chef: 'badge-danger',
    disponent: 'badge-primary',
    mitarbeiter: 'badge-success'
  }
  return badges[role] || 'badge-secondary'
}

function getRoleLabel(role: string) {
  return t(`users.roles.${role}`) || role
}
</script>

<template>
  <div class="space-y-6">
    <div class="flex items-center justify-between">
      <h1 class="text-2xl font-bold text-gray-900 dark:text-white">👥 {{ t('users.title') }}</h1>
      <button class="btn btn-primary" @click="showCreateModal = true">+ {{ t('users.new') }}</button>
    </div>

    <div class="card">
      <div v-if="loading" class="text-center py-8 text-gray-500">{{ t('app.loading') }}</div>
      <div v-else-if="users.length === 0" class="text-center py-8 text-gray-500">{{ t('messages.noData') }}</div>
      
      <table v-else class="w-full">
        <thead>
          <tr class="text-left text-sm text-gray-500 border-b border-gray-200 dark:border-gray-700">
            <th class="pb-3">{{ t('auth.firstName') }} {{ t('auth.lastName') }}</th>
            <th class="pb-3">{{ t('auth.email') }}</th>
            <th class="pb-3">{{ t('users.role') }}</th>
            <th class="pb-3">{{ t('app.status') }}</th>
            <th class="pb-3"></th>
          </tr>
        </thead>
        <tbody>
          <tr v-for="user in users" :key="user.id" class="border-b border-gray-100 dark:border-gray-800">
            <td class="py-3 font-medium">{{ user.first_name }} {{ user.last_name }}</td>
            <td class="py-3 text-gray-500">{{ user.email }}</td>
            <td class="py-3"><span :class="['badge', getRoleBadge(user.role)]">{{ getRoleLabel(user.role) }}</span></td>
            <td class="py-3"><span :class="user.active ? 'text-green-600' : 'text-red-600'">{{ user.active ? t('users.active') : t('users.inactive') }}</span></td>
            <td class="py-3 text-right">
              <button 
                v-if="user.id !== authStore.user?.id && user.role !== 'chef'"
                class="text-sm text-gray-500 hover:text-red-600"
                @click="toggleActive(user)"
              >
                {{ user.active ? t('modules.disable') : t('modules.enable') }}
              </button>
            </td>
          </tr>
        </tbody>
      </table>
    </div>

    <!-- Create Modal -->
    <div v-if="showCreateModal" class="fixed inset-0 z-50 flex items-center justify-center bg-black/50">
      <div class="card w-full max-w-md m-4">
        <h2 class="text-xl font-semibold mb-6">{{ t('users.new') }}</h2>
        <form @submit.prevent="createUser" class="space-y-4">
          <div class="grid grid-cols-2 gap-4">
            <div>
              <label class="block text-sm font-medium mb-1">{{ t('auth.firstName') }} *</label>
              <input v-model="newUser.first_name" type="text" required class="input" />
            </div>
            <div>
              <label class="block text-sm font-medium mb-1">{{ t('auth.lastName') }} *</label>
              <input v-model="newUser.last_name" type="text" required class="input" />
            </div>
          </div>
          <div>
            <label class="block text-sm font-medium mb-1">{{ t('auth.email') }} *</label>
            <input v-model="newUser.email" type="email" required class="input" />
          </div>
          <div>
            <label class="block text-sm font-medium mb-1">{{ t('auth.password') }} *</label>
            <input v-model="newUser.password" type="password" required class="input" />
          </div>
          <div>
            <label class="block text-sm font-medium mb-1">{{ t('users.phone') }}</label>
            <input v-model="newUser.phone" type="tel" class="input" />
          </div>
          <div v-if="authStore.isChef">
            <label class="block text-sm font-medium mb-1">{{ t('users.role') }}</label>
            <select v-model="newUser.role" class="input">
              <option value="mitarbeiter">{{ t('users.roles.mitarbeiter') }}</option>
              <option value="disponent">{{ t('users.roles.disponent') }}</option>
            </select>
          </div>
          <div class="flex justify-end gap-3 pt-4">
            <button type="button" class="btn btn-secondary" @click="showCreateModal = false">{{ t('app.cancel') }}</button>
            <button type="submit" class="btn btn-primary">{{ t('app.add') }}</button>
          </div>
        </form>
      </div>
    </div>
  </div>
</template>
