{% extends "/layouts/main.twig" %}
{% set active_menu = '/admin/settings' %}

{% set xdata = 'settings' %}
{% block title p__('title', 'xAI / Grok Configuration') %}

{% block template %}
	<div>
		{% include "snippets/back.twig" with {link: 'admin/settings', label: 'Settings'} %}

		<h1 class="mt-4">xAI / Grok</h1>
	</div>

	<x-form>
		<form class="flex flex-col gap-8" @submit.prevent="submit" x-ref="form">
			<div class="flex flex-col gap-2">
				<section class="flex flex-col gap-6 box" data-density="comfortable">
					<h2>{{ p__('heading', 'API') }}</h2>

					<div>
						<label for="xai.api_key">
							{{ p__('label', 'API Key') }}
						</label>

						<div class="relative mt-2" x-data="{isVisible: false}">
							<input class="pe-11 input" id="xai.api_key" name="xai[api_key]" :type="isVisible ? 'text' : 'password'" autocomplete="off" placeholder="{{ __('Include your xAI API key')|e('html_attr') }}" value="{{ environment == 'demo' ? 'hidden-in-demo' : option.xai.api_key ?? '' }}" required>

							<button type="button" class="absolute end-3 top-1/2 text-2xl -translate-y-1/2 text-content-dimmed" @click="isVisible = !isVisible">
								<i class="block ti" :class="{'ti-eye-closed' : isVisible, 'ti-eye':!isVisible}"></i>
							</button>
						</div>


						<ul class="info mt-2">
							<li>
								{{ __("Can't find your API Key?") }}

								<a href="https://console.x.ai/" target="_blank" class="text-content hover:underline">
									{{ p__('button', 'Click here') }}
								</a>
							</li>
						</ul>
					</div>
				</section>

				<section class="flex flex-col gap-6 box" data-density="comfortable">
					<h2>{{ p__('heading', 'Webhooks') }}</h2>

					<div x-data="{isVisible: false}">
						<div class="flex items-end gap-2 justify-between">
							<label for="xai.webhook_secret">
								<span class="text-failure">*</span>
								{{ p__('label', 'Webhook secret') }}
							</label>

							<button type="button" class="button button-outline button-xs" @click="
								$refs.webhookSecret.value = Array.from(crypto.getRandomValues(new Uint8Array(32)), b => b.toString(36).padStart(2, '0')).join('').slice(0, 32);
								$refs.webhookSecret.dispatchEvent(new Event('input', { bubbles: true }));
								isVisible = true;
							">
								{{ p__('button', 'Generate') }}
							</button>
						</div>

						<div class="relative mt-2">
							<input class="pe-11 input" :type="isVisible ? 'text' : 'password'" id="xai.webhook_secret" name="xai[webhook_secret]" autocomplete="new-password" value="{{ environment == 'demo' ? 'hidden-in-demo' : option.xai.webhook_secret ?? '' }}" x-ref="webhookSecret" required/>

							<button type="button" class="absolute end-3 top-1/2 text-2xl -translate-y-1/2 text-content-dimmed" @click="isVisible = !isVisible">
								<i class="block ti" :class="{'ti-eye-closed' : isVisible, 'ti-eye':!isVisible}"></i>
							</button>
						</div>

						<ul class="info mt-2">
							<li>
								{{ __('Required for video generation. Used to secure the webhook URL where xAI uploads completed videos.') }}
							</li>
						</ul>
					</div>
				</section>
			</div>

			<div class="flex gap-4 justify-end">
				<a href="admin/settings" class="button button-outline">
					{{ p__('button', 'Cancel') }}
				</a>

				<button class="button button-accent" type="submit" :processing="isProcessing">
					{% include "/snippets/spinner.twig" %}

					{{ p__('button', 'Save changes') }}
				</button>
			</div>
		</form>
	</x-form>
{% endblock %}
