<script lang="ts" setup>
import { router } from '@inertiajs/vue3'
import { computed } from 'vue'
import AdminLayout from '@/Layouts/AdminLayout.vue'
import type { AffiliatePayout, PaginatedResponse, StatusColor } from '@/types'

interface Props {
  payouts: PaginatedResponse<AffiliatePayout>
}

defineOptions({ layout: AdminLayout })

const props = defineProps<Props>()

function resolveStatusColor(status: string): StatusColor {
  const map: Record<string, StatusColor> = {
    pending: 'warning',
    processing: 'info',
    completed: 'success',
    failed: 'error',
  }
  return map[status] ?? 'secondary'
}

function formatMethod(method: string): string {
  const map: Record<string, string> = {
    credit: 'Account Credit',
    paypal: 'PayPal',
    bank_transfer: 'Bank Transfer',
  }
  return map[method] ?? method
}

function formatDate(dateStr: string): string {
  return new Date(dateStr).toLocaleDateString('en-US', {
    year: 'numeric',
    month: 'short',
    day: 'numeric',
  })
}

const tableHeaders = computed(() => [
  { title: 'Affiliate', key: 'affiliate', sortable: false },
  { title: 'Amount', key: 'amount', sortable: true },
  { title: 'Method', key: 'method', sortable: true },
  { title: 'Status', key: 'status', sortable: true, align: 'center' as const },
  { title: 'Date', key: 'created_at', sortable: true },
  { title: 'Actions', key: 'actions', sortable: false, align: 'center' as const },
])

function processPayout(id: number): void {
  if (confirm('Are you sure you want to process this payout?')) {
    router.post(`/affiliate-payouts/${id}/process`, {}, { preserveScroll: true })
  }
}
</script>

<template>
  <div>
    <div class="text-h4 font-weight-bold mb-2">
      Payout Processing Queue
    </div>
    <div class="text-body-2 text-medium-emphasis mb-6">
      Review and process pending affiliate payouts
    </div>

    <VCard>
      <VDataTable
        :headers="tableHeaders"
        :items="payouts.data"
        :items-per-page="25"
        hover
        class="text-no-wrap"
      >
        <template #item.affiliate="{ item }">
          <div v-if="item.affiliate?.user">
            <div class="font-weight-medium">
              {{ item.affiliate.user.name }}
            </div>
            <div class="text-body-2 text-medium-emphasis">
              {{ item.affiliate.user.email }}
            </div>
          </div>
        </template>

        <template #item.amount="{ item }">
          <span class="font-weight-medium">${{ parseFloat(item.amount).toFixed(2) }}</span>
          <span class="text-medium-emphasis ms-1">{{ item.currency }}</span>
        </template>

        <template #item.method="{ item }">
          {{ formatMethod(item.method) }}
        </template>

        <template #item.status="{ item }">
          <VChip
            :color="resolveStatusColor(item.status)"
            size="small"
            class="text-capitalize"
          >
            {{ item.status }}
          </VChip>
        </template>

        <template #item.created_at="{ item }">
          {{ formatDate(item.created_at) }}
        </template>

        <template #item.actions="{ item }">
          <VBtn
            color="primary"
            size="small"
            variant="tonal"
            prepend-icon="tabler-check"
            @click="processPayout(item.id)"
          >
            Process
          </VBtn>
        </template>

        <template #no-data>
          <div class="text-center py-8">
            <VIcon icon="tabler-cash-banknote" size="48" color="disabled" class="mb-2" />
            <div class="text-medium-emphasis">
              No pending payouts to process.
            </div>
          </div>
        </template>
      </VDataTable>
    </VCard>
  </div>
</template>
