<script lang="ts" setup>
import { computed } from 'vue'
import type { EstimatorPlan, WorkloadEntry } from '@/stores/estimator'

interface Props {
  plan: EstimatorPlan
  workloadEntry: WorkloadEntry | null
  allPlans: EstimatorPlan[]
  cyclePrice: number
  cycleLabel: string
}

const props = defineProps<Props>()

const emit = defineEmits<{
  'change-plan': [planId: number]
}>()

const alternates = computed<EstimatorPlan[]>(() => {
  if (!props.workloadEntry) return []
  return props.workloadEntry.alternates
    .map(slug => props.allPlans.find(p => p.slug === slug))
    .filter((p): p is EstimatorPlan => p !== undefined)
})

const features = computed(() => {
  const f = props.plan.features ?? {}
  return {
    cpu: f.cpu ?? '',
    ram: f.ram ?? '',
    storage: f.storage ?? '',
    bandwidth: f.bandwidth ?? '',
  }
})
</script>

<template>
  <div class="recommended-plan">
    <div class="d-flex align-center ga-2 mb-3">
      <VChip color="primary" variant="tonal" size="small" prepend-icon="tabler-sparkles">
        Recommended
      </VChip>
      <span v-if="workloadEntry" class="text-caption text-medium-emphasis">
        for {{ workloadEntry.label.toLowerCase() }}
      </span>
    </div>

    <div class="recommended-plan__main d-flex align-center flex-wrap ga-4">
      <div class="flex-grow-1 min-w-0">
        <div class="d-flex align-center ga-2 mb-2 flex-wrap">
          <h3 class="text-h5 font-weight-bold mb-0">{{ plan.name }}</h3>
          <VMenu v-if="alternates.length > 0">
            <template #activator="{ props: menuProps }">
              <VBtn
                size="small"
                variant="text"
                v-bind="menuProps"
                class="text-caption"
              >
                or pick another
                <VIcon icon="tabler-chevron-down" end size="small" />
              </VBtn>
            </template>
            <VList density="compact">
              <VListItem
                v-for="alt in alternates"
                :key="alt.id"
                @click="emit('change-plan', alt.id)"
              >
                <VListItemTitle class="text-body-2">{{ alt.name }}</VListItemTitle>
                <VListItemSubtitle class="text-caption">
                  {{ alt.features?.cpu }} · {{ alt.features?.ram }} · ${{ alt.price }}/mo
                </VListItemSubtitle>
              </VListItem>
              <VDivider class="my-1" />
              <VListItem
                v-for="alt in allPlans.filter(p => p.id !== plan.id && !alternates.find(a => a.id === p.id))"
                :key="alt.id"
                @click="emit('change-plan', alt.id)"
              >
                <VListItemTitle class="text-body-2 text-medium-emphasis">{{ alt.name }}</VListItemTitle>
                <VListItemSubtitle class="text-caption">
                  {{ alt.features?.cpu }} · {{ alt.features?.ram }} · ${{ alt.price }}/mo
                </VListItemSubtitle>
              </VListItem>
            </VList>
          </VMenu>
        </div>

        <div class="recommended-plan__specs d-flex flex-wrap ga-3">
          <div v-if="features.cpu" class="recommended-plan__spec">
            <VIcon icon="tabler-cpu" size="16" />
            <span>{{ features.cpu }}</span>
          </div>
          <div v-if="features.ram" class="recommended-plan__spec">
            <VIcon icon="tabler-device-sd-card" size="16" />
            <span>{{ features.ram }} RAM</span>
          </div>
          <div v-if="features.storage" class="recommended-plan__spec">
            <VIcon icon="tabler-database" size="16" />
            <span>{{ features.storage }}</span>
          </div>
          <div v-if="features.bandwidth" class="recommended-plan__spec">
            <VIcon icon="tabler-arrows-right-left" size="16" />
            <span>{{ features.bandwidth }}</span>
          </div>
        </div>
      </div>

      <div class="recommended-plan__price flex-shrink-0">
        <div class="text-h4 font-weight-bold text-primary">
          ${{ cyclePrice.toFixed(2) }}
        </div>
        <div class="text-caption text-medium-emphasis">
          {{ cycleLabel }} · base plan
        </div>
      </div>
    </div>
  </div>
</template>

<style lang="scss" scoped>
.recommended-plan {
  padding: 24px;
  border-radius: 16px;
  background: rgba(var(--v-theme-surface), 0.6);
  backdrop-filter: blur(20px);
  -webkit-backdrop-filter: blur(20px);
  border: 1px solid rgba(var(--v-theme-primary), 0.22);
}

.recommended-plan__spec {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 6px 12px;
  border-radius: 999px;
  background: rgba(var(--v-theme-on-surface), 0.06);
  border: 1px solid rgba(var(--v-theme-on-surface), 0.06);
  font-size: 13px;
  color: rgba(var(--v-theme-on-surface), 0.85);
}

.recommended-plan__price {
  text-align: end;

  @media (max-width: 600px) {
    text-align: start;
    width: 100%;
  }
}
</style>
