<script lang="ts">
	import { onMount } from 'svelte';
	import { slide } from 'svelte/transition';
	import { api } from '$lib/api';
	import { t } from '$lib/i18n';
	import { providersCache, telegramBotsCache, commandConfigsCache } from '$lib/stores/caches.svelte';
	import PageHeader from '$lib/components/PageHeader.svelte';
	import Card from '$lib/components/Card.svelte';
	import Loading from '$lib/components/Loading.svelte';
	import IconPicker from '$lib/components/IconPicker.svelte';
	import MdiIcon from '$lib/components/MdiIcon.svelte';
	import EmptyState from '$lib/components/EmptyState.svelte';
	import ConfirmModal from '$lib/components/ConfirmModal.svelte';
	import IconButton from '$lib/components/IconButton.svelte';
	import CrossLink from '$lib/components/CrossLink.svelte';
	import EntitySelect from '$lib/components/EntitySelect.svelte';
	import { snackSuccess, snackError } from '$lib/stores/snackbar.svelte';
	import { highlightFromUrl } from '$lib/highlight';
	import { globalProviderFilter } from '$lib/stores/provider-filter.svelte';
	import { providerDefaultIcon } from '$lib/grid-items';
	import type { ServiceProvider, TelegramBot } from '$lib/types';

	let allCmdTrackers = $state<any[]>([]);
	let filterText = $state('');
	let filterProviderId = $state(0);
	let effectiveProviderId = $derived(globalProviderFilter.id || filterProviderId);
	let trackers = $derived(allCmdTrackers.filter((t: any) =>
		(!filterText || t.name.toLowerCase().includes(filterText.toLowerCase())) &&
		(!effectiveProviderId || t.provider_id === effectiveProviderId)
	));
	let providers = $derived(providersCache.items);
	let commandConfigs = $derived(commandConfigsCache.items);
	let telegramBots = $derived(telegramBotsCache.items);
	const providerItems = $derived(providers.map(p => ({ value: p.id, label: p.name, icon: providerDefaultIcon(p), desc: p.type })));
	const configItems = $derived(filteredConfigs().map((c: any) => ({ value: c.id, label: c.name, icon: c.icon || 'mdiCog', desc: c.provider_type })));
	const botItems = $derived(telegramBots.map(b => ({ value: b.id, label: b.name, icon: b.icon || 'mdiRobot', desc: b.bot_username ? `@${b.bot_username}` : '' })));
	let loaded = $state(false);
	let showForm = $state(false);
	let editing = $state<number | null>(null);
	let error = $state('');
	let submitting = $state(false);
	let confirmDelete = $state<any>(null);
	let toggling = $state<Record<number, boolean>>({});

	// Listeners per tracker
	let listeners = $state<Record<number, any[]>>({});
	let listenersLoading = $state<Record<number, boolean>>({});
	let expandedTracker = $state<number | null>(null);
	let addingListener = $state<Record<number, boolean>>({});
	let newListenerBotId = $state<Record<number, number>>({});

	const defaultForm = () => ({
		name: '',
		icon: '',
		provider_id: 0,
		command_config_id: 0,
		enabled: true,
	});
	let form = $state(defaultForm());

	// Filter command configs by selected provider's type
	let filteredConfigs = $derived(() => {
		if (!form.provider_id) return commandConfigs;
		const provider = providers.find(p => p.id === form.provider_id);
		if (!provider) return commandConfigs;
		return commandConfigs.filter(c => c.provider_type === provider.type);
	});

	onMount(load);
	async function load() {
		try {
			[allCmdTrackers] = await Promise.all([
				api('/command-trackers'),
				providersCache.fetch(), commandConfigsCache.fetch(),
				telegramBotsCache.fetch(),
			]);
		} catch (err: any) { error = err.message || t('common.loadError'); snackError(error); }
		finally { loaded = true; highlightFromUrl(); }
	}

	function openNew() { form = defaultForm(); editing = null; showForm = true; }
	function editTracker(trk: any) {
		form = {
			name: trk.name,
			icon: trk.icon || '',
			provider_id: trk.provider_id,
			command_config_id: trk.command_config_id,
			enabled: trk.enabled,
		};
		editing = trk.id;
		showForm = true;
	}

	async function saveTracker(e: SubmitEvent) {
		e.preventDefault(); error = ''; submitting = true;
		try {
			const body = JSON.stringify(form);
			if (editing) {
				await api(`/command-trackers/${editing}`, { method: 'PUT', body });
				snackSuccess(t('snack.commandTrackerUpdated'));
			} else {
				await api('/command-trackers', { method: 'POST', body });
				snackSuccess(t('snack.commandTrackerCreated'));
			}
			form = defaultForm(); showForm = false; editing = null; await load();
		} catch (err: any) { error = err.message; snackError(err.message); }
		finally { submitting = false; }
	}

	function remove(trk: any) {
		confirmDelete = {
			id: trk.id,
			onconfirm: async () => {
				try {
					await api(`/command-trackers/${trk.id}`, { method: 'DELETE' });
					await load();
					snackSuccess(t('snack.commandTrackerDeleted'));
				} catch (err: any) { snackError(err.message); }
				finally { confirmDelete = null; }
			}
		};
	}

	async function toggleEnabled(trk: any) {
		toggling = { ...toggling, [trk.id]: true };
		try {
			const endpoint = trk.enabled ? 'disable' : 'enable';
			await api(`/command-trackers/${trk.id}/${endpoint}`, { method: 'POST' });
			snackSuccess(trk.enabled ? t('snack.commandTrackerDisabled') : t('snack.commandTrackerEnabled'));
			await load();
		} catch (err: any) { snackError(err.message); }
		toggling = { ...toggling, [trk.id]: false };
	}

	function toggleListeners(trkId: number) {
		if (expandedTracker === trkId) {
			expandedTracker = null;
			return;
		}
		expandedTracker = trkId;
		loadListeners(trkId);
	}

	async function loadListeners(trkId: number) {
		listenersLoading = { ...listenersLoading, [trkId]: true };
		try {
			listeners = { ...listeners, [trkId]: await api(`/command-trackers/${trkId}/listeners`) };
		} catch { listeners = { ...listeners, [trkId]: [] }; }
		listenersLoading = { ...listenersLoading, [trkId]: false };
	}

	async function addListener(trkId: number) {
		const botId = newListenerBotId[trkId];
		if (!botId) return;
		addingListener = { ...addingListener, [trkId]: true };
		try {
			await api(`/command-trackers/${trkId}/listeners`, {
				method: 'POST',
				body: JSON.stringify({ listener_type: 'telegram_bot', listener_id: botId }),
			});
			snackSuccess(t('snack.listenerAdded'));
			await loadListeners(trkId);
			newListenerBotId = { ...newListenerBotId, [trkId]: 0 };
		} catch (err: any) { snackError(err.message); }
		addingListener = { ...addingListener, [trkId]: false };
	}

	async function removeListener(trkId: number, listenerId: number) {
		try {
			await api(`/command-trackers/${trkId}/listeners/${listenerId}`, { method: 'DELETE' });
			snackSuccess(t('snack.listenerRemoved'));
			await loadListeners(trkId);
		} catch (err: any) { snackError(err.message); }
	}

	function providerName(id: number): string {
		return providers.find(p => p.id === id)?.name || '?';
	}
	function configName(id: number): string {
		return commandConfigs.find(c => c.id === id)?.name || '?';
	}
</script>

<PageHeader title={t('commandTracker.title')} description={t('commandTracker.description')}>
	<button onclick={() => { showForm ? (showForm = false, editing = null) : openNew(); }}
		class="px-3 py-1.5 bg-[var(--color-primary)] text-[var(--color-primary-foreground)] rounded-md text-sm font-medium hover:opacity-90">
		{showForm ? t('common.cancel') : t('commandTracker.newTracker')}
	</button>
</PageHeader>

{#if !loaded}<Loading />{:else}

{#if showForm}
	<Card class="mb-6">
		{#if error}<div class="bg-[var(--color-error-bg)] text-[var(--color-error-fg)] text-sm rounded-md p-3 mb-4">{error}</div>{/if}
		<form onsubmit={saveTracker} class="space-y-3">
			<div>
				<label for="trk-name" class="block text-sm font-medium mb-1">{t('commandTracker.name')}</label>
				<div class="flex gap-2">
					<IconPicker value={form.icon} onselect={(v: string) => form.icon = v} />
					<input id="trk-name" bind:value={form.name} required placeholder={t('commandTracker.namePlaceholder')}
						class="flex-1 px-3 py-2 border border-[var(--color-border)] rounded-md text-sm bg-[var(--color-background)]" />
				</div>
			</div>

			<div>
				<label class="block text-sm font-medium mb-1">{t('commandTracker.provider')}</label>
				<EntitySelect items={providerItems} bind:value={form.provider_id} placeholder={t('commandTracker.selectProvider')} />
			</div>

			<div>
				<label class="block text-sm font-medium mb-1">{t('commandTracker.commandConfig')}</label>
				<EntitySelect items={configItems} bind:value={form.command_config_id} placeholder={t('commandTracker.selectCommandConfig')} />
			</div>

			<button type="submit" disabled={submitting}
				class="px-4 py-2 bg-[var(--color-primary)] text-[var(--color-primary-foreground)] rounded-md text-sm font-medium hover:opacity-90 disabled:opacity-50">
				{submitting ? t('common.loading') : (editing ? t('common.save') : t('common.create'))}
			</button>
		</form>
	</Card>
{/if}

{#if !showForm && allCmdTrackers.length > 0}
<div class="flex items-center gap-2 mb-3">
	<input type="text" bind:value={filterText} placeholder={t('common.filterByName')}
		class="flex-1 px-3 py-1.5 border border-[var(--color-border)] rounded-md text-sm bg-[var(--color-background)]" />
	{#if !globalProviderFilter.id}
	<div class="w-48">
		<EntitySelect items={[{value: 0, label: t('common.allProviders'), icon: 'mdiFilterOff'}, ...providerItems]} bind:value={filterProviderId} placeholder={t('common.allProviders')} />
	</div>
	{/if}
</div>
{/if}

{#if allCmdTrackers.length === 0 && !showForm}
	<Card>
		<EmptyState icon="mdiConsoleLine" message={t('commandTracker.noTrackers')} />
	</Card>
{:else if trackers.length === 0 && !showForm}
	<Card>
		<EmptyState icon="mdiFilterOff" message={t('common.noFilterResults')} />
	</Card>
{:else}
	<div class="space-y-3 stagger-children">
		{#each trackers as trk}
			<Card hover entityId={trk.id}>
				<div class="flex items-center justify-between">
					<div>
						<div class="flex items-center gap-2">
							<span style="color: var(--color-primary);"><MdiIcon name={trk.icon || 'mdiConsoleLine'} size={20} /></span>
							<p class="font-medium">{trk.name}</p>
							<CrossLink href="/providers" icon="mdiServer" label={providerName(trk.provider_id)} entityId={trk.provider_id} />
							<CrossLink href="/command-configs" icon="mdiCog" label={configName(trk.command_config_id)} entityId={trk.command_config_id} />
							<span class="text-xs px-1.5 py-0.5 rounded font-mono {trk.enabled
								? 'bg-emerald-500/10 text-emerald-500'
								: 'bg-red-500/10 text-red-500'}">
								{trk.enabled ? t('commandTracker.enabled') : t('commandTracker.disabled')}
							</span>
						</div>
						{#if trk.listener_count !== undefined}
							<p class="text-xs text-[var(--color-muted-foreground)] mt-0.5">
								{trk.listener_count} {t('commandTracker.listeners').toLowerCase()}
							</p>
						{/if}
					</div>
					<div class="flex items-center gap-1">
						<IconButton icon="mdiPencil" title={t('common.edit')} onclick={() => editTracker(trk)} />
						<IconButton icon={trk.enabled ? 'mdiPause' : 'mdiPlay'} title={trk.enabled ? t('notificationTracker.pause') : t('notificationTracker.resume')} onclick={() => toggleEnabled(trk)} disabled={toggling[trk.id]} />
						<button onclick={() => toggleListeners(trk.id)}
							class="text-xs text-[var(--color-muted-foreground)] hover:underline px-2 py-1">
							{t('commandTracker.listeners')} {expandedTracker === trk.id ? '▲' : '▼'}
						</button>
						<IconButton icon="mdiDelete" title={t('common.delete')} onclick={() => remove(trk)} variant="danger" />
					</div>
				</div>

				<!-- Listeners section -->
				{#if expandedTracker === trk.id}
					<div class="mt-3 border-t border-[var(--color-border)] pt-3" transition:slide>
						{#if listenersLoading[trk.id]}
							<p class="text-xs text-[var(--color-muted-foreground)]">{t('common.loading')}</p>
						{:else if (listeners[trk.id] || []).length === 0}
							<p class="text-xs text-[var(--color-muted-foreground)]">{t('commandTracker.noListeners')}</p>
						{:else}
							<div class="space-y-1">
								{#each listeners[trk.id] as listener}
									<div class="flex items-center justify-between text-sm px-2 py-1 rounded hover:bg-[var(--color-muted)]">
										<div class="flex items-center gap-2">
											<MdiIcon name="mdiRobot" size={14} />
											<CrossLink href="/bots?tab=telegram" icon="mdiRobot" label={listener.name || listener.listener_type} entityId={listener.listener_id} />
											<span class="text-xs px-1.5 py-0.5 rounded bg-blue-500/10 text-blue-500 font-mono">{listener.listener_type}</span>
										</div>
										<IconButton icon="mdiClose" title={t('commandTracker.removeListener')} size={14}
											onclick={() => removeListener(trk.id, listener.id)} variant="danger" />
									</div>
								{/each}
							</div>
						{/if}

						<!-- Add listener -->
						<div class="flex items-center gap-2 mt-2">
							<div class="flex-1">
								<EntitySelect items={botItems} bind:value={newListenerBotId[trk.id]} placeholder={t('commandTracker.selectBot')} />
							</div>
							<button onclick={() => addListener(trk.id)} disabled={!newListenerBotId[trk.id] || addingListener[trk.id]}
								class="text-xs px-3 py-1 bg-[var(--color-primary)] text-[var(--color-primary-foreground)] rounded-md hover:opacity-90 disabled:opacity-50">
								{addingListener[trk.id] ? t('common.loading') : t('commandTracker.addListener')}
							</button>
						</div>
					</div>
				{/if}
			</Card>
		{/each}
	</div>
{/if}

{/if}

<ConfirmModal open={confirmDelete !== null} message={t('commandTracker.confirmDelete')}
	onconfirm={() => confirmDelete?.onconfirm()} oncancel={() => confirmDelete = null} />
