<script setup lang="ts">
import { ref, onMounted } from 'vue'
import { useWipeStore } from '@/stores/wipe'
import { FileText, Plus, ChevronDown, ChevronRight } from 'lucide-vue-next'

const wipeStore = useWipeStore()
const expandedId = ref<string | null>(null)

function toggle(id: string) {
  expandedId.value = expandedId.value === id ? null : id
}

onMounted(() => {
  wipeStore.fetchProfiles()
})
</script>

<template>
  <div class="p-6 space-y-6">
    <!-- Header -->
    <div class="flex items-center justify-between">
      <div class="flex items-center gap-3">
        <FileText class="w-5 h-5 text-oxide-500" />
        <h1 class="text-2xl font-bold text-neutral-100">Wipe Profiles</h1>
      </div>
      <button class="flex items-center gap-2 px-4 py-2 bg-oxide-600 hover:bg-oxide-700 text-white text-sm font-medium rounded-lg transition-colors">
        <Plus class="w-4 h-4" />
        New Profile
      </button>
    </div>

    <!-- Profiles -->
    <div v-if="wipeStore.profiles.length === 0" class="bg-neutral-900 border border-neutral-800 rounded-lg p-12 text-center">
      <FileText class="w-10 h-10 text-neutral-600 mx-auto mb-3" />
      <h3 class="text-lg font-medium text-neutral-300 mb-1">No Wipe Profiles</h3>
      <p class="text-sm text-neutral-500">Create a profile to define pre-wipe and post-wipe behavior.</p>
    </div>

    <div v-else class="space-y-3">
      <div
        v-for="profile in wipeStore.profiles"
        :key="profile.id"
        class="bg-neutral-900 border border-neutral-800 rounded-lg overflow-hidden"
      >
        <button
          @click="toggle(profile.id)"
          class="w-full flex items-center justify-between p-4 text-left hover:bg-neutral-800/50 transition-colors"
        >
          <div>
            <h3 class="text-sm font-medium text-neutral-100">{{ profile.profile_name }}</h3>
            <p class="text-xs text-neutral-500 mt-0.5">{{ profile.description || 'No description' }}</p>
          </div>
          <component :is="expandedId === profile.id ? ChevronDown : ChevronRight" class="w-4 h-4 text-neutral-500" />
        </button>

        <div v-if="expandedId === profile.id" class="border-t border-neutral-800 p-4">
          <div class="grid grid-cols-2 gap-6">
            <!-- Pre-wipe -->
            <div>
              <h4 class="text-xs font-medium text-neutral-400 uppercase tracking-wider mb-3">Pre-Wipe</h4>
              <div class="space-y-2 text-sm">
                <div class="flex justify-between">
                  <span class="text-neutral-500">Backup before wipe</span>
                  <span :class="profile.pre_wipe_config.backup_before_wipe ? 'text-green-400' : 'text-neutral-600'">
                    {{ profile.pre_wipe_config.backup_before_wipe ? 'Yes' : 'No' }}
                  </span>
                </div>
                <div class="flex justify-between">
                  <span class="text-neutral-500">Kick players</span>
                  <span :class="profile.pre_wipe_config.kick_players_before_wipe ? 'text-green-400' : 'text-neutral-600'">
                    {{ profile.pre_wipe_config.kick_players_before_wipe ? 'Yes' : 'No' }}
                  </span>
                </div>
                <div class="flex justify-between">
                  <span class="text-neutral-500">Final save</span>
                  <span :class="profile.pre_wipe_config.run_final_save ? 'text-green-400' : 'text-neutral-600'">
                    {{ profile.pre_wipe_config.run_final_save ? 'Yes' : 'No' }}
                  </span>
                </div>
                <div class="flex justify-between">
                  <span class="text-neutral-500">Discord announce</span>
                  <span :class="profile.pre_wipe_config.discord_pre_announce ? 'text-green-400' : 'text-neutral-600'">
                    {{ profile.pre_wipe_config.discord_pre_announce ? 'Yes' : 'No' }}
                  </span>
                </div>
              </div>
            </div>

            <!-- Post-wipe -->
            <div>
              <h4 class="text-xs font-medium text-neutral-400 uppercase tracking-wider mb-3">Post-Wipe</h4>
              <div class="space-y-2 text-sm">
                <div class="flex justify-between">
                  <span class="text-neutral-500">Verify server started</span>
                  <span :class="profile.post_wipe_config.verify_server_started ? 'text-green-400' : 'text-neutral-600'">
                    {{ profile.post_wipe_config.verify_server_started ? 'Yes' : 'No' }}
                  </span>
                </div>
                <div class="flex justify-between">
                  <span class="text-neutral-500">Verify plugins loaded</span>
                  <span :class="profile.post_wipe_config.verify_plugins_loaded ? 'text-green-400' : 'text-neutral-600'">
                    {{ profile.post_wipe_config.verify_plugins_loaded ? 'Yes' : 'No' }}
                  </span>
                </div>
                <div class="flex justify-between">
                  <span class="text-neutral-500">Rollback on failure</span>
                  <span :class="profile.post_wipe_config.rollback_on_failure ? 'text-oxide-400' : 'text-neutral-600'">
                    {{ profile.post_wipe_config.rollback_on_failure ? 'Yes' : 'No' }}
                  </span>
                </div>
                <div class="flex justify-between">
                  <span class="text-neutral-500">Max restart attempts</span>
                  <span class="text-neutral-300">{{ profile.post_wipe_config.max_restart_attempts }}</span>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>
