import { readFileSync, writeFileSync, existsSync, readdirSync, copyFileSync } from 'fs';
import { resolve } from 'path';
import type { Plugin } from 'vite';

const GUIDE_FOR_FRONTEND = `
<!--
  This is a static build of the frontend.
  It is automatically generated by the build process.
  Do not edit this file directly.
  To make changes, refer to the "Web UI" section in the README.
-->
`.trim();

export function llamaCppBuildPlugin(): Plugin {
	return {
		name: 'llamacpp:build',
		apply: 'build',
		closeBundle() {
			// Ensure the SvelteKit adapter has finished writing to ../public
			setTimeout(() => {
				try {
					const indexPath = resolve('../public/index.html');
					if (!existsSync(indexPath)) return;

					let content = readFileSync(indexPath, 'utf-8');

					const faviconPath = resolve('static/favicon.svg');

					if (existsSync(faviconPath)) {
						const faviconContent = readFileSync(faviconPath, 'utf-8');
						const faviconBase64 = Buffer.from(faviconContent).toString('base64');
						const faviconDataUrl = `data:image/svg+xml;base64,${faviconBase64}`;

						content = content.replace(/href="[^"]*favicon\.svg"/g, `href="${faviconDataUrl}"`);

						console.log('✓ Inlined favicon.svg as base64 data URL');
					}

					content = content.replace(/\r/g, '');
					content = GUIDE_FOR_FRONTEND + '\n' + content;
					content = content.replace(/\/_app\/immutable\/bundle\.[^"]+\.js/g, './bundle.js');
					content = content.replace(
						/\/_app\/immutable\/assets\/bundle\.[^"]+\.css/g,
						'./bundle.css'
					);
					content = content.replace(/__sveltekit_[a-z0-9]+/g, '__sveltekit__');

					writeFileSync(indexPath, content, 'utf-8');
					console.log('✓ Updated index.html');

					// Copy bundle.*.js -> ../public/bundle.js
					const immutableDir = resolve('../public/_app/immutable');
					const bundleDir = resolve('../public/_app/immutable/assets');

					if (existsSync(immutableDir)) {
						const jsFiles = readdirSync(immutableDir).filter((f) => f.match(/^bundle\..+\.js$/));

						if (jsFiles.length > 0) {
							copyFileSync(resolve(immutableDir, jsFiles[0]), resolve('../public/bundle.js'));
							// Normalize __sveltekit_<hash> to __sveltekit__ in bundle.js
							const bundleJsPath = resolve('../public/bundle.js');
							let bundleJs = readFileSync(bundleJsPath, 'utf-8');
							bundleJs = bundleJs.replace(/__sveltekit_[a-z0-9]+/g, '__sveltekit__');
							writeFileSync(bundleJsPath, bundleJs, 'utf-8');
							console.log(`✓ Copied ${jsFiles[0]} -> bundle.js`);
						}
					}

					// Copy bundle.*.css -> ../public/bundle.css
					if (existsSync(bundleDir)) {
						const cssFiles = readdirSync(bundleDir).filter((f) => f.match(/^bundle\..+\.css$/));

						if (cssFiles.length > 0) {
							copyFileSync(resolve(bundleDir, cssFiles[0]), resolve('../public/bundle.css'));
							console.log(`✓ Copied ${cssFiles[0]} -> bundle.css`);
						}
					}
				} catch (error) {
					console.error('Failed to update index.html:', error);
				}
			}, 100);
		}
	};
}
