<template>
  <div
    class="ticket-card rounded-xl border p-6 transition-all duration-200"
    :class="[
      isSelected
        ? 'border-primary bg-primary/5'
        : 'border-guild-600 bg-guild-800/50',
      isAvailable && !alreadyRegistered
        ? 'hover:border-primary/50 cursor-pointer'
        : 'opacity-60 cursor-not-allowed',
    ]"
    @click="handleClick"
  >
    <!-- Ticket Header -->
    <div class="flex items-start justify-between mb-4">
      <div>
        <h3 class="text-lg font-semibold text-guild-100">
          {{ ticketInfo.name }}
        </h3>
        <p v-if="ticketInfo.description" class="text-sm text-guild-300 mt-1">
          {{ ticketInfo.description }}
        </p>
      </div>

      <!-- Badge -->
      <div v-if="ticketInfo.isMember" class="flex-shrink-0 ml-4">
        <span
          class="inline-flex items-center px-3 py-1 rounded-full text-xs font-medium bg-candlelight-900/20 text-candlelight-500 dark:bg-candlelight-900/30 dark:text-candlelight-400"
        >
          Members Only
        </span>
      </div>
    </div>

    <!-- Price Display -->
    <div class="mb-4">
      <div class="flex items-baseline gap-2">
        <span
          class="text-3xl font-bold text-ui-mono"
          :class="ticketInfo.isFree ? 'text-candlelight-400' : 'text-guild-100'"
        >
          {{ ticketInfo.formattedPrice }}
        </span>

        <!-- Early Bird Badge -->
        <span
          v-if="ticketInfo.isEarlyBird"
          class="inline-flex items-center px-2 py-1 rounded-full text-xs font-medium bg-candlelight-900/20 text-candlelight-600 dark:bg-candlelight-900/35 dark:text-candlelight-400"
        >
          Early Bird
        </span>
      </div>

      <!-- Regular Price (if early bird) -->
      <div
        v-if="ticketInfo.isEarlyBird && ticketInfo.formattedRegularPrice"
        class="mt-1"
      >
        <span class="text-sm text-guild-400 line-through">
          Regular: {{ ticketInfo.formattedRegularPrice }}
        </span>
      </div>

      <!-- Early Bird Countdown -->
      <div
        v-if="ticketInfo.isEarlyBird && ticketInfo.earlyBirdDeadline"
        class="mt-2 text-xs text-candlelight-500 dark:text-candlelight-400"
      >
        <Icon name="heroicons:clock" class="w-4 h-4 inline mr-1" />
        Early bird ends {{ formatDeadline(ticketInfo.earlyBirdDeadline) }}
      </div>
    </div>

    <!-- Member Savings -->
    <div
      v-if="ticketInfo.publicTicket && ticketInfo.memberSavings > 0"
      class="mb-4 p-3 bg-candlelight-900/20 rounded-lg border border-candlelight-800"
    >
      <p class="text-sm text-candlelight-400">
        <Icon name="heroicons:check-circle" class="w-4 h-4 inline mr-1" />
        You save {{ formatPrice(ticketInfo.memberSavings) }} as a member!
      </p>
      <p class="text-xs text-guild-400 mt-1">
        Public price: {{ ticketInfo.publicTicket.formattedPrice }}
      </p>
    </div>

    <!-- Availability -->
    <div class="flex items-center justify-between text-sm">
      <div>
        <span
          v-if="alreadyRegistered"
          class="text-candlelight-400 flex items-center gap-1"
        >
          <Icon name="heroicons:check-circle-solid" class="w-4 h-4" />
          You're registered
        </span>
        <span
          v-else-if="!isAvailable"
          class="text-ember-400 flex items-center gap-1"
        >
          <Icon name="heroicons:x-circle-solid" class="w-4 h-4" />
          Sold Out
        </span>
        <span v-else-if="ticketInfo.remaining !== null" class="text-guild-300">
          {{ ticketInfo.remaining }} remaining
        </span>
        <span v-else class="text-guild-300"> Unlimited availability </span>
      </div>

      <!-- Selection Indicator -->
      <div v-if="isSelected && isAvailable && !alreadyRegistered">
        <Icon name="heroicons:check-circle-solid" class="w-5 h-5 text-primary" />
      </div>
    </div>

    <!-- Waitlist Option -->
    <div
      v-if="!isAvailable && ticketInfo.waitlistAvailable && !alreadyRegistered"
      class="mt-4 pt-4 border-t border-guild-600"
    >
      <UButton
        color="gray"
        size="sm"
        block
        @click.stop="$emit('join-waitlist')"
      >
        Join Waitlist
      </UButton>
    </div>
  </div>
</template>

<script setup>
const props = defineProps({
  ticketInfo: {
    type: Object,
    required: true,
  },
  isSelected: {
    type: Boolean,
    default: false,
  },
  isAvailable: {
    type: Boolean,
    default: true,
  },
  alreadyRegistered: {
    type: Boolean,
    default: false,
  },
});

const emit = defineEmits(["select", "join-waitlist"]);

const handleClick = () => {
  if (props.isAvailable && !props.alreadyRegistered) {
    emit("select");
  }
};

const formatDeadline = (deadline) => {
  const date = new Date(deadline);
  const now = new Date();
  const diff = date - now;

  // If less than 24 hours, show hours
  if (diff < 24 * 60 * 60 * 1000) {
    const hours = Math.floor(diff / (60 * 60 * 1000));
    return `in ${hours} hour${hours !== 1 ? "s" : ""}`;
  }

  // Otherwise show date
  return `on ${date.toLocaleDateString("en-US", {
    month: "short",
    day: "numeric",
  })}`;
};

const formatPrice = (amount) => {
  return new Intl.NumberFormat("en-CA", {
    style: "currency",
    currency: "CAD",
  }).format(amount);
};
</script>

<style scoped>
.ticket-card {
  position: relative;
}
</style>
