<script setup>
const {
  loading,
  config,
  selectedExtension,
  dialTarget,
  activeSession,
  remoteAudio,
  sipLoading,
  sipRegistered,
  sipStatus,
  sipError,
  callState,
  registererState,
  canRegisterSip,
  canStartCall,
  canHangup,
  loadTelephony,
  registerSip,
  stopSip,
  startCall,
  hangupCall,
} = useTelephonySoftphone()

onMounted(loadTelephony)
</script>

<template>
  <div class="min-h-0 flex-1 overflow-y-auto bg-gray-50 px-4 py-6 sm:px-6 lg:px-8">
    <div class="mx-auto flex max-w-7xl flex-col gap-6">
      <div class="flex flex-col gap-4 border-b border-gray-200 pb-5 lg:flex-row lg:items-end lg:justify-between">
        <div>
          <p class="text-sm font-medium text-primary-600">
            Kommunikation
          </p>
          <h1 class="mt-1 text-2xl font-semibold text-gray-950">
            Telefonie
          </h1>
          <p class="mt-2 max-w-3xl text-sm text-gray-600">
            Anrufe direkt in FEDEO starten und annehmen.
          </p>
        </div>

        <div class="flex flex-wrap gap-2">
          <UButton
            icon="i-heroicons-arrow-path"
            variant="soft"
            :loading="loading"
            @click="loadTelephony"
          >
            Aktualisieren
          </UButton>
          <UButton
            to="/communication/phone-setup"
            icon="i-heroicons-cog-6-tooth"
            variant="outline"
          >
            Telefonie-Setup
          </UButton>
          <UButton
            to="/communication/chat"
            icon="i-heroicons-chat-bubble-left-right"
            variant="outline"
          >
            Zum Chat
          </UButton>
        </div>
      </div>

      <UCard>
        <template #header>
          <div class="flex flex-col gap-3 lg:flex-row lg:items-center lg:justify-between">
            <div>
              <h2 class="text-base font-semibold text-gray-950">
                FEDEO Softphone
              </h2>
              <p class="mt-1 text-sm text-gray-500">
                Registrierung und Anrufe über den lokalen Asterisk.
              </p>
            </div>
            <UBadge
              :color="sipRegistered ? 'success' : 'neutral'"
              variant="soft"
              class="w-fit"
            >
              {{ sipStatus }}
            </UBadge>
          </div>
        </template>

        <div class="grid gap-4 lg:grid-cols-[0.9fr_1.1fr]">
          <div class="space-y-4">
            <div>
              <label class="text-sm font-medium text-gray-700">Nebenstelle</label>
              <div class="mt-2 grid gap-2 sm:grid-cols-2">
                <button
                  v-for="account in config?.testAccounts || []"
                  :key="account.extension"
                  type="button"
                  class="rounded-lg border px-4 py-3 text-left transition"
                  :class="selectedExtension === account.extension ? 'border-primary-500 bg-primary-50 text-primary-900' : 'border-gray-200 bg-white text-gray-700 hover:border-gray-300'"
                  :disabled="sipRegistered || sipLoading"
                  @click="selectedExtension = account.extension"
                >
                  <span class="block font-semibold">{{ account.extension }}</span>
                  <span class="mt-1 block text-xs opacity-75">{{ account.displayName }}</span>
                </button>
              </div>
            </div>

            <div class="grid gap-3 sm:grid-cols-2">
              <UButton
                icon="i-heroicons-phone-arrow-up-right"
                :loading="sipLoading && !sipRegistered"
                :disabled="!canRegisterSip || sipRegistered"
                @click="registerSip"
              >
                Registrieren
              </UButton>
              <UButton
                icon="i-heroicons-x-circle"
                color="neutral"
                variant="soft"
                :disabled="!sipRegistered || sipLoading"
                @click="stopSip"
              >
                Trennen
              </UButton>
            </div>
          </div>

          <div class="rounded-lg border border-gray-200 bg-white p-4">
            <div class="grid gap-3 sm:grid-cols-[1fr_auto]">
              <UInput
                v-model="dialTarget"
                icon="i-heroicons-hashtag"
                placeholder="Ziel, z. B. 600 oder 1002"
                :disabled="!sipRegistered || Boolean(activeSession)"
              />
              <UButton
                icon="i-heroicons-phone"
                :loading="sipLoading && sipRegistered"
                :disabled="!canStartCall"
                @click="startCall"
              >
                Anrufen
              </UButton>
            </div>

            <div class="mt-4 flex flex-wrap items-center gap-2">
              <UButton
                icon="i-heroicons-phone-x-mark"
                color="error"
                variant="soft"
                :disabled="!canHangup"
                @click="hangupCall"
              >
                Auflegen
              </UButton>
              <UBadge color="neutral" variant="soft">
                {{ callState === "active" ? "Aktiver Anruf" : callState === "connecting" ? "Verbindet" : callState === "incoming" ? "Eingehend" : "Bereit" }}
              </UBadge>
              <UBadge :color="sipRegistered ? 'success' : 'warning'" variant="soft">
                Registrierung: {{ registererState }}
              </UBadge>
            </div>

            <UAlert
              v-if="sipError"
              class="mt-4"
              color="error"
              icon="i-heroicons-exclamation-triangle"
              title="Telefoniefehler"
              :description="sipError"
            />

            <audio ref="remoteAudio" autoplay playsinline />
          </div>
        </div>
      </UCard>
    </div>
  </div>
</template>
