<template>
  <div class="manual-outcome-entry">
    <div class="entry-container">
      <!-- Header -->
      <div class="entry-header">
        <h3 class="text-lg font-bold text-gray-900">Select Outcome</h3>
        <div class="text-sm text-gray-600">
          Read your player card and select the result
        </div>
      </div>

      <!-- Outcome Selection -->
      <div class="outcome-section">
        <label class="form-label">Outcome Type</label>

        <!-- Outcome Categories -->
        <div class="outcome-categories">
          <div
            v-for="category in outcomeCategories"
            :key="category.name"
            class="outcome-category"
          >
            <div class="category-header">{{ category.name }}</div>
            <div class="category-buttons">
              <button
                v-for="outcome in category.outcomes"
                :key="outcome"
                :class="[
                  'outcome-button',
                  selectedOutcome === outcome ? 'outcome-button-selected' : 'outcome-button-default'
                ]"
                @click="selectOutcome(outcome)"
              >
                {{ formatOutcome(outcome) }}
              </button>
            </div>
          </div>
        </div>
      </div>

      <!-- Hit Location (conditional) -->
      <div v-if="needsHitLocation" class="hit-location-section">
        <label class="form-label">Hit Location</label>
        <div class="location-grid">
          <div v-if="showInfieldLocations" class="location-group">
            <div class="location-group-label">Infield</div>
            <div class="location-buttons">
              <button
                v-for="position in infieldPositions"
                :key="position"
                :class="[
                  'location-button',
                  selectedHitLocation === position ? 'location-button-selected' : 'location-button-default'
                ]"
                @click="selectHitLocation(position)"
              >
                {{ position }}
              </button>
            </div>
          </div>
          <div v-if="showOutfieldLocations" class="location-group">
            <div class="location-group-label">Outfield</div>
            <div class="location-buttons">
              <button
                v-for="position in outfieldPositions"
                :key="position"
                :class="[
                  'location-button',
                  selectedHitLocation === position ? 'location-button-selected' : 'location-button-default'
                ]"
                @click="selectHitLocation(position)"
              >
                {{ position }}
              </button>
            </div>
          </div>
        </div>
      </div>

      <!-- Action Buttons -->
      <div class="action-buttons">
        <button
          class="button button-cancel"
          @click="handleCancel"
        >
          Cancel
        </button>
        <button
          :disabled="!canSubmitForm"
          :class="[
            'button',
            canSubmitForm ? 'button-submit' : 'button-submit-disabled'
          ]"
          @click="handleSubmit"
        >
          Submit Outcome
        </button>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, computed } from 'vue'
import type { PlayOutcome, RollData } from '~/types'

// Import centralized outcome constants
import { OUTCOME_CATEGORIES, OUTCOMES_REQUIRING_HIT_LOCATION, INFIELD_ONLY_OUTCOMES, OUTFIELD_ONLY_OUTCOMES, HIT_LOCATIONS } from '~/constants/outcomes'

interface Props {
  rollData: RollData | null
  canSubmit: boolean
  outs?: number
  hasRunners?: boolean
}

const props = withDefaults(defineProps<Props>(), {
  outs: 0,
  hasRunners: false,
})

const emit = defineEmits<{
  submit: [{ outcome: PlayOutcome; hitLocation?: string }]
  cancel: []
}>()

// Local state
const selectedOutcome = ref<PlayOutcome | null>(null)
const selectedHitLocation = ref<string | null>(null)

// Use imported constants
const outcomeCategories = OUTCOME_CATEGORIES
const infieldPositions = HIT_LOCATIONS.infield
const outfieldPositions = HIT_LOCATIONS.outfield
const outcomesNeedingHitLocation = OUTCOMES_REQUIRING_HIT_LOCATION

// Computed
const needsHitLocation = computed(() => {
  if (!selectedOutcome.value) return false
  if (!(outcomesNeedingHitLocation as readonly string[]).includes(selectedOutcome.value)) return false

  // Hit location matters when there are runners on base
  // (for determining runner advancement regardless of outs)
  return props.hasRunners
})

// Hide outfield positions for groundball outcomes (they stay in the infield)
const showOutfieldLocations = computed(() => {
  if (!selectedOutcome.value) return true
  return !(INFIELD_ONLY_OUTCOMES as readonly string[]).includes(selectedOutcome.value)
})

// Hide infield positions for flyball outcomes (they go to the outfield)
const showInfieldLocations = computed(() => {
  if (!selectedOutcome.value) return true
  return !(OUTFIELD_ONLY_OUTCOMES as readonly string[]).includes(selectedOutcome.value)
})

const canSubmitForm = computed(() => {
  if (!selectedOutcome.value) return false
  if (needsHitLocation.value && !selectedHitLocation.value) return false
  return props.canSubmit && props.rollData !== null
})

// Methods
const selectOutcome = (outcome: PlayOutcome) => {
  selectedOutcome.value = outcome
  // Clear hit location if the new outcome doesn't need it
  if (!(outcomesNeedingHitLocation as readonly string[]).includes(outcome)) {
    selectedHitLocation.value = null
  }
  // Clear outfield hit location if switching to groundball (infield only)
  if ((INFIELD_ONLY_OUTCOMES as readonly string[]).includes(outcome)) {
    const outfield = HIT_LOCATIONS.outfield as readonly string[]
    if (selectedHitLocation.value && outfield.includes(selectedHitLocation.value)) {
      selectedHitLocation.value = null
    }
  }
  // Clear infield hit location if switching to flyball (outfield only)
  if ((OUTFIELD_ONLY_OUTCOMES as readonly string[]).includes(outcome)) {
    const infield = HIT_LOCATIONS.infield as readonly string[]
    if (selectedHitLocation.value && infield.includes(selectedHitLocation.value)) {
      selectedHitLocation.value = null
    }
  }
}

const selectHitLocation = (location: string) => {
  selectedHitLocation.value = location
}

const handleSubmit = () => {
  if (!canSubmitForm.value || !selectedOutcome.value) return

  emit('submit', {
    outcome: selectedOutcome.value,
    hitLocation: selectedHitLocation.value || undefined,
  })

  // Reset form
  selectedOutcome.value = null
  selectedHitLocation.value = null
}

const handleCancel = () => {
  selectedOutcome.value = null
  selectedHitLocation.value = null
  emit('cancel')
}

const formatOutcome = (outcome: string): string => {
  // Convert outcome to readable format
  return outcome
    .split('_')
    .map(word => word.charAt(0) + word.slice(1).toLowerCase())
    .join(' ')
}
</script>

<style scoped>
.manual-outcome-entry {
  @apply w-full;
}

.entry-container {
  @apply bg-white rounded-xl shadow-lg p-6 space-y-6;
}

/* Header */
.entry-header {
  @apply pb-4 border-b border-gray-200;
}

/* Form Sections */
.outcome-section,
.hit-location-section {
  @apply space-y-3;
}

.form-label {
  @apply block text-sm font-semibold text-gray-700 uppercase tracking-wide mb-2;
}

/* Outcome Categories */
.outcome-categories {
  @apply space-y-4;
}

.outcome-category {
  @apply space-y-2;
}

.category-header {
  @apply text-xs font-bold text-gray-600 uppercase tracking-wider;
}

.category-buttons {
  @apply flex flex-wrap gap-2;
}

/* Outcome Buttons */
.outcome-button {
  @apply px-4 py-2 rounded-lg text-sm font-medium transition-all duration-150;
  @apply border-2 min-h-[44px];
}

.outcome-button-default {
  @apply bg-white border-gray-300 text-gray-700;
  @apply hover:border-blue-400 hover:bg-blue-50;
}

.outcome-button-selected {
  @apply bg-gradient-to-r from-blue-500 to-blue-600 border-blue-600 text-white;
  @apply shadow-md;
}

/* Hit Location Grid */
.location-grid {
  @apply grid grid-cols-1 gap-4;
}

@media (min-width: 640px) {
  .location-grid {
    @apply grid-cols-2;
  }
}

.location-group {
  @apply space-y-2;
}

.location-group-label {
  @apply text-xs font-bold text-gray-600 uppercase tracking-wider;
}

.location-buttons {
  @apply grid grid-cols-3 gap-2;
}

/* Location Buttons */
.location-button {
  @apply px-3 py-3 rounded-lg text-sm font-bold transition-all duration-150;
  @apply border-2 min-h-[44px];
}

.location-button-default {
  @apply bg-white border-gray-300 text-gray-700;
  @apply hover:border-green-400 hover:bg-green-50;
}

.location-button-selected {
  @apply bg-gradient-to-r from-green-500 to-green-600 border-green-600 text-white;
  @apply shadow-md;
}

/* Action Buttons */
.action-buttons {
  @apply flex gap-3 pt-4 border-t border-gray-200;
}

.button {
  @apply flex-1 px-6 py-3 rounded-lg font-bold text-base transition-all duration-200;
  @apply shadow-md min-h-[52px];
}

.button-cancel {
  @apply bg-white border-2 border-gray-300 text-gray-700;
  @apply hover:border-gray-400 hover:bg-gray-50;
}

.button-submit {
  @apply bg-gradient-to-r from-blue-500 to-blue-600 text-white;
  @apply hover:from-blue-600 hover:to-blue-700 hover:shadow-lg;
  @apply active:scale-95;
}

.button-submit-disabled {
  @apply bg-gray-300 text-gray-500 cursor-not-allowed;
}

/* Dark mode support */
@media (prefers-color-scheme: dark) {
  .entry-container {
    @apply bg-gray-800 border border-gray-700;
  }

  .entry-header {
    @apply border-gray-700;
  }

  .entry-header h3,
  .form-label {
    @apply text-gray-100;
  }

  .entry-header .text-sm {
    @apply text-gray-400;
  }

  .category-header,
  .location-group-label {
    @apply text-gray-400;
  }

  .outcome-button-default,
  .location-button-default {
    @apply bg-gray-700 border-gray-600 text-gray-200;
    @apply hover:border-blue-400 hover:bg-gray-600;
  }

  .button-cancel {
    @apply bg-gray-700 border-gray-600 text-gray-200;
    @apply hover:border-gray-500 hover:bg-gray-600;
  }

  .action-buttons {
    @apply border-gray-700;
  }
}
</style>
