<script lang="ts">
	import { t } from '$lib/i18n';
	import MdiIcon from '$lib/components/MdiIcon.svelte';
	import Hint from '$lib/components/Hint.svelte';

	interface Props {
		webhookSecret: string;
		cacheTtlHours: string;
		cacheMaxEntries: string;
	}

	let {
		webhookSecret = $bindable(),
		cacheTtlHours = $bindable(),
		cacheMaxEntries = $bindable(),
	}: Props = $props();

	let showSecret = $state(false);

	const secretSet = $derived(!!webhookSecret && webhookSecret.length > 0);
	const ttlHours = $derived(Number(cacheTtlHours || '0'));
	const ttlIsOff = $derived(ttlHours <= 0);

	function ttlHumanized(h: number): string {
		if (h <= 0) return t('settings.ttlNoExpiry');
		if (h < 24) return `${h}h`;
		const d = Math.round(h / 24);
		if (d < 7) return `${d}d`;
		const w = Math.round(d / 7);
		if (w < 8) return `${w}w`;
		const mo = Math.round(d / 30);
		return `${mo}mo`;
	}
</script>

<section class="tg glass">
	<header class="tg-head">
		<div class="tg-eyebrow">
			<MdiIcon name="mdiSend" size={12} />
			<span>{t('settings.telegram')}</span>
		</div>
		<h3 class="tg-title">{t('settings.telegramHeadline')}</h3>
	</header>

	<div class="tg-grid">
		<!-- Webhook secret column -->
		<div class="col">
			<div class="col-head">
				<span class="col-num">A</span>
				<span class="col-name">
					{t('settings.webhookSecret')}
					<Hint text={t('settings.webhookSecretHint')} />
				</span>
				<span class="col-status" data-state={secretSet ? 'set' : 'unset'}>
					<span class="dot"></span>
					{secretSet ? t('settings.secretSet') : t('settings.secretUnset')}
				</span>
			</div>
			<form class="secret-field" onsubmit={(e) => e.preventDefault()} autocomplete="off">
				<input
					bind:value={webhookSecret}
					type={showSecret ? 'text' : 'password'}
					autocomplete="off"
					placeholder={t('providers.optional')}
					class="secret-input"
				/>
				<button
					type="button"
					class="secret-toggle"
					onclick={() => (showSecret = !showSecret)}
					aria-label={showSecret ? t('settings.hide') : t('settings.show')}
					title={showSecret ? t('settings.hide') : t('settings.show')}
				>
					<MdiIcon name={showSecret ? 'mdiEyeOff' : 'mdiEye'} size={14} />
				</button>
			</form>
		</div>

		<!-- Cache config column -->
		<div class="col">
			<div class="col-head">
				<span class="col-num">B</span>
				<span class="col-name">{t('settings.cacheConfig')}</span>
			</div>
			<div class="cache-grid">
				<label class="num-field">
					<span class="num-label">
						{t('settings.cacheTtlShort')}
						<Hint text={t('settings.cacheTtlHint')} />
					</span>
					<div class="num-row">
						<input
							bind:value={cacheTtlHours}
							type="number"
							min="0"
							max="8760"
							class="num-input"
						/>
						<span class="num-suffix">{t('settings.hoursShort')}</span>
					</div>
					<span class="num-meta" class:num-meta-off={ttlIsOff}>
						{ttlHumanized(ttlHours)}
					</span>
				</label>

				<label class="num-field">
					<span class="num-label">
						{t('settings.cacheMaxShort')}
						<Hint text={t('settings.cacheMaxEntriesHint')} />
					</span>
					<div class="num-row">
						<input
							bind:value={cacheMaxEntries}
							type="number"
							min="100"
							max="100000"
							class="num-input"
						/>
						<span class="num-suffix">{t('settings.entriesShort')}</span>
					</div>
					<span class="num-meta">
						{t('settings.cacheMaxFootnote')}
					</span>
				</label>
			</div>
		</div>
	</div>
</section>

<style>
	.tg {
		padding: 1.5rem 1.6rem 1.4rem;
		display: flex;
		flex-direction: column;
		gap: 1.15rem;
		min-height: 100%;
	}
	.tg-head {
		position: relative;
		z-index: 1;
	}
	.tg-eyebrow {
		display: inline-flex;
		align-items: center;
		gap: 0.35rem;
		font-family: var(--font-mono);
		font-size: 0.62rem;
		text-transform: uppercase;
		letter-spacing: 0.18em;
		color: var(--color-muted-foreground);
		margin-bottom: 0.45rem;
	}
	.tg-title {
		margin: 0;
		font-family: var(--font-display);
		font-style: italic;
		font-weight: 400;
		font-size: 1.15rem;
		line-height: 1.35;
		letter-spacing: -0.015em;
		color: var(--color-foreground);
		max-width: 36ch;
	}

	.tg-grid {
		position: relative;
		z-index: 1;
		display: grid;
		grid-template-columns: 1fr;
		gap: 1.25rem;
	}
	@media (min-width: 720px) {
		.tg-grid { grid-template-columns: 1fr 1fr; gap: 1.6rem; }
	}

	.col {
		display: flex;
		flex-direction: column;
		gap: 0.55rem;
	}
	.col-head {
		display: flex;
		align-items: center;
		gap: 0.55rem;
		flex-wrap: wrap;
	}
	.col-num {
		font-family: var(--font-mono);
		font-size: 0.62rem;
		letter-spacing: 0.18em;
		color: var(--color-muted-foreground);
		text-transform: uppercase;
	}
	.col-name {
		font-size: 0.78rem;
		font-weight: 500;
		color: var(--color-foreground);
		display: inline-flex;
		align-items: center;
	}
	.col-status {
		margin-left: auto;
		display: inline-flex;
		align-items: center;
		gap: 0.35rem;
		font-family: var(--font-mono);
		font-size: 0.6rem;
		text-transform: uppercase;
		letter-spacing: 0.14em;
		padding: 0.2rem 0.55rem;
		border-radius: 999px;
		border: 1px solid var(--color-border);
		background: var(--color-glass-strong);
		color: var(--color-muted-foreground);
	}
	.col-status .dot {
		width: 6px; height: 6px;
		border-radius: 50%;
		background: var(--color-muted-foreground);
	}
	.col-status[data-state="set"] {
		color: var(--color-mint);
		border-color: color-mix(in srgb, var(--color-mint) 35%, var(--color-border));
		background: color-mix(in srgb, var(--color-mint) 8%, var(--color-glass-strong));
	}
	.col-status[data-state="set"] .dot {
		background: var(--color-mint);
		box-shadow: 0 0 8px color-mix(in srgb, var(--color-mint) 60%, transparent);
	}

	/* --- Secret field --- */
	.secret-field {
		display: flex;
		align-items: center;
		gap: 0.25rem;
		padding: 0.15rem 0.35rem 0.15rem 0.7rem;
		border: 1px solid var(--color-rule-strong);
		border-radius: 0.625rem;
		background: var(--color-input-bg);
		transition: border-color 0.18s, box-shadow 0.18s;
	}
	.secret-field:focus-within {
		border-color: var(--color-primary);
		box-shadow: 0 0 0 3px var(--color-glow);
	}
	.secret-input {
		flex: 1;
		background: transparent;
		border: 0;
		outline: 0;
		padding: 0.5rem 0.4rem;
		font-family: var(--font-mono);
		font-size: 0.82rem;
		color: var(--color-foreground);
		min-width: 0;
		letter-spacing: 0.05em;
	}
	.secret-toggle {
		display: inline-flex;
		align-items: center;
		justify-content: center;
		width: 28px; height: 28px;
		border-radius: 8px;
		background: transparent;
		border: 0;
		color: var(--color-muted-foreground);
		cursor: pointer;
		transition: background 0.15s, color 0.15s;
	}
	.secret-toggle:hover {
		background: var(--color-glass-strong);
		color: var(--color-foreground);
	}

	/* --- Cache config grid --- */
	.cache-grid {
		display: grid;
		grid-template-columns: 1fr 1fr;
		gap: 0.7rem;
	}
	.num-field {
		display: flex;
		flex-direction: column;
		gap: 0.3rem;
		padding: 0.7rem 0.85rem 0.65rem;
		border-radius: 12px;
		border: 1px solid var(--color-border);
		background: var(--color-glass-strong);
	}
	.num-label {
		font-family: var(--font-mono);
		font-size: 0.6rem;
		text-transform: uppercase;
		letter-spacing: 0.14em;
		color: var(--color-muted-foreground);
		display: inline-flex;
		align-items: center;
	}
	.num-row {
		display: flex;
		align-items: baseline;
		gap: 0.35rem;
	}
	.num-input {
		width: 100%;
		padding: 0.1rem 0;
		border: 0;
		background: transparent;
		font-family: var(--font-mono);
		font-size: 1.4rem;
		font-weight: 500;
		font-variant-numeric: tabular-nums;
		color: var(--color-foreground);
		letter-spacing: -0.015em;
		line-height: 1.1;
		outline: none;
		appearance: textfield;
		-moz-appearance: textfield;
	}
	.num-input::-webkit-outer-spin-button,
	.num-input::-webkit-inner-spin-button {
		-webkit-appearance: none;
		margin: 0;
	}
	.num-suffix {
		font-family: var(--font-mono);
		font-size: 0.65rem;
		color: var(--color-muted-foreground);
		text-transform: uppercase;
		letter-spacing: 0.14em;
	}
	.num-meta {
		font-size: 0.7rem;
		color: var(--color-mint);
		font-family: var(--font-mono);
	}
	.num-meta-off {
		color: var(--color-citrus);
	}

	@media (max-width: 480px) {
		.cache-grid { grid-template-columns: 1fr; }
	}
</style>
