<script setup lang="ts">
import type { Printer } from '~/composables/usePrinters'

const props = defineProps<{
  printer: Printer
  isSelected: boolean
}>()

const emit = defineEmits<{
  select: []
  edit: []
  delete: []
  setDefault: []
}>()

const menuItems = computed(() => [
  [
    {
      label: 'Editar',
      icon: 'i-heroicons-pencil-square',
      click: () => emit('edit')
    },
    {
      label: props.printer.isDefault ? 'Es predeterminada' : 'Hacer predeterminada',
      icon: 'i-heroicons-star',
      disabled: props.printer.isDefault,
      click: () => emit('setDefault')
    }
  ],
  [
    {
      label: 'Eliminar',
      icon: 'i-heroicons-trash',
      color: 'error' as const,
      click: () => emit('delete')
    }
  ]
])
</script>

<template>
  <UCard
    :class="[
      'cursor-pointer transition-all',
      isSelected ? 'ring-2 ring-primary-500' : 'hover:bg-gray-50 dark:hover:bg-gray-800'
    ]"
    @click="emit('select')"
  >
    <div class="flex items-start justify-between">
      <div class="flex-1">
        <div class="flex items-center gap-2">
          <h3 class="font-semibold text-gray-900 dark:text-white">
            {{ printer.name }}
          </h3>
          <UBadge v-if="printer.isDefault" color="primary" variant="subtle" size="xs">
            Default
          </UBadge>
          <UBadge v-if="isSelected" color="success" variant="subtle" size="xs">
            Seleccionada
          </UBadge>
        </div>

        <div class="mt-2 space-y-1 text-sm text-gray-500 dark:text-gray-400">
          <div class="flex items-center gap-2">
            <UIcon name="i-heroicons-globe-alt" class="w-4 h-4" />
            <span>{{ printer.host }}</span>
          </div>
          <div class="flex items-center gap-2">
            <UIcon name="i-heroicons-identification" class="w-4 h-4" />
            <span>{{ printer.deviceId }}</span>
          </div>
          <div class="flex items-center gap-2">
            <UIcon name="i-heroicons-clock" class="w-4 h-4" />
            <span>{{ printer.timeout }}ms timeout</span>
          </div>
        </div>
      </div>

      <UDropdownMenu :items="menuItems" @click.stop>
        <UButton
          icon="i-heroicons-ellipsis-vertical"
          color="neutral"
          variant="ghost"
          size="sm"
        />
      </UDropdownMenu>
    </div>
  </UCard>
</template>
