import { FolkShape } from "./folk-shape";
import { css, html } from "./tags";
import { extractArtifactToCanvas, extractBtnCss } from "./extract-artifact";

const styles = css`
	:host {
		background: var(--rs-bg-surface, #fff);
		color: var(--rs-text-primary, #1e293b);
		border-radius: 8px;
		box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
		min-width: 380px;
		min-height: 460px;
	}

	.header {
		display: flex;
		align-items: center;
		justify-content: space-between;
		padding: 8px 12px;
		background: linear-gradient(135deg, #059669, #34d399);
		color: white;
		border-radius: 8px 8px 0 0;
		font-size: 12px;
		font-weight: 600;
		cursor: move;
	}

	.header-title {
		display: flex;
		align-items: center;
		gap: 6px;
	}

	.header-actions {
		display: flex;
		gap: 4px;
	}

	.header-actions button {
		background: transparent;
		border: none;
		color: white;
		cursor: pointer;
		padding: 2px 6px;
		border-radius: 4px;
		font-size: 14px;
	}

	.header-actions button:hover {
		background: rgba(255, 255, 255, 0.2);
	}

	.wrapper {
		height: 100%;
		overflow: hidden;
		display: flex;
		flex-direction: column;
	}

	.content {
		display: flex;
		flex-direction: column;
		flex: 1;
		min-height: 0;
		overflow: hidden;
	}

	.prompt-area {
		padding: 12px;
		border-bottom: 1px solid #e2e8f0;
	}

	.prompt-input {
		width: 100%;
		padding: 10px 12px;
		border: 2px solid #e2e8f0;
		border-radius: 8px;
		font-size: 13px;
		resize: none;
		outline: none;
		font-family: inherit;
	}

	.prompt-input:focus {
		border-color: #059669;
	}

	.component-input {
		width: 100%;
		padding: 8px 10px;
		border: 2px solid #e2e8f0;
		border-radius: 6px;
		font-size: 12px;
		outline: none;
		margin-top: 8px;
		font-family: inherit;
	}

	.component-input:focus {
		border-color: #059669;
	}

	.controls {
		display: flex;
		gap: 8px;
		margin-top: 8px;
	}

	.generate-btn {
		flex: 1;
		padding: 8px 16px;
		background: linear-gradient(135deg, #059669, #34d399);
		color: white;
		border: none;
		border-radius: 6px;
		font-size: 13px;
		font-weight: 600;
		cursor: pointer;
		transition: opacity 0.2s;
	}

	.generate-btn:hover {
		opacity: 0.9;
	}

	.generate-btn:disabled {
		opacity: 0.5;
		cursor: not-allowed;
	}

	.tabs {
		display: flex;
		border-bottom: 1px solid #e2e8f0;
	}

	.tab {
		flex: 1;
		padding: 8px;
		text-align: center;
		font-size: 12px;
		font-weight: 600;
		color: #64748b;
		border: none;
		background: none;
		cursor: pointer;
		border-bottom: 2px solid transparent;
		transition: all 0.2s;
	}

	.tab:hover {
		color: #059669;
	}

	.tab.active {
		color: #059669;
		border-bottom-color: #059669;
	}

	.preview-area {
		flex: 1;
		overflow: auto;
		padding: 12px;
		min-height: 0;
		display: flex;
		flex-direction: column;
	}

	.preview-area img {
		max-width: 100%;
		max-height: 100%;
		object-fit: contain;
		border-radius: 8px;
		box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
	}

	.drc-results {
		font-size: 12px;
		color: #334155;
	}

	.drc-results .pass {
		color: #059669;
		font-weight: 600;
	}

	.drc-results .fail {
		color: #ef4444;
		font-weight: 600;
	}

	.export-row {
		display: flex;
		gap: 8px;
		padding: 8px 12px;
		border-top: 1px solid #e2e8f0;
		flex-wrap: wrap;
	}

	.export-btn {
		padding: 6px 12px;
		background: #334155;
		color: white;
		border: none;
		border-radius: 6px;
		font-size: 12px;
		cursor: pointer;
		text-decoration: none;
	}

	.export-btn:hover {
		background: #475569;
	}

	.export-btn.primary {
		background: #059669;
	}

	.export-btn.primary:hover {
		background: #047857;
	}

	${extractBtnCss}

	.placeholder {
		flex: 1;
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
		color: #94a3b8;
		text-align: center;
		gap: 8px;
	}

	.placeholder-icon {
		font-size: 48px;
		opacity: 0.5;
	}

	.loading {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
		padding: 24px;
		gap: 12px;
	}

	.spinner {
		width: 32px;
		height: 32px;
		border: 3px solid #e2e8f0;
		border-top-color: #059669;
		border-radius: 50%;
		animation: spin 1s linear infinite;
	}

	@keyframes spin {
		to { transform: rotate(360deg); }
	}

	.error {
		color: #ef4444;
		padding: 12px;
		background: #fef2f2;
		border-radius: 6px;
		font-size: 13px;
	}
`;

declare global {
	interface HTMLElementTagNameMap {
		"folk-kicad": FolkKiCAD;
	}
}

export class FolkKiCAD extends FolkShape {
	static override tagName = "folk-kicad";

	static {
		const sheet = new CSSStyleSheet();
		const parentRules = Array.from(FolkShape.styles.cssRules)
			.map((r) => r.cssText)
			.join("\n");
		const childRules = Array.from(styles.cssRules)
			.map((r) => r.cssText)
			.join("\n");
		sheet.replaceSync(`${parentRules}\n${childRules}`);
		this.styles = sheet;
	}

	#isLoading = false;
	#error: string | null = null;
	#prompt: string | null = null;
	#components: string[] = [];
	#schematicSvg: string | null = null;
	#boardSvg: string | null = null;
	#gerberUrl: string | null = null;
	#bomUrl: string | null = null;
	#pdfUrl: string | null = null;
	#drcResults: any = null;
	#activeTab: "schematic" | "board" | "drc" = "schematic";
	#promptInput: HTMLTextAreaElement | null = null;
	#componentInput: HTMLInputElement | null = null;
	#generateBtn: HTMLButtonElement | null = null;
	#previewArea: HTMLElement | null = null;
	#exportRow: HTMLElement | null = null;

	override createRenderRoot() {
		const root = super.createRenderRoot();

		const wrapper = document.createElement("div");
		wrapper.className = "wrapper";
		wrapper.innerHTML = html`
			<div class="header">
				<span class="header-title">
					<span>🔌</span>
					<span>KiCAD PCB</span>
				</span>
				<div class="header-actions">
					<button class="close-btn" title="Close">×</button>
				</div>
			</div>
			<div class="content">
				<div class="prompt-area">
					<textarea class="prompt-input" placeholder="Describe the PCB design...\ne.g. 'ESP32-based sensor board with BME280, OLED display, and USB-C'" rows="2"></textarea>
					<input class="component-input" type="text" placeholder="Components (comma-separated): ESP32-WROOM, BME280, SSD1306..." />
					<div class="controls">
						<button class="generate-btn">🔌 Generate PCB</button>
					</div>
				</div>
				<div class="tabs">
					<button class="tab active" data-tab="schematic">📋 Schematic</button>
					<button class="tab" data-tab="board">📟 Board</button>
					<button class="tab" data-tab="drc">✅ DRC</button>
				</div>
				<div class="preview-area">
					<div class="placeholder">
						<span class="placeholder-icon">🔌</span>
						<span>Describe a PCB design and click Generate</span>
					</div>
				</div>
				<div class="export-row" style="display:none">
					<a class="export-btn primary gerber-dl" href="#" download>⬇️ Gerber</a>
					<a class="export-btn bom-dl" href="#" download>📋 BOM</a>
					<a class="export-btn pdf-dl" href="#" download>📄 PDF</a>
				</div>
			</div>
		`;

		const slot = root.querySelector("slot");
		const containerDiv = slot?.parentElement as HTMLElement;
		if (containerDiv) {
			containerDiv.replaceWith(wrapper);
		}

		this.#promptInput = wrapper.querySelector(".prompt-input");
		this.#componentInput = wrapper.querySelector(".component-input");
		this.#generateBtn = wrapper.querySelector(".generate-btn");
		this.#previewArea = wrapper.querySelector(".preview-area");
		this.#exportRow = wrapper.querySelector(".export-row");
		const closeBtn = wrapper.querySelector(".close-btn") as HTMLButtonElement;

		// Tab switching
		wrapper.querySelectorAll(".tab").forEach((tab) => {
			tab.addEventListener("click", (e) => {
				e.stopPropagation();
				const tabName = (tab as HTMLElement).dataset.tab as "schematic" | "board" | "drc";
				this.#activeTab = tabName;
				wrapper.querySelectorAll(".tab").forEach((t) => t.classList.remove("active"));
				tab.classList.add("active");
				this.#renderPreview();
			});
		});

		this.#generateBtn?.addEventListener("click", (e) => {
			e.stopPropagation();
			this.#generate();
		});

		this.#promptInput?.addEventListener("keydown", (e) => {
			if (e.key === "Enter" && !e.shiftKey) {
				e.preventDefault();
				this.#generate();
			}
		});
		this.#promptInput?.addEventListener("pointerdown", (e) => e.stopPropagation());
		this.#componentInput?.addEventListener("pointerdown", (e) => e.stopPropagation());

		closeBtn.addEventListener("click", (e) => {
			e.stopPropagation();
			this.dispatchEvent(new CustomEvent("close"));
		});

		// Restore persisted state
		if (this.#prompt && this.#promptInput) this.#promptInput.value = this.#prompt;
		if (this.#components.length && this.#componentInput) this.#componentInput.value = this.#components.join(", ");
		if (this.#schematicSvg || this.#boardSvg || this.#drcResults) {
			this.#renderPreview();
			this.#showExports();
		}

		// Extract button — delegated click
		this.#previewArea?.addEventListener("click", (e) => {
			const btn = (e.target as HTMLElement).closest(".extract-btn") as HTMLElement;
			if (!btn) return;
			e.stopPropagation();
			const mediaType = (btn.dataset.media || "image") as "image" | "pdf";
			extractArtifactToCanvas({ url: btn.dataset.url!, mediaType, title: btn.dataset.title, sourceShape: this });
		});

		// Health check — only disable if endpoint unreachable (sidecar starts on demand)
		fetch("/api/kicad/health").then(r => r.json()).then((h: any) => {
			if (this.#generateBtn && h.available === false && h.status && !h.status.includes("on demand")) {
				this.#generateBtn.title = h.error || h.status || "KiCAD MCP server unavailable";
			}
		}).catch(() => {});

		return root;
	}

	async #generate() {
		const prompt = this.#promptInput?.value.trim();
		if (!prompt || this.#isLoading) return;

		const components = this.#componentInput?.value
			.split(",")
			.map((c) => c.trim())
			.filter(Boolean) || [];

		this.#prompt = prompt;
		this.#components = components;
		this.#isLoading = true;
		this.#error = null;
		if (this.#generateBtn) this.#generateBtn.disabled = true;

		if (this.#previewArea) {
			this.#previewArea.innerHTML = '<div class="loading"><div class="spinner"></div><span>Designing PCB with AI... (may take 30-60s)</span></div>';
		}

		try {
			const createRes = await fetch("/api/kicad/generate", {
				method: "POST",
				headers: { "Content-Type": "application/json" },
				body: JSON.stringify({ prompt, components }),
			});

			if (!createRes.ok) {
				const err = await createRes.json().catch(() => ({ error: createRes.statusText }));
				throw new Error(err.error || "PCB generation failed");
			}

			const data = await createRes.json();
			this.#schematicSvg = data.schematic_svg || null;
			this.#boardSvg = data.board_svg || null;
			this.#gerberUrl = data.gerber_url || null;
			this.#bomUrl = data.bom_url || null;
			this.#pdfUrl = data.pdf_url || null;
			this.#drcResults = data.drc || null;

			this.#renderPreview();
			this.#showExports();
		} catch (err) {
			this.#error = err instanceof Error ? err.message : "Generation failed";
			if (this.#previewArea) {
				this.#previewArea.innerHTML = `<div class="error">${this.#escapeHtml(this.#error)}</div>`;
			}
		} finally {
			this.#isLoading = false;
			if (this.#generateBtn) this.#generateBtn.disabled = false;
		}
	}

	#renderPreview() {
		if (!this.#previewArea) return;

		switch (this.#activeTab) {
			case "schematic":
				if (this.#schematicSvg) {
					this.#previewArea.innerHTML = `<button class="extract-btn" data-url="${this.#escapeHtml(this.#schematicSvg)}" data-title="Schematic" data-media="image" title="Extract to canvas">↗</button><img src="${this.#escapeHtml(this.#schematicSvg)}" alt="Schematic" />`;
				} else {
					this.#previewArea.innerHTML = '<div class="placeholder"><span class="placeholder-icon">📋</span><span>Schematic will appear here</span></div>';
				}
				break;
			case "board":
				if (this.#boardSvg) {
					this.#previewArea.innerHTML = `<button class="extract-btn" data-url="${this.#escapeHtml(this.#boardSvg)}" data-title="Board Layout" data-media="image" title="Extract to canvas">↗</button><img src="${this.#escapeHtml(this.#boardSvg)}" alt="Board Layout" />`;
				} else {
					this.#previewArea.innerHTML = '<div class="placeholder"><span class="placeholder-icon">📟</span><span>Board layout will appear here</span></div>';
				}
				break;
			case "drc":
				if (this.#drcResults) {
					const violations = this.#drcResults.violations || [];
					const passed = violations.length === 0;
					this.#previewArea.innerHTML = `
						<div class="drc-results" style="padding:12px">
							<h4 style="margin:0 0 8px">${passed
								? '<span class="pass">✅ DRC Passed</span>'
								: `<span class="fail">❌ ${violations.length} Violation(s)</span>`
							}</h4>
							${violations.map((v: any) => `<p style="margin:4px 0;font-size:12px">• ${this.#escapeHtml(v.message || v)}</p>`).join("")}
						</div>
					`;
				} else {
					this.#previewArea.innerHTML = '<div class="placeholder"><span class="placeholder-icon">✅</span><span>DRC results will appear here</span></div>';
				}
				break;
		}
	}

	#showExports() {
		if (this.#exportRow && (this.#gerberUrl || this.#bomUrl || this.#pdfUrl)) {
			this.#exportRow.style.display = "flex";
			const gerberLink = this.#exportRow.querySelector(".gerber-dl") as HTMLAnchorElement;
			const bomLink = this.#exportRow.querySelector(".bom-dl") as HTMLAnchorElement;
			const pdfLink = this.#exportRow.querySelector(".pdf-dl") as HTMLAnchorElement;
			if (gerberLink) gerberLink.href = this.#gerberUrl || "#";
			if (bomLink) bomLink.href = this.#bomUrl || "#";
			if (pdfLink) pdfLink.href = this.#pdfUrl || "#";
		}
	}

	#escapeHtml(text: string): string {
		const div = document.createElement("div");
		div.textContent = text;
		return div.innerHTML;
	}

	static override fromData(data: Record<string, any>): FolkKiCAD {
		const shape = FolkShape.fromData(data) as FolkKiCAD;
		if (data.prompt) shape.#prompt = data.prompt;
		if (data.components) shape.#components = data.components;
		if (data.schematicSvg) shape.#schematicSvg = data.schematicSvg;
		if (data.boardSvg) shape.#boardSvg = data.boardSvg;
		if (data.gerberUrl) shape.#gerberUrl = data.gerberUrl;
		if (data.bomUrl) shape.#bomUrl = data.bomUrl;
		if (data.pdfUrl) shape.#pdfUrl = data.pdfUrl;
		if (data.drcResults) shape.#drcResults = data.drcResults;
		return shape;
	}

	override toJSON() {
		return {
			...super.toJSON(),
			type: "folk-kicad",
			prompt: this.#prompt,
			components: this.#components,
			schematicSvg: this.#schematicSvg,
			boardSvg: this.#boardSvg,
			gerberUrl: this.#gerberUrl,
			bomUrl: this.#bomUrl,
			pdfUrl: this.#pdfUrl,
			drcResults: this.#drcResults,
		};
	}

	override applyData(data: Record<string, any>): void {
		super.applyData(data);
		if ("prompt" in data) this.#prompt = data.prompt;
		if ("components" in data) this.#components = data.components || [];
		if ("schematicSvg" in data) this.#schematicSvg = data.schematicSvg;
		if ("boardSvg" in data) this.#boardSvg = data.boardSvg;
		if ("gerberUrl" in data) this.#gerberUrl = data.gerberUrl;
		if ("bomUrl" in data) this.#bomUrl = data.bomUrl;
		if ("pdfUrl" in data) this.#pdfUrl = data.pdfUrl;
		if ("drcResults" in data) this.#drcResults = data.drcResults;
		if (this.#promptInput && this.#prompt) this.#promptInput.value = this.#prompt;
		if (this.#componentInput && this.#components.length) this.#componentInput.value = this.#components.join(", ");
		if (this.#schematicSvg || this.#boardSvg || this.#drcResults) {
			this.#renderPreview();
			this.#showExports();
		}
	}
}
