<template>
  <Settings :title="$t('extensions')" :description="$t('extensions_description')" docs-url="docs/usage/extensions">
    <form @submit.prevent="saveExtensions">
      <InputField :label="$t('extensions_signatures_public_key')">
        <pre class="code-box">{{ signaturePublicKey }}</pre>
        <template #description>
          {{ $t('extensions_signatures_public_key_description') }}
        </template>
      </InputField>
      <InputField :label="$t('config_extension_endpoint')" docs-url="docs/usage/extensions/configuration-extension">
        <TextField v-model="extensions.config_extension_endpoint" :placeholder="$t('extension_endpoint_placeholder')" />

        <Checkbox
          v-model="extensions.config_extension_exclusive"
          class="pt-3"
          :label="$t('config_extension_exclusive')"
          :description="$t('config_extension_exclusive_desc')"
        />

        <Checkbox
          v-model="extensions.config_extension_netrc"
          :label="$t('extension_netrc')"
          :description="$t('extension_netrc_desc')"
        />
      </InputField>

      <InputField :label="$t('registry_extension_endpoint')" docs-url="docs/usage/extensions/registry-extension">
        <TextField
          v-model="extensions.registry_extension_endpoint"
          :placeholder="$t('extension_endpoint_placeholder')"
        />

        <Checkbox
          v-model="extensions.registry_extension_netrc"
          class="pt-3"
          :label="$t('extension_netrc')"
          :description="$t('extension_netrc_desc')"
        />
      </InputField>

      <InputField :label="$t('secret_extension_endpoint')" docs-url="docs/usage/extensions/secret-extension">
        <TextField v-model="extensions.secret_extension_endpoint" :placeholder="$t('extension_endpoint_placeholder')" />

        <Checkbox
          v-model="extensions.secret_extension_netrc"
          class="pt-3"
          :label="$t('extension_netrc')"
          :description="$t('extension_netrc_desc')"
        />
      </InputField>
      <Button :is-loading="isSaving" color="green" type="submit" :text="$t('save')" />
    </form>
  </Settings>
</template>

<script lang="ts" setup>
import { inject, onMounted, ref } from 'vue';
import type { Ref } from 'vue';
import { useI18n } from 'vue-i18n';

import Button from '~/components/atomic/Button.vue';
import Checkbox from '~/components/form/Checkbox.vue';
import InputField from '~/components/form/InputField.vue';
import TextField from '~/components/form/TextField.vue';
import Settings from '~/components/layout/Settings.vue';
import useApiClient from '~/compositions/useApiClient';
import { useAsyncAction } from '~/compositions/useAsyncAction';
import useNotifications from '~/compositions/useNotifications';
import type { ExtensionSettings, Repo } from '~/lib/api/types';

const i18n = useI18n();

const apiClient = useApiClient();
const notifications = useNotifications();

const repo = inject<Ref<Repo>>('repo');
if (!repo) {
  throw new Error('Missing repo');
}

const signaturePublicKey = ref<string>();

onMounted(async () => {
  signaturePublicKey.value = await apiClient.getSignaturePublicKey();
});

const extensions = ref<ExtensionSettings>({
  config_extension_endpoint: repo.value.config_extension_endpoint,
  config_extension_exclusive: repo.value.config_extension_exclusive,
  config_extension_netrc: repo.value.config_extension_netrc,
  registry_extension_endpoint: repo.value.registry_extension_endpoint,
  registry_extension_netrc: repo.value.registry_extension_netrc,
  secret_extension_endpoint: repo.value.secret_extension_endpoint,
  secret_extension_netrc: repo.value.secret_extension_netrc,
});

const { doSubmit: saveExtensions, isLoading: isSaving } = useAsyncAction(async () => {
  await apiClient.updateRepo(repo.value.id, extensions.value);

  // await loadRepo();
  notifications.notify({ title: i18n.t('extensions_configuration_saved'), type: 'success' });
});
</script>
