<template>
  <div class="min-h-screen bg-base-100 p-4 md:p-6">
    <BreadcrumbHeader />

    <div class="max-w-3xl mx-auto">
      <div class="mb-6 text-center md:text-left">
        <h1 class="text-2xl font-semibold text-base-content">
          Create New API Key
        </h1>
        <p class="text-sm text-base-content/70 mt-1">
          Enter the API key's details below.
        </p>
      </div>

      <div v-if="error" role="alert" class="alert shadow-lg bg-rose-100">
        <svg xmlns="http://www.w3.org/2000/svg" class="h-6 w-6 shrink-0 stroke-current" fill="none" viewBox="0 0 24 24">
          <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2"
            d="M10 14l2-2m0 0l2-2m-2 2l-2-2m2 2l2 2m7-2a9 9 0 11-18 0 9 9 0 0118 0z" />
        </svg>
        <div>
          <span>Error! {{ error }}</span>
        </div>
        <button class="btn btn-sm" @click="error = null">X</button>
      </div>

      <div class="card border border-base-300/40 shadow-sm">
        <form @submit.prevent="createApiKey" class="card-body space-y-5">
          <div class="space-y-4">
            <h2 class="text-base font-medium text-base-content border-b border-base-300/30 pb-2 mb-4">
              Basic Information
            </h2>
            <div class="grid grid-cols-1 md:grid-cols-2 gap-x-4 gap-y-4">
              <div class="form-control">
                <label for="name" class="label">
                  <span class="label-text">
                    Name <span class="text-red-500">*</span>
                  </span>
                </label>
                <input id="name" type="text" v-model="newApiKey.name" placeholder="API Key Name"
                  class="input input-sm input-bordered w-full" required />
              </div>

              <div class="form-control">
                <label for="apitype" class="label">
                  <span class="label-text">
                    Type <span class="text-red-500">*</span>
                  </span>
                </label>
                <div class="relative">
                  <select id="apitype" v-model="newApiKey.type" class="select select-sm select-bordered w-full pl-10"
                    required>
                    <option class="disabled" value="" selected>Select API Type</option>
                    <option value="openai">OpenAI</option>
                    <option value="claude">Claude</option>
                    <option value="gemini">Gemini</option>
                    <option value="openai-compatible">OpenAI Compatible</option>
                  </select>
                  <button type="button" @click="togglePasswordVisibility" tabindex="-1"
                    class="absolute inset-y-0 left-0 px-3 flex items-center text-base-content/60 hover:text-base-content/80 focus:outline-none focus:ring-0 rounded-r-md"
                    id="password-visibility-toggle">
                    <template v-if="newApiKey.type === 'openai'">
                      <img src="../../assets/openai.svg" class="w-5 h-5" alt="">
                    </template>
                    <template v-else-if="newApiKey.type === 'claude'">
                      <img src="../../assets/claude.svg" class="w-5 h-5" alt="">
                    </template>
                    <template v-else-if="newApiKey.type === 'gemini'">
                      <img src="../../assets/gemini.svg" class="w-5 h-5" alt="">
                    </template>
                    <template v-else="newApiKey.type">
                      <img src="../../assets/logo.svg" class="w-5 h-5" alt="">
                    </template>
                  </button>
                </div>
              </div>

              <div class="form-control">
                <label for="apikey" class="label">
                  <span class="label-text">
                    API Key <span class="text-red-500">*</span>
                  </span>
                </label>
                <input id="apikey" type="text" v-model="newApiKey.apikey" placeholder="Your API Key"
                  class="input input-sm input-bordered w-full" required />
              </div>

              <div class="form-control">
                <label for="endpoint" class="label">
                  <span class="label-text">Endpoint</span>
                </label>
                <input id="endpoint" type="text" v-model="newApiKey.endpoint" placeholder="API Endpoint URL"
                  class="input input-sm input-bordered w-full" />
              </div>
            </div>
          </div>

          <div class="collapse collapse-arrow">
            <input type="checkbox" v-model="showAdvancedOptions" class="min-h-0 py-2" />
            <div class="collapse-title text-base font-medium min-h-0 py-1 px-0">
              Advanced Options
            </div>
            <div class="collapse-content px-0">
              <div class="grid grid-cols-1 md:grid-cols-2 gap-x-4 gap-y-4 pt-3">
                <div class="form-control">
                  <label for="resource_name" class="label">
                    <span class="label-text">Resource Name</span>
                  </label>
                  <input id="resource_name" type="text" v-model="newApiKey.resource_name" placeholder="Resource Name"
                    class="input input-sm input-bordered w-full" />
                </div>

                <div class="form-control">
                  <label for="deployment_name" class="label">
                    <span class="label-text">Deployment Name</span>
                  </label>
                  <input id="deployment_name" type="text" v-model="newApiKey.deployment_name"
                    placeholder="Deployment Name" class="input input-sm input-bordered w-full" />
                </div>

                <div class="form-control">
                  <label for="api_secret" class="label">
                    <span class="label-text">API Secret</span>
                  </label>
                  <input id="api_secret" type="text" v-model="newApiKey.api_secret" placeholder="API Secret"
                    class="input input-sm input-bordered w-full" />
                </div>

                <div class="form-control">
                  <label for="model_prefix" class="label">
                    <span class="label-text">Model Prefix</span>
                  </label>
                  <input id="model_prefix" type="text" v-model="newApiKey.model_prefix" placeholder="Model Prefix"
                    class="input input-sm input-bordered w-full" />
                </div>

                <div class="form-control">
                  <label for="model_alias" class="label">
                    <span class="label-text">Model Alias</span>
                  </label>
                  <textarea id="model_alias" v-model="newApiKey.model_alias" placeholder="{}"
                    class="textarea textarea-sm textarea-bordered w-full"></textarea>
                </div>

                <div class="form-control">
                  <label for="parameters" class="label">
                    <span class="label-text">Parameters (JSON)</span>
                  </label>
                  <textarea id="parameters" v-model="newApiKey.parameters" placeholder="{}"
                    class="textarea textarea-sm textarea-bordered w-full"></textarea>
                </div>

                <div class="md:col-span-2 form-control">
                  <label for="support_models" class="label">
                    <span class="label-text">Support Models</span>
                  </label>
                  <!-- <textarea id="support_models" v-model="newApiKey.support_models_text"
                    placeholder='["model1", "model2"]' class="textarea textarea-sm textarea-bordered w-full"></textarea> -->
                  <el-input-tag v-model="newApiKey.support_models_array" :trigger="'Enter'" clearable
                    placeholder="Please input" @change="onchange_supportmodel"/>
                </div>

                <div class="form-control">
                  <label class="label">
                    <span class="label-text">Status</span>
                  </label>
                  <div class="flex items-center space-x-3 h-9">
                    <input type="checkbox" v-model="newApiKey.active"
                      :class="`toggle toggle-sm ${newApiKey.active ? 'toggle-success' : 'toggle-error'}`" />
                    <span class="text-sm text-base-content/90">
                      {{ newApiKey.active ? 'Active' : 'Inactive' }}
                    </span>
                  </div>
                </div>
              </div>
            </div>
          </div>
          
          <div class="flex justify-end pt-4 items-center gap-2">
            <button @click="cancel" class="btn btn-sm btn-outline">Cancel</button>
            <button type="submit" class="btn btn-outline btn-sm px-4 text-sm font-medium btn-success" :disabled="!isFormValid">
              Create
            </button>
          </div>
        </form>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, computed, inject } from 'vue'
import { useRouter } from 'vue-router'
import { useKeyStore } from '@/stores/key';
import BreadcrumbHeader from '@/components/dashboard/BreadcrumbHeader.vue';

const router = useRouter()
const keyStore = useKeyStore()
const { setToast } = inject('toast')
const error = ref(null)

// Control advanced options visibility
const showAdvancedOptions = ref(false)

// Initialize API key object
const newApiKey = ref({
  name: '',
  type: '',
  apikey: '',
  active: true,
  endpoint: '',
  resource_name: '',
  deployment_name: '',
  api_secret: '',
  model_prefix: '',
  model_alias: '',
  parameters: '{}',
  support_models: '',
  support_models_array: [],
})

const resetNewApiKey = () => {
  newApiKey.value = {
    name: '',
    type: '',
    apikey: '',
    active: true,
    endpoint: '',
    resource_name: '',
    deployment_name: '',
    api_secret: '',
    model_prefix: '',
    model_alias: '',
    parameters: '{}',
    support_models: '',
    support_models_array: [],
  }
}

const onchange_supportmodel = () => {
  newApiKey.value.support_models = JSON.stringify(newApiKey.value.support_models_array)
}

// Form validation
const isFormValid = computed(() => {
  return newApiKey.value.name &&
    newApiKey.value.type &&
    newApiKey.value.apikey
})

const cancel = () => {
  resetNewApiKey()
  emit('closeModal', true)
}

const createApiKey = async () => {
  if (!isFormValid.value) {
    setToast('Please fill in all required fields (Name, Type, API Key).', 'error')
    return
  }
  
  try {
    try {
      if (!Array.isArray(newApiKey.value.support_models_array)) {
        setToast('Support Models must be a JSON array.', 'error');
        return;
      }
    } catch (e) {
      setToast('Invalid JSON format for Support Models.', 'error');
      return;
    }

    // Attempt to parse parameters JSON
    try {
      JSON.parse(newApiKey.value.parameters);
    } catch (e) {
      setToast('Invalid JSON format for Parameters.', 'error');
      return;
    }

    const res = await keyStore.createKey(newApiKey.value);
    if (res.data?.code === 200) {
      error.value = null;
      resetNewApiKey();
      setToast('API Key created successfully', 'success')
      // Optionally navigate or reset form
      emit('closeModal', true)
    } else {
      setToast(res.error || res.data?.message || 'Failed to create API Key', 'error')
    }
  } catch (err) {
    console.log('createApiKey error:', err)
    error.value = err || 'Failed to create API Key'
    // setToast(error.response?.data?.error || 'Failed to create API Key', 'error')
  }
}


const emit = defineEmits(['closeModal'])

</script>

<style scoped>
/* Minimal custom styles if absolutely necessary */
.collapse .collapse-title {
  min-height: 0;
  padding-top: 0.5rem;
  padding-bottom: 0.5rem;
}
</style>