<template>
  <div class="admin-reports">
    <div class="header">
      <button class="back-link" @click="$router.push('/admin')">
        <span class="material-icons">arrow_back</span> Volver al Panel
      </button>
      <h1>Reportes de Usuarios</h1>
    </div>

    <div v-if="isLoading" class="loading">
      <LoadingBranded message="Cargando reportes..." icon="assignment" />
    </div>

    <div v-else-if="reports.length > 0" class="reports-container">
      <div class="stats-overview">
        <div class="stat-card">
          <span class="stat-value">{{ reports.length }}</span>
          <span class="stat-label">Total Reportes</span>
        </div>
        <div class="stat-card pending">
          <span class="stat-value">{{ reports.filter(r => r.status === 'pending').length }}</span>
          <span class="stat-label">Pendientes</span>
        </div>
      </div>

      <div class="reports-grid">
        <div v-for="report in sortedReports" :key="report.id" class="report-card" :class="report.status">
          <div class="report-header">
            <div class="user-info">
              <span class="material-icons">account_circle</span>
              <div>
                <h3>{{ report.user_name || 'Usuario Anónimo' }}</h3>
                <span class="date">{{ formatDate(report.created_at) }}</span>
              </div>
            </div>
            <div class="status-badge" :class="report.status">
              {{ statusDisplay(report.status) }}
            </div>
          </div>
          
          <div class="report-body">
            <p>{{ report.message }}</p>
          </div>

          <div class="report-actions">
            <button 
              v-if="report.status === 'pending'"
              @click="handleUpdateStatus(report.id, 'resolved')"
              class="btn-resolve"
            >
              <span class="material-icons">check_circle</span> Marcar como Resuelto
            </button>
            <button 
              v-else-if="report.status === 'resolved'"
              @click="handleUpdateStatus(report.id, 'archived')"
              class="btn-archive"
            >
              <span class="material-icons">archive</span> Archivar
            </button>
          </div>
        </div>
      </div>
    </div>

    <div v-else class="no-results">
      <span class="material-icons">info</span>
      <p>No hay reportes nuevos en este momento.</p>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted, computed } from 'vue'
import { reportsService, type Report } from '@/services/reportsService'
import LoadingBranded from '@/components/common/LoadingBranded.vue'

const reports = ref<Report[]>([])
const isLoading = ref(true)

const sortedReports = computed(() => {
  return [...reports.value].sort((a, b) => 
    new Date(b.created_at).getTime() - new Date(a.created_at).getTime()
  )
})

onMounted(async () => {
  await fetchReports()
})

async function fetchReports() {
  isLoading.value = true
  try {
    const data = await reportsService.getReports()
    reports.value = data || []
  } catch (e) {
    console.error('Error fetching reports:', e)
  } finally {
    isLoading.value = false
  }
}

async function handleUpdateStatus(id: string, status: string) {
  try {
    await reportsService.updateReportStatus(id, status)
    await fetchReports()
  } catch (e) {
    alert('Error al actualizar el estado del reporte')
  }
}

function formatDate(dateStr: string) {
  return new Date(dateStr).toLocaleString('es-ES', {
    day: '2-digit',
    month: 'short',
    hour: '2-digit',
    minute: '2-digit'
  })
}

function statusDisplay(status: string) {
  const mapping: Record<string, string> = {
    'pending': 'Pendiente',
    'resolved': 'Resuelto',
    'archived': 'Archivado'
  }
  return mapping[status] || status
}
</script>

<style scoped>
.admin-reports {
  padding: 48px 24px;
  max-width: 1000px;
  margin: 0 auto;
}

.header {
  margin-bottom: 40px;
}

.back-link {
  background: transparent;
  border: none;
  color: var(--text-secondary);
  cursor: pointer;
  display: flex;
  align-items: center;
  gap: 8px;
  font-weight: 600;
  margin-bottom: 16px;
  transition: color 0.3s;
}

.back-link:hover {
  color: var(--active-color);
}

h1 {
  font-size: 2.5rem;
  font-weight: 900;
  background: linear-gradient(135deg, #fee715 0%, #facc15 100%);
  -webkit-background-clip: text;
  background-clip: text;
  -webkit-text-fill-color: transparent;
  margin: 0;
}

.stats-overview {
  display: flex;
  gap: 24px;
  margin-bottom: 32px;
}

.stat-card {
  background: var(--card-bg);
  padding: 24px;
  border-radius: 20px;
  border: 1px solid var(--border-color);
  flex: 1;
  display: flex;
  flex-direction: column;
}

.stat-value {
  font-size: 2rem;
  font-weight: 900;
  color: var(--active-color);
}

.stat-label {
  color: var(--text-secondary);
  font-size: 0.9rem;
  font-weight: 600;
  text-transform: uppercase;
}

.reports-grid {
  display: grid;
  gap: 20px;
}

.report-card {
  background: var(--card-bg);
  backdrop-filter: blur(12px);
  border-radius: 24px;
  padding: 24px;
  border: 1px solid var(--border-color);
  transition: all 0.3s ease;
}

.report-card:hover {
  border-color: rgba(254, 231, 21, 0.3);
  transform: translateY(-4px);
}

.report-header {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  margin-bottom: 20px;
}

.user-info {
  display: flex;
  gap: 12px;
  align-items: center;
}

.user-info h3 {
  margin: 0;
  font-size: 1.1rem;
}

.user-info .date {
  font-size: 0.8rem;
  color: var(--text-secondary);
}

.status-badge {
  padding: 4px 12px;
  border-radius: 100px;
  font-size: 0.75rem;
  font-weight: 800;
  text-transform: uppercase;
}

.status-badge.pending { background: rgba(239, 68, 68, 0.1); color: #ef4444; }
.status-badge.resolved { background: rgba(34, 197, 94, 0.1); color: #22c55e; }
.status-badge.archived { background: var(--bg-secondary); color: var(--text-secondary); }

.report-body {
  margin-bottom: 24px;
  line-height: 1.6;
  color: var(--text-primary);
}

.report-actions {
  display: flex;
  justify-content: flex-end;
}

.btn-resolve, .btn-archive {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px 20px;
  border-radius: 12px;
  font-weight: 700;
  cursor: pointer;
  transition: all 0.3s;
}

.btn-resolve {
  background: var(--active-color);
  color: #101820;
  border: none;
}

.btn-resolve:hover {
  transform: scale(1.05);
  box-shadow: 0 4px 15px rgba(254, 231, 21, 0.3);
}

.btn-archive {
  background: transparent;
  border: 1px solid var(--border-color);
  color: var(--text-secondary);
}

.btn-archive:hover {
  border-color: var(--text-secondary);
  color: var(--text-primary);
}

.no-results, .loading {
  text-align: center;
  padding: 80px 24px;
  color: var(--text-secondary);
}

.spinner {
  width: 40px;
  height: 40px;
  border: 3px solid rgba(254, 231, 21, 0.1);
  border-top-color: var(--active-color);
  border-radius: 50%;
  animation: spin 1s linear infinite;
  margin: 0 auto 16px;
}

@keyframes spin {
  to { transform: rotate(360deg); }
}

@media (max-width: 600px) {
  .admin-reports { padding: 24px 16px; }
  .stats-overview { flex-direction: column; }
}
</style>
