<script lang="ts">
	import { onMount } from 'svelte';
	import { api } from '$lib/api';
	import { t, getLocale } from '$lib/i18n';
	import { providersCache, targetsCache, trackingConfigsCache, templateConfigsCache } 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 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 { snackSuccess, snackError } from '$lib/stores/snackbar.svelte';
	import { highlightFromUrl } from '$lib/highlight';
	import type { Tracker, TrackingConfig, TemplateConfig } from '$lib/types';

	import TrackerForm from './TrackerForm.svelte';
	import LinkedTargetsSection from './LinkedTargetsSection.svelte';
	import SharedLinkModal from './SharedLinkModal.svelte';
	import TestMenu from './TestMenu.svelte';

	let loaded = $state(false);
	let loadError = $state('');
	let notificationTrackers = $state<Tracker[]>([]);
	let providers = $derived(providersCache.items);
	const providerItems = $derived(providers.map(p => ({ value: p.id, label: p.name, icon: p.icon || 'mdiServer', desc: p.type })));
	let targets = $derived(targetsCache.items);
	let trackingConfigs = $derived(trackingConfigsCache.items);
	let templateConfigs = $derived(templateConfigsCache.items);
	let collections = $state<any[]>([]);
	let showForm = $state(false);
	let editing = $state<number | null>(null);
	let collectionFilter = $state('');
	let submitting = $state(false);
	let confirmDelete = $state<Tracker | null>(null);
	let toggling = $state<Record<number, boolean>>({});
	let ttTesting = $state<Record<string, string>>({});

	// Shared link validation
	let linkWarning = $state<{ albums: any[], providerId: number } | null>(null);
	let linkCheckLoading = $state(false);
	let linkCreating = $state(false);
	let previousCollectionIds = $state<string[]>([]);

	// Tracker form
	const defaultForm = () => ({
		name: '', icon: '', provider_id: 0, collection_ids: [] as string[],
		scan_interval: 60, batch_duration: 0,
		filters: {} as Record<string, any>,
	});
	let form = $state(defaultForm());
	let selectedProviderType = $derived(
		providers.find(p => p.id === form.provider_id)?.type || ''
	);
	let error = $state('');

	// Linked targets management
	let expandedTracker = $state<number | null>(null);
	let addingTarget = $state<Record<number, boolean>>({});
	let newLinkTargetId = $state<Record<number, number>>({});
	let newLinkTrackingConfigId = $state<Record<number, number>>({});
	let newLinkTemplateConfigId = $state<Record<number, number>>({});

	// Test menu
	let testMenuOpen = $state<string | null>(null);
	let testMenuStyle = $state('');

	const immichTestTypes = [
		{ key: 'basic', icon: 'mdiSend', labelKey: 'notificationTracker.testBasic' },
		{ key: 'periodic', icon: 'mdiCalendarClock', labelKey: 'notificationTracker.testPeriodic' },
		{ key: 'scheduled', icon: 'mdiImageMultiple', labelKey: 'notificationTracker.testScheduled' },
		{ key: 'memory', icon: 'mdiHistory', labelKey: 'notificationTracker.testMemory' },
	];
	const defaultTestTypes = [
		{ key: 'basic', icon: 'mdiSend', labelKey: 'notificationTracker.testBasic' },
	];

	let testMenuTrackerId = $state<number | null>(null);
	let testTypes = $derived(() => {
		if (!testMenuTrackerId) return defaultTestTypes;
		const tracker = notificationTrackers.find(t => t.id === testMenuTrackerId);
		if (!tracker) return defaultTestTypes;
		const provider = providers.find(p => p.id === tracker.provider_id);
		if (provider?.type === 'immich') return immichTestTypes;
		return defaultTestTypes;
	});

	onMount(load);

	async function load() {
		loadError = '';
		try {
			[notificationTrackers] = await Promise.all([
				api('/notification-trackers'),
				providersCache.fetch(), targetsCache.fetch(),
				trackingConfigsCache.fetch(), templateConfigsCache.fetch(),
			]);
		} catch (err: any) {
			loadError = err.message || 'Failed to load data';
			snackError(loadError);
		} finally { loaded = true; highlightFromUrl(); }
	}

	async function loadCollections() {
		if (!form.provider_id) return;
		try { collections = await api(`/providers/${form.provider_id}/collections`); } catch { collections = []; }
	}

	let _prevProviderId = 0;
	$effect(() => {
		if (showForm && form.provider_id && form.provider_id !== _prevProviderId) {
			_prevProviderId = form.provider_id;
			loadCollections();
		}
	});

	function openNew() { form = defaultForm(); editing = null; showForm = true; collections = []; previousCollectionIds = []; }

	async function edit(trk: any) {
		form = {
			name: trk.name, icon: trk.icon || '', provider_id: trk.provider_id,
			collection_ids: [...(trk.collection_ids || [])],
			scan_interval: trk.scan_interval, batch_duration: trk.batch_duration ?? 0,
			filters: trk.filters || {},
		};
		previousCollectionIds = [...(trk.collection_ids || [])];
		editing = trk.id; showForm = true;
		if (form.provider_id) await loadCollections();
	}

	async function save(e: SubmitEvent) {
		e.preventDefault(); error = '';
		if (submitting) return;

		const newAlbumIds = form.collection_ids.filter(id => !previousCollectionIds.includes(id));
		if (newAlbumIds.length > 0 && form.provider_id) {
			linkCheckLoading = true;
			try {
				const missingAlbums: any[] = [];
				for (const albumId of newAlbumIds) {
					const links = await api(`/providers/${form.provider_id}/albums/${albumId}/shared-links`);
					const validLink = (links as any[]).find((l: any) => l.is_accessible && !l.is_expired);
					if (!validLink) {
						const album = collections.find(c => c.id === albumId);
						const problematicLink = (links as any[]).find((l: any) => l.is_expired || l.has_password);
						missingAlbums.push({
							id: albumId,
							name: album?.albumName || album?.name || albumId,
							issue: problematicLink
								? (problematicLink.is_expired ? 'expired' : 'password-protected')
								: 'missing',
						});
					}
				}
				if (missingAlbums.length > 0) {
					linkWarning = { albums: missingAlbums, providerId: form.provider_id };
					linkCheckLoading = false;
					return;
				}
			} catch { /* Proceed if check fails */ }
			linkCheckLoading = false;
		}

		await doSave();
	}

	async function doSave() {
		submitting = true;
		try {
			if (editing) {
				await api(`/notification-trackers/${editing}`, { method: 'PUT', body: JSON.stringify(form) });
				snackSuccess(t('snack.trackerUpdated'));
			} else {
				await api('/notification-trackers', { method: 'POST', body: JSON.stringify(form) });
				snackSuccess(t('snack.trackerCreated'));
			}
			showForm = false; editing = null; linkWarning = null; await load();
		} catch (err: any) { error = err.message; snackError(err.message); } finally { submitting = false; }
	}

	async function autoCreateLinks() {
		if (!linkWarning) return;
		linkCreating = true;
		let created = 0;
		for (const album of linkWarning.albums) {
			if (album.issue === 'missing') {
				try {
					await api(`/providers/${linkWarning.providerId}/albums/${album.id}/shared-links`, { method: 'POST' });
					created++;
				} catch (err: any) {
					snackError(`Failed to create link for "${album.name}": ${err.message}`);
				}
			}
		}
		if (created > 0) snackSuccess(`Created ${created} public link(s)`);
		linkWarning = null;
		linkCreating = false;
		await doSave();
	}

	async function dismissLinkWarning() {
		linkWarning = null;
		await doSave();
	}

	async function toggle(tracker: any) {
		if (toggling[tracker.id]) return;
		toggling = { ...toggling, [tracker.id]: true };
		try {
			await api(`/notification-trackers/${tracker.id}`, { method: 'PUT', body: JSON.stringify({ enabled: !tracker.enabled }) });
			await load();
			snackSuccess(tracker.enabled ? t('snack.trackerPaused') : t('snack.trackerResumed'));
		} catch (err: any) { snackError(err.message); } finally { toggling = { ...toggling, [tracker.id]: false }; }
	}

	function startDelete(tracker: any) { confirmDelete = tracker; }

	async function doDelete() {
		if (!confirmDelete) return;
		try {
			await api(`/notification-trackers/${confirmDelete.id}`, { method: 'DELETE' });
			await load();
			snackSuccess(t('snack.trackerDeleted'));
		} catch (err: any) { error = err.message; snackError(err.message); }
		confirmDelete = null;
	}

	function toggleCollection(collectionId: string) {
		form.collection_ids = form.collection_ids.includes(collectionId)
			? form.collection_ids.filter(id => id !== collectionId)
			: [...form.collection_ids, collectionId];
	}

	function formatDate(dateStr: string): string {
		if (!dateStr) return '';
		try {
			const d = new Date(dateStr);
			return d.toLocaleDateString(undefined, { month: 'short', day: 'numeric', year: 'numeric' });
		} catch { return ''; }
	}

	// --- Linked Targets helpers ---
	function toggleExpand(trackerId: number) {
		if (expandedTracker === trackerId) { expandedTracker = null; return; }
		expandedTracker = trackerId;
	}

	function getProviderType(tracker: any): string {
		const p = providers.find(p => p.id === tracker.provider_id);
		return p?.type || '';
	}

	function getProviderName(id: number): string {
		const p = providers.find(p => p.id === id);
		return p?.name || `#${id}`;
	}

	function configsForTracker(tracker: any, configs: (TrackingConfig | TemplateConfig)[]): any[] {
		const pt = getProviderType(tracker);
		return pt ? configs.filter((c: any) => c.provider_type === pt) : configs;
	}

	function getUnlinkedTargets(tracker: any): any[] {
		const linkedIds = new Set((tracker.tracker_targets || []).map((tt: any) => tt.target_id));
		return targets.filter(t => !linkedIds.has(t.id));
	}

	async function addTargetLink(trackerId: number) {
		const targetId = newLinkTargetId[trackerId];
		if (!targetId) return;
		addingTarget = { ...addingTarget, [trackerId]: true };
		try {
			await api(`/notification-trackers/${trackerId}/targets`, {
				method: 'POST',
				body: JSON.stringify({
					target_id: targetId,
					tracking_config_id: newLinkTrackingConfigId[trackerId] || null,
					template_config_id: newLinkTemplateConfigId[trackerId] || null,
				}),
			});
			newLinkTargetId[trackerId] = 0;
			newLinkTrackingConfigId[trackerId] = 0;
			newLinkTemplateConfigId[trackerId] = 0;
			await load();
			snackSuccess(t('snack.targetLinked'));
		} catch (err: any) { snackError(err.message); }
		addingTarget = { ...addingTarget, [trackerId]: false };
	}

	async function removeTargetLink(trackerId: number, ttId: number) {
		try {
			await api(`/notification-trackers/${trackerId}/targets/${ttId}`, { method: 'DELETE' });
			await load();
			snackSuccess(t('snack.targetUnlinked'));
		} catch (err: any) { snackError(err.message); }
	}

	async function updateTargetLink(trackerId: number, tt: any, field: string, value: any) {
		try {
			await api(`/notification-trackers/${trackerId}/targets/${tt.id}`, {
				method: 'PUT',
				body: JSON.stringify({ [field]: value }),
			});
			await load();
		} catch (err: any) { snackError(err.message); }
	}

	async function testTrackerTarget(trackerId: number, ttId: number, testType: string) {
		testMenuOpen = null;
		const key = `${ttId}_${testType}`;
		if (ttTesting[key]) return;
		ttTesting = { ...ttTesting, [key]: testType };
		try {
			await api(`/notification-trackers/${trackerId}/targets/${ttId}/test/${testType}?locale=${getLocale()}`, { method: 'POST' });
			snackSuccess(t('snack.targetTestSent'));
		} catch (err: any) {
			snackError(err.message);
		} finally {
			ttTesting = { ...ttTesting, [key]: '' };
		}
	}

	function openTestMenu(ttId: number, event: MouseEvent) {
		const btn = event.currentTarget as HTMLElement;
		const rect = btn.getBoundingClientRect();
		testMenuStyle = `position:fixed; z-index:9999; top:${rect.bottom + 4}px; right:${window.innerWidth - rect.right}px;`;
		testMenuTrackerId = notificationTrackers.find(t => t.tracker_targets?.some((x: any) => String(x.id) === String(ttId)))?.id ?? null;
		testMenuOpen = testMenuOpen === String(ttId) ? null : String(ttId);
	}

	function handleTestFromMenu(ttId: number, testType: string) {
		const trackerId = notificationTrackers.find(t => t.tracker_targets?.some((x: any) => String(x.id) === String(ttId)))?.id;
		if (trackerId) testTrackerTarget(trackerId, ttId, testType);
	}
</script>

<PageHeader title={t('notificationTracker.title')} description={t('notificationTracker.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('notificationTracker.cancel') : t('notificationTracker.newTracker')}
	</button>
</PageHeader>

{#if !loaded}
<Loading />
{:else if loadError}
	<Card>
		<div class="bg-[var(--color-error-bg)] text-[var(--color-error-fg)] text-sm rounded-md p-3">{loadError}</div>
	</Card>
{:else if showForm}
	<TrackerForm
		bind:form
		{providerItems}
		{collections}
		bind:collectionFilter
		{editing}
		{submitting}
		{linkCheckLoading}
		{error}
		providerType={selectedProviderType}
		onsave={save}
		ontoggleCollection={toggleCollection}
		{formatDate}
	/>
{/if}

{#if loaded && !loadError}
{#if notificationTrackers.length === 0 && !showForm}
	<Card>
		<EmptyState icon="mdiRadar" message={t('notificationTracker.noTrackers')} />
	</Card>
{:else if !showForm}
	<div class="space-y-3 stagger-children">
		{#each notificationTrackers as tracker}
			<Card hover entityId={tracker.id}>
				<div class="flex items-center justify-between">
					<div>
						<div class="flex items-center gap-2">
							<span style="color: var(--color-primary);"><MdiIcon name={tracker.icon || 'mdiRadar'} size={20} /></span>
							<p class="font-medium">{tracker.name}</p>
							<span class="text-xs px-1.5 py-0.5 rounded {tracker.enabled ? 'bg-[var(--color-success-bg)] text-[var(--color-success-fg)]' : 'bg-[var(--color-muted)] text-[var(--color-muted-foreground)]'}">
								{tracker.enabled ? t('notificationTracker.active') : t('notificationTracker.paused')}
							</span>
						<CrossLink href="/providers" icon="mdiServer" label={getProviderName(tracker.provider_id)} entityId={tracker.provider_id} />
						</div>
						<p class="text-sm text-[var(--color-muted-foreground)]">
							{(tracker.collection_ids || []).length} {t('notificationTracker.albums_count')} · {t('notificationTracker.every')} {tracker.scan_interval}s · {(tracker.tracker_targets || []).length} {t('notificationTracker.linkedTargets')}
						</p>
					</div>
					<div class="flex items-center gap-1 flex-wrap justify-end">
						<IconButton icon="mdiPencil" title={t('common.edit')} onclick={() => edit(tracker)} />
						<IconButton icon={tracker.enabled ? 'mdiPause' : 'mdiPlay'} title={tracker.enabled ? t('notificationTracker.pause') : t('notificationTracker.resume')} onclick={() => toggle(tracker)} disabled={toggling[tracker.id]} />
						<button onclick={() => toggleExpand(tracker.id)}
							class="text-xs text-[var(--color-muted-foreground)] hover:underline px-2 py-1">
							{t('notificationTracker.linkedTargets')} {expandedTracker === tracker.id ? '▲' : '▼'}
						</button>
						<IconButton icon="mdiDelete" title={t('notificationTracker.delete')} onclick={() => startDelete(tracker)} variant="danger" />
					</div>
				</div>

				{#if expandedTracker === tracker.id}
					<LinkedTargetsSection
						{tracker}
						{trackingConfigs}
						{templateConfigs}
						unlinkedTargets={getUnlinkedTargets(tracker)}
						newLinkTargetId={newLinkTargetId[tracker.id] || 0}
						newLinkTrackingConfigId={newLinkTrackingConfigId[tracker.id] || 0}
						newLinkTemplateConfigId={newLinkTemplateConfigId[tracker.id] || 0}
						addingTarget={addingTarget[tracker.id] || false}
						{ttTesting}
						configsForTracker={(configs) => configsForTracker(tracker, configs)}
						onupdateLink={(tt, field, value) => updateTargetLink(tracker.id, tt, field, value)}
						onremoveLink={(ttId) => removeTargetLink(tracker.id, ttId)}
						onaddLink={() => addTargetLink(tracker.id)}
						onopenTestMenu={openTestMenu}
						onchangeNewTarget={(v) => newLinkTargetId = { ...newLinkTargetId, [tracker.id]: v }}
						onchangeNewTrackingConfig={(v) => newLinkTrackingConfigId = { ...newLinkTrackingConfigId, [tracker.id]: v }}
						onchangeNewTemplateConfig={(v) => newLinkTemplateConfigId = { ...newLinkTemplateConfigId, [tracker.id]: v }}
					/>
				{/if}
			</Card>
		{/each}
	</div>
{/if}
{/if}

<TestMenu
	{testMenuOpen}
	{testMenuStyle}
	{ttTesting}
	testTypes={testTypes()}
	ontest={handleTestFromMenu}
	onclose={() => testMenuOpen = null}
/>

<SharedLinkModal
	{linkWarning}
	{linkCreating}
	onclose={() => { linkWarning = null; }}
	onautoCreate={autoCreateLinks}
	ondismiss={dismissLinkWarning}
/>

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