<script setup lang="ts">
definePageMeta({ ssr: false })
usePageTitle('Colectivos · Cartera')

const { data, pending } = usePolicy('/policies', {
  query: {
    'page_size': 100
  }
})

const policies = computed(() => data.value?.data ?? [])

function fmtCurrency(n: number) {
  return new Intl.NumberFormat('en-US', { style: 'currency', currency: 'USD', minimumFractionDigits: 0, maximumFractionDigits: 0 }).format(n)
}

function fmtDate(d: string) {
  if (!d) return '—'
  return new Date(d).toLocaleDateString('en-US', { month: 'short', day: 'numeric', year: 'numeric' })
}

function policyApplicantName(p: any) {
  const info = p.insured
  if (!info || typeof info !== 'object') return '—'
  if (info.type === 'corporate') {
    return info.company_name || '—'
  }
  return info.name || '—'
}

function policyDetailsSummary(p: any) {
  const d = p.insured_object
  if (!d || typeof d !== 'object') return '—'
  if (p.policy_type === 'car') {
    const parts = [d.year, d.make, d.model].filter((x: any) => x !== undefined && x !== null && String(x) !== '')
    return parts.length ? parts.map(String).join(' ') : '—'
  }
  if (p.policy_type === 'life') {
    return `Life · ${d.coverage_amount || 0} USD`
  }
  if (p.policy_type === 'fire_structure' || p.policy_type === 'fire_contents') {
    return d.location || '—'
  }
  return '—'
}

function statusColor(status: string) {
  switch (status) {
    case 'quote_requested': return 'yellow'
    case 'quotes_received': return 'blue'
    case 'awaiting_policy': return 'purple'
    case 'issued': return 'green'
    default: return 'gray'
  }
}

function statusLabel(status: string) {
  switch (status) {
    case 'quote_requested': return 'Quote Requested'
    case 'quotes_received': return 'Quotes Received'
    case 'awaiting_policy': return 'Awaiting Policy'
    case 'issued': return 'Issued'
    default: return status
  }
}
</script>

<template>
  <div class="gc-page">
    <div class="gc-header">
      <div class="gc-header-left">
        <h1 class="gc-title">Colectivos</h1>
        <span class="gc-count-badge">{{ policies.length }}</span>
      </div>
      <div class="gc-header-right">
        <NuxtLink to="/policies" class="gc-header-link">
          <UIcon name="i-heroicons-arrow-left" class="gc-header-link-icon" />
          All Policies
        </NuxtLink>
      </div>
    </div>

    <div v-if="pending" class="gc-card">
      <div style="padding: 20px;">
        <div v-for="n in 8" :key="n" class="pol-skeleton-row">
          <div class="pol-skeleton" style="width: 80px; height: 12px;" />
          <div class="pol-skeleton" style="width: 140px; height: 12px;" />
          <div class="pol-skeleton" style="width: 50px; height: 18px; border-radius: 10px;" />
          <div class="pol-skeleton" style="width: 100px; height: 12px;" />
          <div class="pol-skeleton" style="width: 100px; height: 12px;" />
          <div class="pol-skeleton" style="width: 70px; height: 12px;" />
          <div class="pol-skeleton" style="width: 50px; height: 18px; border-radius: 10px;" />
          <div class="pol-skeleton" style="width: 80px; height: 12px;" />
          <div class="pol-skeleton" style="width: 60px; height: 12px;" />
        </div>
      </div>
    </div>

    <template v-else>
      <div class="gc-table-wrap">
        <table class="gc-table">
          <thead>
            <tr>
              <th class="gc-th gc-th--left">Applicant</th>
              <th class="gc-th gc-th--left">Type</th>
              <th class="gc-th gc-th--left">Details</th>
              <th class="gc-th gc-th--right">Premium</th>
              <th class="gc-th gc-th--left">Status</th>
              <th class="gc-th gc-th--left">Submitted</th>
            </tr>
          </thead>
          <tbody>
            <tr v-for="policy in policies" :key="policy.application_id" class="gc-row">
              <td class="gc-td">
                <div class="gc-group-cell">
                  <span class="gc-avatar">{{ policyApplicantName(policy)[0] }}</span>
                  <div>
                    <NuxtLink :to="`/policies/app/${policy.application_id}`" class="gc-group-link">
                      {{ policyApplicantName(policy) }}
                    </NuxtLink>
                  </div>
                </div>
              </td>
              <td class="gc-td">
                <span class="gc-lob-pill gc-lob--neutral capitalize">{{ policy.policy_type }}</span>
              </td>
              <td class="gc-td">{{ policyDetailsSummary(policy) }}</td>
              <td class="gc-td gc-td--num gc-td--premium">{{ fmtCurrency(policy.premium || 0) }}</td>
              <td class="gc-td">
                <span class="gc-status-pill" :class="`gc-status--${statusColor(policy.status)}`">
                  {{ statusLabel(policy.status) }}
                </span>
              </td>
              <td class="gc-td">{{ fmtDate(policy.submitted_at) }}</td>
            </tr>
            <tr v-if="policies.length === 0">
              <td colspan="6" class="gc-empty">No policies found</td>
            </tr>
          </tbody>
        </table>
      </div>

      <div class="gc-bottom-bar">
        <div class="gc-bottom-inner">
          <span class="gc-bottom-item">
            <UIcon name="i-heroicons-table-cells" class="gc-bottom-icon" />
            {{ policies.length }} polic{{ policies.length !== 1 ? 'ies' : 'y' }}
          </span>
        </div>
      </div>
    </template>
  </div>
</template>

<style scoped>
.gc-page {
  display: flex;
  flex-direction: column;
  gap: 16px;
  padding-bottom: 48px;
  max-width: 80rem;
  margin: 0 auto;
}

.gc-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
}
.gc-header-left {
  display: flex;
  align-items: center;
  gap: 10px;
}
.gc-header-right {
  display: flex;
  align-items: center;
  gap: 8px;
}
.gc-count-badge {
  font-size: 11px;
  font-weight: 700;
  color: #01696f;
  background: rgba(1,105, 111, 0.08);
  padding: 2px 9px;
  border-radius: 10px;
}

.gc-title {
  font-size: 24px;
  font-weight: 600;
  letter-spacing: -0.01em;
  color: var(--text-primary);
  line-height: 1;
}

.gc-header-link {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  font-size: 13px;
  font-weight: 500;
  color: #01696f;
  text-decoration: none;
  white-space: nowrap;
  transition: all 150ms ease;
}

.gc-header-link:hover { text-decoration: underline; }

.gc-header-link-icon {
  width: 14px;
  height: 14px;
}

.gc-card {
  background: #ffffff;
  border-radius: 12px;
  border: 1px solid rgba(0, 0, 0, 0.06);
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.03);
}

.gc-table-wrap {
  background: #fff;
  border: 1px solid rgba(0, 0, 0, 0.06);
  border-radius: 12px;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.03);
  overflow-x: auto;
}

.gc-table {
  width: 100%;
  font-size: 13px;
  border-collapse: collapse;
}

.gc-th {
  padding: 10px 14px;
  font-size: 11px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: #8a8a86;
  border-bottom: 1px solid rgba(0, 0, 0, 0.06);
  background: rgba(0, 0, 0, 0.015);
  white-space: nowrap;
}

.gc-th--left { text-align: left; }
.gc-th--right { text-align: right; }

.gc-row {
  transition: all 150ms ease;
}

.gc-row:hover {
  background: rgba(1, 105, 111, 0.03);
}

.gc-row:not(:last-child) .gc-td {
  border-bottom: 1px solid rgba(0, 0, 0, 0.04);
}

.gc-td {
  padding: 10px 14px;
  vertical-align: middle;
  color: var(--text-primary);
}

.gc-td--num {
  text-align: right;
  font-variant-numeric: tabular-nums;
}

.gc-td--premium {
  font-weight: 600;
}

.gc-group-cell {
  display: flex;
  align-items: center;
  gap: 10px;
}

.gc-avatar {
  flex-shrink: 0;
  width: 32px;
  height: 32px;
  border-radius: 8px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 11px;
  font-weight: 700;
  letter-spacing: 0.02em;
  line-height: 1;
  background: rgba(1, 105, 111, 0.08);
  color: #01696f;
}

.gc-group-link {
  font-weight: 600;
  color: #01696f;
  text-decoration: none;
  display: block;
  line-height: 1.3;
  transition: all 150ms ease;
}

.gc-group-link:hover {
  text-decoration: underline;
  text-underline-offset: 2px;
}

.gc-lob-pill {
  display: inline-block;
  font-size: 9px;
  font-weight: 700;
  padding: 1px 5px;
  border-radius: 9999px;
  white-space: nowrap;
  line-height: 1.6;
}

.gc-lob--neutral {
  color: #8a8a86;
  background: rgba(0, 0, 0, 0.06);
}

.gc-status-pill {
  display: inline-block;
  font-size: 9px;
  font-weight: 700;
  padding: 1px 5px;
  border-radius: 9999px;
  white-space: nowrap;
  line-height: 1.6;
}

.gc-status--success {
  color: #16a34a;
  background: rgba(22, 163, 74, 0.1);
}

.gc-status--info {
  color: #3b82f6;
  background: rgba(59, 130, 246, 0.1);
}

.gc-status--warning {
  color: #ca8a04;
  background: rgba(202, 138, 4, 0.1);
}

.gc-status--error {
  color: #dc2626;
  background: rgba(220, 38, 38, 0.1);
}

.gc-status--neutral {
  color: #8a8a86;
  background: rgba(0, 0, 0, 0.06);
}

.gc-empty {
  padding: 40px 14px;
  text-align: center;
  color: #8a8a86;
  font-size: 14px;
}

.gc-bottom-bar {
  background: #ffffff;
  border: 1px solid rgba(0, 0, 0, 0.06);
  border-radius: 12px;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.03);
  padding: 14px 20px;
}

.gc-bottom-inner {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 10px;
}

.gc-bottom-item {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  font-size: 13px;
  color: var(--text-muted);
}

.gc-bottom-item strong {
  color: var(--text-primary);
  font-weight: 600;
  font-variant-numeric: tabular-nums;
}

.gc-bottom-icon {
  width: 14px;
  height: 14px;
  color: #8a8a86;
}

.pol-skeleton-row {
  display: flex;
  align-items: center;
  gap: 16px;
  padding: 10px 0;
  border-bottom: 1px solid rgba(0, 0, 0, 0.03);
}
.pol-skeleton-row:last-child { border-bottom: none; }
.pol-skeleton {
  height: 12px;
  border-radius: 4px;
  background: rgba(0, 0, 0, 0.05);
  animation: pol-pulse 1.5s ease-in-out infinite;
}
@keyframes pol-pulse {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.4; }
}
</style>
