<template>
  <div class="offer-card" :class="offerClass">
    <div class="offer-header">
      <span class="offer-type">{{ offerTypeText }}</span>
      <span class="offer-status" :class="'status-' + offer.status">
        {{ statusText }}
      </span>
    </div>
    
    <div class="offer-content">
      <div class="offer-section">
        <h5>Ofrece:</h5>
        <div class="tokens">
          <span v-if="offer.offering.turkey > 0">🦃 {{ offer.offering.turkey }}</span>
          <span v-if="offer.offering.coffee > 0">☕ {{ offer.offering.coffee }}</span>
          <span v-if="offer.offering.corn > 0">🌽 {{ offer.offering.corn }}</span>
          <span v-if="totalOffering === 0">Nada</span>
        </div>
      </div>
      
      <div class="exchange-icon">⇄</div>
      
      <div class="offer-section">
        <h5>Solicita:</h5>
        <div class="tokens">
          <span v-if="offer.requesting.turkey > 0">🦃 {{ offer.requesting.turkey }}</span>
          <span v-if="offer.requesting.coffee > 0">☕ {{ offer.requesting.coffee }}</span>
          <span v-if="offer.requesting.corn > 0">🌽 {{ offer.requesting.corn }}</span>
          <span v-if="totalRequesting === 0">Nada</span>
        </div>
      </div>
    </div>
    
    <div v-if="offer.status === 'pending'" class="offer-actions">
      <template v-if="isMyOffer">
        <button @click="$emit('cancel', offer.id)" class="btn-cancel">
          Cancelar
        </button>
      </template>
      <template v-else>
        <button @click="$emit('respond', offer.id, 'accept')" class="btn-accept">
          Aceptar
        </button>
        <button @click="$emit('respond', offer.id, 'reject')" class="btn-reject">
          Rechazar
        </button>
        <button @click="$emit('respond', offer.id, 'snatch')" class="btn-snatch">
          Robar
        </button>
      </template>
    </div>
  </div>
</template>

<script setup lang="ts">
import { computed } from 'vue'

const props = defineProps<{
  offer: {
    id: string
    offererId: string
    targetId: string
    offering: {
      turkey: number
      coffee: number
      corn: number
    }
    requesting: {
      turkey: number
      coffee: number
      corn: number
    }
    status: string
  }
  currentPlayerId: string
}>()

const emit = defineEmits<{
  'respond': [offerId: string, response: string]
  'cancel': [offerId: string]
}>()

const isMyOffer = computed(() => props.offer.offererId === props.currentPlayerId)
const isTargetedAtMe = computed(() => props.offer.targetId === props.currentPlayerId)

const offerClass = computed(() => ({
  'my-offer': isMyOffer.value,
  'targeted-at-me': isTargetedAtMe.value,
  'status-pending': props.offer.status === 'pending',
  'status-accepted': props.offer.status === 'accepted',
  'status-rejected': props.offer.status === 'rejected',
  'status-snatched': props.offer.status === 'snatched',
  'status-cancelled': props.offer.status === 'cancelled'
}))

const offerTypeText = computed(() => {
  if (isMyOffer.value) return 'Tu oferta'
  if (isTargetedAtMe.value) return 'Oferta para ti'
  return 'Oferta'
})

const statusText = computed(() => {
  switch (props.offer.status) {
    case 'pending': return 'Pendiente'
    case 'accepted': return 'Aceptada'
    case 'rejected': return 'Rechazada'
    case 'snatched': return '¡Robada!'
    case 'cancelled': return 'Cancelada'
    default: return props.offer.status
  }
})

const totalOffering = computed(() => 
  props.offer.offering.turkey + props.offer.offering.coffee + props.offer.offering.corn
)

const totalRequesting = computed(() => 
  props.offer.requesting.turkey + props.offer.requesting.coffee + props.offer.requesting.corn
)
</script>

<style scoped>
.offer-card {
  background: rgba(255, 255, 255, 0.1);
  border-radius: 12px;
  padding: 1.25rem;
  margin-bottom: 1rem;
  backdrop-filter: blur(10px);
  border: 2px solid rgba(255, 255, 255, 0.2);
  transition: all 0.3s ease;
}

.offer-card.my-offer {
  border-color: rgba(103, 126, 234, 0.5);
  background: rgba(103, 126, 234, 0.1);
}

.offer-card.targeted-at-me {
  border-color: rgba(255, 215, 0, 0.5);
  background: rgba(255, 215, 0, 0.1);
}

.offer-card.status-accepted {
  border-color: rgba(76, 175, 80, 0.5);
  background: rgba(76, 175, 80, 0.1);
}

.offer-card.status-rejected {
  border-color: rgba(244, 67, 54, 0.5);
  background: rgba(244, 67, 54, 0.1);
}

.offer-card.status-snatched {
  border-color: rgba(255, 107, 107, 0.7);
  background: rgba(255, 107, 107, 0.2);
}

.offer-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 1rem;
}

.offer-type {
  font-weight: 600;
  font-size: 1.1rem;
}

.offer-status {
  padding: 0.25rem 0.75rem;
  border-radius: 6px;
  font-size: 0.9rem;
  font-weight: 500;
}

.status-pending {
  background: rgba(255, 193, 7, 0.2);
  color: #ffc107;
}

.status-accepted {
  background: rgba(76, 175, 80, 0.2);
  color: #4caf50;
}

.status-rejected {
  background: rgba(244, 67, 54, 0.2);
  color: #f44336;
}

.status-snatched {
  background: rgba(255, 107, 107, 0.3);
  color: #ff6b6b;
}

.status-cancelled {
  background: rgba(158, 158, 158, 0.2);
  color: #9e9e9e;
}

.offer-content {
  display: flex;
  align-items: center;
  gap: 1rem;
  margin-bottom: 1rem;
  padding: 1rem;
  background: rgba(0, 0, 0, 0.2);
  border-radius: 8px;
}

.offer-section {
  flex: 1;
}

.offer-section h5 {
  margin: 0 0 0.5rem 0;
  font-size: 0.9rem;
  opacity: 0.8;
}

.tokens {
  display: flex;
  gap: 0.75rem;
  font-size: 1.1rem;
  font-weight: 500;
}

.exchange-icon {
  font-size: 1.5rem;
  opacity: 0.5;
}

.offer-actions {
  display: flex;
  gap: 0.5rem;
}

button {
  flex: 1;
  padding: 0.5rem 1rem;
  border: none;
  border-radius: 6px;
  font-size: 0.9rem;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.3s ease;
  color: white;
}

.btn-accept {
  background: rgba(76, 175, 80, 0.8);
}

.btn-accept:hover {
  background: rgba(76, 175, 80, 1);
  transform: translateY(-1px);
  box-shadow: 0 3px 10px rgba(0, 0, 0, 0.3);
}

.btn-reject {
  background: rgba(244, 67, 54, 0.8);
}

.btn-reject:hover {
  background: rgba(244, 67, 54, 1);
  transform: translateY(-1px);
  box-shadow: 0 3px 10px rgba(0, 0, 0, 0.3);
}

.btn-snatch {
  background: rgba(255, 107, 107, 0.8);
}

.btn-snatch:hover {
  background: rgba(255, 107, 107, 1);
  transform: translateY(-1px);
  box-shadow: 0 3px 10px rgba(0, 0, 0, 0.3);
}

.btn-cancel {
  background: rgba(158, 158, 158, 0.8);
}

.btn-cancel:hover {
  background: rgba(158, 158, 158, 1);
  transform: translateY(-1px);
  box-shadow: 0 3px 10px rgba(0, 0, 0, 0.3);
}
</style>