/**
 * Home Assistant Sources — CRUD, test, cards.
 */

import { _cachedHASources, haSourcesCache } from '../core/state.ts';
import { fetchWithAuth, escapeHtml } from '../core/api.ts';
import { t } from '../core/i18n.ts';
import { Modal } from '../core/modal.ts';
import { showToast, showConfirm } from '../core/ui.ts';
import { ICON_CLONE, ICON_EDIT, ICON_TEST } from '../core/icons.ts';
import * as P from '../core/icon-paths.ts';
import { wrapCard } from '../core/card-colors.ts';
import { TagInput, renderTagChips } from '../core/tag-input.ts';
import { loadPictureSources } from './streams.ts';
import type { HomeAssistantSource } from '../types.ts';

const ICON_HA = `<svg class="icon" viewBox="0 0 24 24">${P.home}</svg>`;

// ── Modal ──

let _haTagsInput: TagInput | null = null;

class HASourceModal extends Modal {
    constructor() { super('ha-source-modal'); }

    onForceClose() {
        if (_haTagsInput) { _haTagsInput.destroy(); _haTagsInput = null; }
    }

    snapshotValues() {
        return {
            name: (document.getElementById('ha-source-name') as HTMLInputElement).value,
            host: (document.getElementById('ha-source-host') as HTMLInputElement).value,
            token: (document.getElementById('ha-source-token') as HTMLInputElement).value,
            use_ssl: (document.getElementById('ha-source-ssl') as HTMLInputElement).checked,
            entity_filters: (document.getElementById('ha-source-filters') as HTMLInputElement).value,
            description: (document.getElementById('ha-source-description') as HTMLInputElement).value,
            tags: JSON.stringify(_haTagsInput ? _haTagsInput.getValue() : []),
        };
    }
}

const haSourceModal = new HASourceModal();

// ── Show / Close ──

export async function showHASourceModal(editData: HomeAssistantSource | null = null): Promise<void> {
    const isEdit = !!editData;
    const titleKey = isEdit ? 'ha_source.edit' : 'ha_source.add';
    document.getElementById('ha-source-modal-title')!.innerHTML = `${ICON_HA} ${t(titleKey)}`;
    (document.getElementById('ha-source-id') as HTMLInputElement).value = editData?.id || '';
    (document.getElementById('ha-source-error') as HTMLElement).style.display = 'none';

    if (isEdit) {
        (document.getElementById('ha-source-name') as HTMLInputElement).value = editData.name || '';
        (document.getElementById('ha-source-host') as HTMLInputElement).value = editData.host || '';
        (document.getElementById('ha-source-token') as HTMLInputElement).value = '';  // never expose token
        (document.getElementById('ha-source-ssl') as HTMLInputElement).checked = editData.use_ssl ?? false;
        (document.getElementById('ha-source-filters') as HTMLInputElement).value = (editData.entity_filters || []).join(', ');
        (document.getElementById('ha-source-description') as HTMLInputElement).value = editData.description || '';
    } else {
        (document.getElementById('ha-source-name') as HTMLInputElement).value = '';
        (document.getElementById('ha-source-host') as HTMLInputElement).value = '';
        (document.getElementById('ha-source-token') as HTMLInputElement).value = '';
        (document.getElementById('ha-source-ssl') as HTMLInputElement).checked = false;
        (document.getElementById('ha-source-filters') as HTMLInputElement).value = '';
        (document.getElementById('ha-source-description') as HTMLInputElement).value = '';
    }

    // Tags
    if (_haTagsInput) { _haTagsInput.destroy(); _haTagsInput = null; }
    _haTagsInput = new TagInput(document.getElementById('ha-source-tags-container'), { placeholder: t('tags.placeholder') });
    _haTagsInput.setValue(isEdit ? (editData.tags || []) : []);

    // Show/hide test button based on edit mode
    const testBtn = document.getElementById('ha-source-test-btn');
    if (testBtn) testBtn.style.display = isEdit ? '' : 'none';

    // Token hint
    const tokenHint = document.getElementById('ha-source-token-hint');
    if (tokenHint) tokenHint.style.display = isEdit ? '' : 'none';

    haSourceModal.open();
    haSourceModal.snapshot();
}

export async function closeHASourceModal(): Promise<void> {
    await haSourceModal.close();
}

// ── Save ──

export async function saveHASource(): Promise<void> {
    const id = (document.getElementById('ha-source-id') as HTMLInputElement).value;
    const name = (document.getElementById('ha-source-name') as HTMLInputElement).value.trim();
    const host = (document.getElementById('ha-source-host') as HTMLInputElement).value.trim();
    const token = (document.getElementById('ha-source-token') as HTMLInputElement).value.trim();
    const use_ssl = (document.getElementById('ha-source-ssl') as HTMLInputElement).checked;
    const filtersRaw = (document.getElementById('ha-source-filters') as HTMLInputElement).value.trim();
    const entity_filters = filtersRaw ? filtersRaw.split(',').map(s => s.trim()).filter(Boolean) : [];
    const description = (document.getElementById('ha-source-description') as HTMLInputElement).value.trim() || null;

    if (!name) {
        haSourceModal.showError(t('ha_source.error.name_required'));
        return;
    }
    if (!id && !host) {
        haSourceModal.showError(t('ha_source.error.host_required'));
        return;
    }
    if (!id && !token) {
        haSourceModal.showError(t('ha_source.error.token_required'));
        return;
    }

    const payload: Record<string, any> = {
        name, use_ssl, entity_filters, description,
        tags: _haTagsInput ? _haTagsInput.getValue() : [],
    };
    // Only send host/token if provided (edit mode may leave token blank)
    if (host) payload.host = host;
    if (token) payload.token = token;

    try {
        const method = id ? 'PUT' : 'POST';
        const url = id ? `/home-assistant/sources/${id}` : '/home-assistant/sources';
        const resp = await fetchWithAuth(url, {
            method,
            headers: { 'Content-Type': 'application/json' },
            body: JSON.stringify(payload),
        });
        if (!resp.ok) {
            const err = await resp.json().catch(() => ({}));
            throw new Error(err.detail || `HTTP ${resp.status}`);
        }
        showToast(t(id ? 'ha_source.updated' : 'ha_source.created'), 'success');
        haSourceModal.forceClose();
        haSourcesCache.invalidate();
        await loadPictureSources();
    } catch (e: any) {
        if (e.isAuth) return;
        haSourceModal.showError(e.message);
    }
}

// ── Edit / Clone / Delete ──

export async function editHASource(sourceId: string): Promise<void> {
    try {
        const resp = await fetchWithAuth(`/home-assistant/sources/${sourceId}`);
        if (!resp.ok) throw new Error(t('ha_source.error.load'));
        const data = await resp.json();
        await showHASourceModal(data);
    } catch (e: any) {
        if (e.isAuth) return;
        showToast(e.message, 'error');
    }
}

export async function cloneHASource(sourceId: string): Promise<void> {
    try {
        const resp = await fetchWithAuth(`/home-assistant/sources/${sourceId}`);
        if (!resp.ok) throw new Error(t('ha_source.error.load'));
        const data = await resp.json();
        delete data.id;
        data.name = data.name + ' (copy)';
        await showHASourceModal(data);
    } catch (e: any) {
        if (e.isAuth) return;
        showToast(e.message, 'error');
    }
}

export async function deleteHASource(sourceId: string): Promise<void> {
    const confirmed = await showConfirm(t('ha_source.delete.confirm'));
    if (!confirmed) return;
    try {
        const resp = await fetchWithAuth(`/home-assistant/sources/${sourceId}`, { method: 'DELETE' });
        if (!resp.ok) {
            const err = await resp.json().catch(() => ({}));
            throw new Error(err.detail || `HTTP ${resp.status}`);
        }
        showToast(t('ha_source.deleted'), 'success');
        haSourcesCache.invalidate();
        await loadPictureSources();
    } catch (e: any) {
        if (e.isAuth) return;
        showToast(e.message, 'error');
    }
}

// ── Test ──

export async function testHASource(): Promise<void> {
    const id = (document.getElementById('ha-source-id') as HTMLInputElement).value;
    if (!id) return;

    const testBtn = document.getElementById('ha-source-test-btn');
    if (testBtn) testBtn.classList.add('loading');

    try {
        const resp = await fetchWithAuth(`/home-assistant/sources/${id}/test`, { method: 'POST' });
        if (!resp.ok) throw new Error(`HTTP ${resp.status}`);
        const data = await resp.json();
        if (data.success) {
            showToast(`${t('ha_source.test.success')} | HA ${data.ha_version} | ${data.entity_count} entities`, 'success');
        } else {
            showToast(`${t('ha_source.test.failed')}: ${data.error}`, 'error');
        }
    } catch (e: any) {
        if (e.isAuth) return;
        showToast(e.message, 'error');
    } finally {
        if (testBtn) testBtn.classList.remove('loading');
    }
}

// ── Card rendering ──

export function createHASourceCard(source: HomeAssistantSource) {
    let healthClass: string, healthTitle: string;
    if (source.connected) {
        healthClass = 'health-online';
        healthTitle = `${t('ha_source.connected')} — ${source.entity_count} entities`;
    } else {
        healthClass = 'health-offline';
        healthTitle = t('ha_source.disconnected');
    }
    const statusDot = `<span class="health-dot ${healthClass}" title="${escapeHtml(healthTitle)}" role="status"></span>`;

    return wrapCard({
        type: 'template-card',
        dataAttr: 'data-id',
        id: source.id,
        removeOnclick: `deleteHASource('${source.id}')`,
        removeTitle: t('common.delete'),
        content: `
            <div class="template-card-header">
                <div class="template-name">${ICON_HA} ${statusDot} ${escapeHtml(source.name)}</div>
            </div>
            <div class="stream-card-props">
                <span class="stream-card-prop">
                    <svg class="icon" viewBox="0 0 24 24">${P.wifi}</svg> ${escapeHtml(source.host)}
                </span>
                ${source.connected ? `<span class="stream-card-prop">
                    <svg class="icon" viewBox="0 0 24 24">${P.listChecks}</svg> ${source.entity_count} entities
                </span>` : ''}
                ${source.use_ssl ? `<span class="stream-card-prop">
                    <svg class="icon" viewBox="0 0 24 24">${P.lock}</svg> SSL
                </span>` : ''}
            </div>
            ${renderTagChips(source.tags)}
            ${source.description ? `<div class="template-config" style="opacity:0.7;">${escapeHtml(source.description)}</div>` : ''}`,
        actions: `
                <button class="btn btn-icon btn-secondary" data-action="test" title="${t('ha_source.test')}">${ICON_TEST}</button>
                <button class="btn btn-icon btn-secondary" data-action="clone" title="${t('common.clone')}">${ICON_CLONE}</button>
                <button class="btn btn-icon btn-secondary" data-action="edit" title="${t('common.edit')}">${ICON_EDIT}</button>`,
    });
}

// ── Event delegation ──

const _haSourceActions: Record<string, (id: string) => void> = {
    test: (id) => _testHASourceFromCard(id),
    clone: cloneHASource,
    edit: editHASource,
};

async function _testHASourceFromCard(sourceId: string): Promise<void> {
    try {
        const resp = await fetchWithAuth(`/home-assistant/sources/${sourceId}/test`, { method: 'POST' });
        if (!resp.ok) throw new Error(`HTTP ${resp.status}`);
        const data = await resp.json();
        if (data.success) {
            showToast(`HA ${data.ha_version} | ${data.entity_count} entities`, 'success');
        } else {
            showToast(`${t('ha_source.test.failed')}: ${data.error}`, 'error');
        }
    } catch (e: any) {
        if (e.isAuth) return;
        showToast(e.message, 'error');
    }
}

export function initHASourceDelegation(container: HTMLElement): void {
    container.addEventListener('click', (e: MouseEvent) => {
        const btn = (e.target as HTMLElement).closest<HTMLElement>('[data-action]');
        if (!btn) return;

        const section = btn.closest<HTMLElement>('[data-card-section="ha-sources"]');
        if (!section) return;
        const card = btn.closest<HTMLElement>('[data-id]');
        if (!card) return;

        const action = btn.dataset.action;
        const id = card.getAttribute('data-id');
        if (!action || !id) return;

        const handler = _haSourceActions[action];
        if (handler) {
            e.stopPropagation();
            handler(id);
        }
    });
}

// ── Expose to global scope for HTML template onclick handlers ──

window.showHASourceModal = showHASourceModal;
window.closeHASourceModal = closeHASourceModal;
window.saveHASource = saveHASource;
window.editHASource = editHASource;
window.cloneHASource = cloneHASource;
window.deleteHASource = deleteHASource;
window.testHASource = testHASource;
