<script setup lang="ts">
import AdminNominationReviewModal from './AdminNominationReviewModal.vue'
import AdminReviewsHistorySection from './AdminReviewsHistorySection.vue'
import AdminReviewsQueueHeader from './AdminReviewsQueueHeader.vue'
import AdminReviewsQueueList from './AdminReviewsQueueList.vue'
import AdminReviewSubcategoryTabs from './AdminReviewSubcategoryTabs.vue'
import { getTrackerStatusMeta, useAdminReviewsManager } from './useAdminReviewsManager'
import { watchAdminToast } from '../../composables/useAdminToast'

const props = withDefaults(defineProps<{
  categoryIdsFilter?: number[] | null
  activeMainCategory?: string | null
  showCategoryFilterChips?: boolean
  historyCollapsible?: boolean
  showBlacklistButton?: boolean
}>(), {
  categoryIdsFilter: null,
  activeMainCategory: null,
  showCategoryFilterChips: true,
  historyCollapsible: false,
  showBlacklistButton: false,
})

const emit = defineEmits<{
  'open-blacklist': []
}>()

const {
  reviewSaving,
  blacklistSaving,
  adminMessage,
  adminError,
  reviewForms,
  seasonDetail,
  reviewFilter,
  categoryFilter,
  subcategoryFilter,
  selectedNominationId,
  isReviewModalOpen,
  candidatePlatformOptions,
  filteredNominations,
  selectedNomination,
  reviewedNominations,
  categoryOptions,
  availableSubcategoryOptions,
  tierCategoryOptions,
  selectedCandidateCollision,
  selectedRelatedPendingNominations,
  selectedNominationSignalSummary,
  trackingReviewNotes,
  canApproveSelected,
  approveNomination,
  rejectNomination,
  reopenRejectedNomination,
  updateTrackingReview,
  addStreamUrlToBlacklist,
  openNominationById,
  closeNominationModal,
  selectedPlatformValue,
  handlePlatformSelection,
  extractNominationStreamUrl,
} = useAdminReviewsManager({
  categoryIdsFilter: () => props.categoryIdsFilter,
})

watchAdminToast(adminMessage, adminError)
</script>

<template>
  <div class="min-h-0 overflow-y-auto bg-white">
    <AdminReviewsQueueHeader
      v-model:review-filter="reviewFilter"
      v-model:category-filter="categoryFilter"
      :total-pending="seasonDetail.pendingNominations.length"
      :visible-count="filteredNominations.length"
      :category-options="categoryOptions"
      :active-main-category="props.activeMainCategory"
      :show-category-filter-chips="showCategoryFilterChips"
      :show-blacklist-button="showBlacklistButton"
      @open-blacklist="emit('open-blacklist')"
    />

    <div class="space-y-4 p-4 sm:p-6">
      <AdminReviewSubcategoryTabs
        v-model="subcategoryFilter"
        :tabs="availableSubcategoryOptions"
      />

      <AdminReviewsQueueList
        :nominations="filteredNominations"
        :total-pending="seasonDetail.pendingNominations.length"
        :selected-nomination-id="selectedNominationId"
        :tracker-status-meta="getTrackerStatusMeta"
        @select="selectedNominationId = $event"
        @open="openNominationById"
      />

      <details v-if="historyCollapsible" class="rounded-[24px] border border-violet-100 bg-violet-50/40">
        <summary class="cursor-pointer px-5 py-4 text-sm font-semibold text-violet-900">
          Letzte Entscheidungen anzeigen
        </summary>
        <div class="border-t border-violet-100 p-5">
          <AdminReviewsHistorySection
            :reviewed-nominations="reviewedNominations"
            :reviewed-total="seasonDetail.reviewedNominations.length"
            :review-saving="reviewSaving"
            @undo-rejected="reopenRejectedNomination"
          />
        </div>
      </details>

      <AdminReviewsHistorySection
        v-else
        :reviewed-nominations="reviewedNominations"
        :reviewed-total="seasonDetail.reviewedNominations.length"
        :review-saving="reviewSaving"
        @undo-rejected="reopenRejectedNomination"
      />
    </div>

    <AdminNominationReviewModal
      :open="isReviewModalOpen"
      :nomination="selectedNomination"
      :review-saving="reviewSaving"
      :review-form="selectedNomination ? reviewForms[selectedNomination.id] : null"
      :candidate-platform-options="candidatePlatformOptions"
      :category-options="tierCategoryOptions"
      :selected-candidate-collision="selectedCandidateCollision"
      :selected-related-pending-nominations="selectedRelatedPendingNominations"
      :signal-summary="selectedNominationSignalSummary"
      :tracking-review-notes="trackingReviewNotes"
      :stream-url="selectedNomination ? extractNominationStreamUrl(selectedNomination) : ''"
      :can-approve-selected="canApproveSelected"
      :blacklist-saving="blacklistSaving"
      :selected-platform-value="selectedPlatformValue"
      :tracker-status-meta="getTrackerStatusMeta"
      @close="closeNominationModal"
      @platform-change="handlePlatformSelection"
      @approve="approveNomination"
      @reject="rejectNomination"
      @review-flagged="updateTrackingReview($event, 'reviewed')"
      @override-flagged="updateTrackingReview($event, 'overridden')"
      @blacklist-link="addStreamUrlToBlacklist"
    />
  </div>
</template>
