<script setup>
defineProps({
  loading: {
    type: Boolean,
    required: true
  },
  isWorking: {
    type: Boolean,
    required: true
  },
  isProcessingSearch: {
    type: Boolean,
    required: true
  },
  session: {
    type: Object,
    default: null
  },
  simulatorUrl: {
    type: String,
    required: true
  },
  busyLabel: {
    type: String,
    required: true
  },
  canPickFile: {
    type: Boolean,
    required: true
  },
  isDragging: {
    type: Boolean,
    required: true
  },
  selectedFile: {
    type: Object,
    default: null
  },
  selectedFileSizeLabel: {
    type: String,
    required: true
  },
  canStartSearch: {
    type: Boolean,
    required: true
  },
  fileInput: {
    type: Object,
    required: true
  },
  t: {
    type: Function,
    required: true
  }
});

const emit = defineEmits([
  'open-file-picker',
  'file-change',
  'drag-enter',
  'drag-over',
  'drag-leave',
  'drop',
  'clear-file',
  'submit-search'
]);
</script>

<template>
  <section class="faceai-panel shadow-sm">
    <div v-if="loading" class="faceai-loading-state">
      <span class="spinner-border text-warning" role="status" aria-hidden="true"></span>
      <span>{{ busyLabel }}</span>
    </div>

    <div v-else-if="!session" class="faceai-empty-state">
      <h2 class="faceai-section-title">{{ t('pageTitle') }}</h2>
      <p class="mb-3">{{ t('handoffMissing') }}</p>
      <a class="btn btn-warning" :href="simulatorUrl">{{ t('openSimulator') }}</a>
    </div>

    <template v-else>
      <div class="faceai-panel-header">
        <div>
          <h2 class="faceai-section-title mb-2">{{ t('uploaderTitle') }}</h2>
          <p class="faceai-panel-subtitle mb-0">{{ t('uploaderHint') }}</p>
        </div>
      </div>

      <div v-if="isWorking && busyLabel" class="faceai-busy-banner" aria-live="polite">
        <span class="faceai-spinner" role="status" aria-hidden="true"></span>
        <span>{{ busyLabel }}</span>
      </div>

      <div
        class="faceai-dropzone"
        :class="{
          'is-dragging': isDragging,
          'is-disabled': !canPickFile,
          'has-file': selectedFile,
          'is-processing': isProcessingSearch
        }"
        @click="emit('open-file-picker')"
        @dragenter="emit('drag-enter', $event)"
        @dragover="emit('drag-over', $event)"
        @dragleave="emit('drag-leave', $event)"
        @drop="emit('drop', $event)"
      >
        <input
          ref="fileInput"
          class="d-none"
          type="file"
          accept="image/*"
          :disabled="!canPickFile"
          @change="emit('file-change', $event)"
        />

        <div class="faceai-dropzone-inner">
          <div class="faceai-dropzone-icon">
            <i class="fa fa-cloud-upload" aria-hidden="true"></i>
          </div>

          <template v-if="selectedFile">
            <p class="faceai-dropzone-title mb-2">{{ t('uploaderSelected') }}</p>
            <strong class="faceai-file-name">{{ selectedFile.name }}</strong>
            <p class="faceai-file-meta mb-0">{{ selectedFileSizeLabel }}</p>
          </template>

          <template v-else>
            <p class="faceai-dropzone-title mb-2">
              {{ isDragging ? t('uploaderDragActive') : t('uploaderDragIdle') }}
            </p>
            <p class="faceai-dropzone-copy mb-0">
              {{ canPickFile ? t('uploaderFormats') : t('dropzoneDisabled') }}
            </p>
          </template>
        </div>

        <div class="faceai-dropzone-actions" @click.stop>
          <button class="btn btn-outline-warning" type="button" :disabled="!canPickFile" @click="emit('open-file-picker')">
            {{ selectedFile ? t('uploaderReplace') : t('uploaderBrowse') }}
          </button>
          <button v-if="selectedFile" class="btn btn-link" type="button" @click="emit('clear-file')">
            {{ t('uploaderRemove') }}
          </button>
        </div>

        <div v-if="isDragging" class="faceai-dropzone-overlay">
          <span>{{ t('uploaderDragActive') }}</span>
        </div>

        <div v-if="isProcessingSearch" class="faceai-processing-overlay" aria-live="polite" aria-busy="true">
          <span class="faceai-spinner faceai-spinner-lg" role="status" aria-hidden="true"></span>
          <strong>{{ busyLabel }}</strong>
        </div>
      </div>

      <div class="faceai-action-row">
        <button v-if="selectedFile" class="btn btn-warning" type="button" :disabled="!canStartSearch" @click="emit('submit-search')">
          {{ t('uploadButton') }}
        </button>
        <a class="btn btn-light" :href="session.returnUrl">{{ t('backButton') }}</a>
      </div>

      <div v-if="!selectedFile && canPickFile" class="faceai-subtle-note">
        {{ t('noFileCta') }}
      </div>
    </template>
  </section>
</template>

<style scoped>
.faceai-panel {
  border-radius: 28px;
  padding: 1.5rem;
  background: #fffdf9;
  border: 1px solid rgba(212, 189, 154, 0.55);
}

.faceai-panel-header {
  display: flex;
  justify-content: space-between;
  gap: 1rem;
  margin-bottom: 1.25rem;
}

.faceai-section-title {
  margin: 0;
  font-size: 1.45rem;
  color: #30261e;
}

.faceai-panel-subtitle,
.faceai-dropzone-copy,
.faceai-subtle-note {
  color: #665548;
}

.faceai-busy-banner {
  display: inline-flex;
  align-items: center;
  gap: 0.65rem;
  margin-bottom: 1rem;
  padding: 0.8rem 1rem;
  border-radius: 999px;
  background: rgba(255, 248, 224, 0.95);
  color: #744500;
  border: 1px solid rgba(213, 138, 0, 0.24);
}

.faceai-spinner {
  width: 1rem;
  height: 1rem;
  border-radius: 50%;
  border: 2px solid rgba(191, 158, 117, 0.3);
  border-top-color: #c87800;
  animation: faceai-spin 0.75s linear infinite;
}

.faceai-spinner-lg {
  width: 2.75rem;
  height: 2.75rem;
  border-width: 4px;
}

.faceai-loading-state,
.faceai-empty-state {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 1rem;
  padding: 1rem 0.25rem;
}

.faceai-dropzone {
  position: relative;
  border-radius: 24px;
  border: 2px dashed rgba(187, 144, 72, 0.55);
  background: linear-gradient(180deg, rgba(255, 248, 235, 0.95), rgba(252, 244, 230, 0.98));
  min-height: 280px;
  padding: 1.5rem;
  cursor: pointer;
  transition: transform 0.18s ease, border-color 0.18s ease, box-shadow 0.18s ease, background 0.18s ease;
}

.faceai-dropzone.is-processing {
  overflow: hidden;
}

.faceai-dropzone:hover {
  transform: translateY(-1px);
  box-shadow: 0 18px 38px rgba(93, 72, 44, 0.08);
}

.faceai-dropzone.is-dragging {
  border-color: #d58a00;
  background: linear-gradient(180deg, #fff4d7, #ffe7a8);
  box-shadow: 0 20px 45px rgba(213, 138, 0, 0.18);
}

.faceai-dropzone.is-disabled {
  cursor: not-allowed;
  opacity: 0.7;
}

.faceai-dropzone.has-file {
  border-style: solid;
  background: linear-gradient(180deg, #fffaf0, #f8efe0);
}

.faceai-dropzone-inner {
  min-height: 190px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  text-align: center;
}

.faceai-dropzone-icon {
  width: 88px;
  height: 88px;
  margin-bottom: 1rem;
  border-radius: 50%;
  display: grid;
  place-items: center;
  background: rgba(255, 255, 255, 0.9);
  color: #b87014;
  font-size: 2rem;
  box-shadow: inset 0 0 0 1px rgba(191, 158, 117, 0.24);
}

.faceai-dropzone-title {
  font-size: 1.2rem;
  font-weight: 700;
  color: #2d241c;
}

.faceai-file-name {
  display: block;
  font-size: 1.05rem;
  color: #2d241c;
  word-break: break-word;
}

.faceai-file-meta {
  color: #7b6857;
}

.faceai-dropzone-actions {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 0.75rem;
  margin-top: 1rem;
}

.faceai-dropzone-overlay {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  border-radius: 22px;
  background: rgba(255, 236, 184, 0.84);
  color: #764300;
  font-size: 1.15rem;
  font-weight: 700;
  letter-spacing: 0.02em;
}

.faceai-processing-overlay {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 0.9rem;
  border-radius: 22px;
  background: rgba(255, 249, 238, 0.9);
  color: #5e3800;
  text-align: center;
  padding: 1.5rem;
  backdrop-filter: blur(2px);
}

.faceai-action-row {
  display: flex;
  flex-wrap: wrap;
  gap: 0.75rem;
  margin-top: 1.25rem;
}

.faceai-subtle-note {
  margin-top: 0.85rem;
  font-size: 0.95rem;
}

@media (max-width: 991.98px) {
  .faceai-panel {
    padding: 1.25rem;
  }
}

@media (max-width: 767.98px) {
  .faceai-dropzone {
    min-height: 240px;
    padding: 1rem;
  }

  .faceai-action-row {
    flex-direction: column;
  }

  .faceai-action-row .btn,
  .faceai-empty-state .btn {
    width: 100%;
  }

  .faceai-dropzone-actions {
    flex-direction: column;
  }

  .faceai-dropzone-actions .btn {
    width: 100%;
  }
}

@keyframes faceai-spin {
  to {
    transform: rotate(360deg);
  }
}
</style>