<script setup lang="ts">
import { ref, onMounted } from 'vue'

const props = defineProps<{ token: string }>()
const API_BASE = import.meta.env.VITE_API_BASE_URL || 'http://localhost:8080/api/v1'

const competitors = ref<Array<any>>([])
const isLoading = ref(true)
const isSubmitting = ref(false)
const errorMsg = ref('')

const showForm = ref(false)
const form = ref({
  competitor_name: '',
  platform: 'twitter',
  target_handle: '',
  keywords: ''
})

const fetchCompetitors = async () => {
  isLoading.value = true
  try {
    const res = await fetch(`${API_BASE}/monitors/competitors`, {
      headers: { 'Authorization': `Bearer ${props.token}` }
    })
    const data = await res.json()
    if (res.ok && data.code === 200) {
      competitors.value = data.data || []
    }
  } catch (err) {
    console.error(err)
  } finally {
    isLoading.value = false
  }
}

const createCompetitor = async () => {
  isSubmitting.value = true
  errorMsg.value = ''
  try {
    const res = await fetch(`${API_BASE}/monitors/competitors`, {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json',
        'Authorization': `Bearer ${props.token}`
      },
      body: JSON.stringify(form.value)
    })
    const data = await res.json()
    if (!res.ok || data.code !== 200) {
      throw new Error(data.message || 'Failed to add competitor')
    }
    
    form.value = { competitor_name: '', platform: 'twitter', target_handle: '', keywords: '' }
    showForm.value = false
    await fetchCompetitors()
    
  } catch (err: any) {
    errorMsg.value = err.message
  } finally {
    isSubmitting.value = false
  }
}

const deleteCompetitor = async (id: number) => {
  if (!confirm('Are you sure you want to stop tracking this competitor?')) return
  
  try {
    const res = await fetch(`${API_BASE}/monitors/competitors/${id}`, {
      method: 'DELETE',
      headers: { 'Authorization': `Bearer ${props.token}` }
    })
    if (res.ok) {
      competitors.value = competitors.value.filter(c => c.id !== id)
    }
  } catch (err) {
    console.error(err)
  }
}

onMounted(() => {
  if (props.token) fetchCompetitors()
})
</script>

<template>
  <div class="space-y-12">
    
    <header class="flex justify-between items-end">
      <div class="space-y-1">
        <h2 class="text-2xl font-bold tracking-tight">Competitor Radar</h2>
        <p class="text-sm text-zinc-500 font-medium">Identify and engage in conversations happening around your alternatives.</p>
      </div>
      <button 
        v-if="!showForm"
        @click="showForm = true"
        class="px-5 py-2.5 bg-blue-600 hover:bg-blue-500 text-white rounded-[14px] text-sm font-bold transition-apple shadow-lg shadow-blue-900/20 active:scale-95 flex items-center gap-2"
      >
        <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="3" stroke-linecap="round" stroke-linejoin="round"><path d="M5 12h14"/><path d="M12 5v14"/></svg>
        Add Target
      </button>
    </header>

    <div v-if="isLoading" class="py-20 flex justify-center">
      <div class="w-8 h-8 border-2 border-white/10 border-t-blue-500 rounded-full animate-spin"></div>
    </div>

    <div v-else class="space-y-8">
      <!-- Create Form Modal View -->
      <transition enter-active-class="transition duration-500 ease-out transform" enter-from-class="opacity-0 scale-95 -translate-y-4" enter-to-class="opacity-100 scale-100 translate-y-0">
        <form v-if="showForm" @submit.prevent="createCompetitor" class="bg-white/[0.03] border border-white/5 rounded-[32px] p-8 space-y-8 shadow-2xl relative">
          <div class="flex justify-between items-center">
            <h3 class="text-lg font-bold">New Radar Target</h3>
            <button type="button" @click="showForm = false" class="p-2 rounded-full hover:bg-white/5 transition-apple">
              <svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><path d="M18 6L6 18M6 6l12 12"/></svg>
            </button>
          </div>

          <div v-if="errorMsg" class="p-4 bg-rose-500/10 border border-rose-500/20 text-rose-400 text-xs font-bold rounded-xl">
            {{ errorMsg }}
          </div>

          <div class="grid grid-cols-2 gap-8">
            <div class="space-y-3">
              <label class="text-[10px] font-bold text-zinc-500 uppercase tracking-[0.2em] ml-1">Competitor Name</label>
              <input v-model="form.competitor_name" required class="dashboard-input" placeholder="e.g. Acme Corp" />
            </div>
            <div class="space-y-3">
              <label class="text-[10px] font-bold text-zinc-500 uppercase tracking-[0.2em] ml-1">Platform</label>
              <div class="relative">
                <select v-model="form.platform" class="dashboard-input appearance-none cursor-pointer">
                    <option value="twitter">Twitter / X</option>
                    <option value="reddit">Reddit (Coming Soon)</option>
                </select>
                <div class="absolute right-5 top-1/2 -translate-y-1/2 pointer-events-none text-zinc-600">
                  <svg xmlns="http://www.w3.org/2000/svg" width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="3" stroke-linecap="round" stroke-linejoin="round"><path d="m6 9 6 6 6-6"/></svg>
                </div>
              </div>
            </div>
          </div>
          
          <div class="grid grid-cols-2 gap-8">
            <div class="space-y-3">
              <label class="text-[10px] font-bold text-zinc-500 uppercase tracking-[0.2em] ml-1">X Handle <span class="text-zinc-700">(optional)</span></label>
              <div class="relative">
                  <span class="absolute inset-y-0 left-5 flex items-center text-zinc-600 pointer-events-none text-sm font-bold">@</span>
                  <input v-model="form.target_handle" class="dashboard-input pl-10" placeholder="handle" />
              </div>
            </div>
            <div class="space-y-3">
              <label class="text-[10px] font-bold text-zinc-500 uppercase tracking-[0.2em] ml-1">Monitor Keywords</label>
              <input v-model="form.keywords" required class="dashboard-input" placeholder="e.g. acme sucks, alternative to acme" />
            </div>
          </div>

          <div class="flex justify-end">
            <button type="submit" :disabled="isSubmitting" class="px-8 py-3.5 bg-blue-600 hover:bg-blue-500 text-white rounded-2xl text-[15px] font-bold transition-apple shadow-xl shadow-blue-900/30 flex items-center gap-3 active:scale-95 disabled:opacity-50">
               <div v-if="isSubmitting" class="w-4 h-4 border-2 border-white/20 border-t-white rounded-full animate-spin"></div>
               {{ isSubmitting ? 'Calibrating...' : 'Initialize Radar' }}
            </button>
          </div>
        </form>
      </transition>

      <!-- Radar List -->
      <div v-if="competitors.length === 0 && !showForm" class="text-center py-20 bg-white/[0.01] border-2 border-dashed border-white/5 rounded-[40px] space-y-4">
        <div class="w-16 h-16 bg-white/[0.03] rounded-3xl mx-auto flex items-center justify-center border border-white/5">
           <svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" class="text-zinc-700"><circle cx="12" cy="12" r="10"/><circle cx="12" cy="12" r="3"/><path d="M12 2v2"/><path d="M12 20v2"/><path d="M2 12h2"/><path d="M20 12h2"/></svg>
        </div>
        <p class="text-zinc-500 font-bold tracking-tight">Radar is currently silent. No competitors added.</p>
      </div>

      <div class="grid grid-cols-2 gap-6">
        <div v-for="c in competitors" :key="c.id" class="group bg-white/[0.02] border border-white/5 rounded-[32px] p-6 flex flex-col justify-between transition-apple hover:bg-white/[0.04] hover:border-white/10 hover:shadow-2xl relative overflow-hidden">
            
            <div class="flex justify-between items-start mb-6 z-10">
                <div class="flex items-center gap-4">
                    <div class="w-12 h-12 rounded-2xl bg-blue-500/10 border border-blue-500/20 flex items-center justify-center text-blue-500 group-hover:scale-110 transition-apple">
                        <svg v-if="c.platform === 'twitter'" width="20" height="20" viewBox="0 0 24 24" fill="currentColor"><path d="M23.953 4.57a10 10 0 01-2.825.775 4.958 4.958 0 002.163-2.723c-.951.555-2.005.959-3.127 1.184a4.92 4.92 0 00-8.384 4.482C7.69 8.095 4.067 6.13 1.64 3.162a4.822 4.822 0 00-.666 2.475c0 1.71.87 3.213 2.188 4.096a4.904 4.904 0 01-2.228-.616v.06a4.923 4.923 0 003.946 4.827 4.996 4.996 0 01-2.212.085 4.936 4.936 0 004.604 3.417 9.867 9.867 0 01-6.102 2.105c-.39 0-.779-.023-1.17-.067a13.995 13.995 0 007.557 2.209c9.053 0 13.998-7.496 13.998-13.985 0-.21 0-.42-.015-.63A9.935 9.935 0 0024 4.59z"/></svg>
                        <svg v-else width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><circle cx="12" cy="12" r="10"/><path d="M12 2v2"/><path d="M12 20v2"/><path d="M2 12h2"/><path d="M20 12h2"/></svg>
                    </div>
                    <div>
                        <h4 class="text-[17px] font-black tracking-tight group-hover:text-blue-400 transition-apple">{{ c.competitor_name }}</h4>
                        <p v-if="c.target_handle" class="text-[11px] font-bold text-zinc-600 mt-0.5 tracking-tight font-mono">@{{ c.target_handle }}</p>
                    </div>
                </div>
                <div class="group/pulse flex items-center gap-2 px-3 py-1 bg-emerald-500/10 rounded-full border border-emerald-500/20">
                   <div class="w-1.5 h-1.5 rounded-full bg-emerald-500 animate-pulse"></div>
                   <span class="text-[9px] font-black text-emerald-400 uppercase tracking-widest">Tracking</span>
                </div>
            </div>

            <div class="space-y-3 z-10">
                <span class="text-[9px] font-black text-zinc-700 uppercase tracking-[0.2em] flex items-center gap-2">
                  <svg xmlns="http://www.w3.org/2000/svg" width="10" height="10" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="3"><circle cx="11" cy="11" r="8"/><path d="m21 21-4.3-4.3"/></svg>
                  Search Logic
                </span>
                <div class="flex flex-wrap gap-2">
                    <span v-for="kw in c.keywords.split(',')" :key="kw" class="px-3 py-1 bg-black/40 text-zinc-400 text-[11px] font-bold rounded-lg border border-white/5 transition-apple group-hover:border-blue-500/30 group-hover:text-zinc-200">
                        {{ kw.trim() }}
                    </span>
                </div>
            </div>

            <button @click="deleteCompetitor(c.id)" class="absolute top-2 right-2 p-3 text-zinc-800 hover:text-rose-500 transition-apple opacity-0 group-hover:opacity-100 z-20 active:scale-90">
                <svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.5"><path d="M3 6h18M19 6v14a2 2 0 01-2 2H7a2 2 0 01-2-2V6m3 0V4a2 2 0 012-2h4a2 2 0 012 2v2"/></svg>
            </button>

            <!-- Abstract Decor -->
            <div class="absolute -right-10 -bottom-10 w-32 h-32 bg-blue-500/5 blur-3xl rounded-full group-hover:bg-blue-500/10 transition-apple"></div>
        </div>
      </div>
      
    </div>
  </div>
</template>

<style scoped>
.dashboard-input {
  @apply w-full bg-black/40 border border-white/5 rounded-2xl px-5 py-3.5 text-sm font-semibold;
  @apply transition-apple focus:outline-none focus:ring-4 focus:ring-blue-500/10 focus:border-blue-500/40 placeholder:text-zinc-700;
}
</style>
