package main

import (
	"embed"
	"io/fs"
	"net/http"

	"git.dev.alexdunmow.com/block/pluginsdk/blocks"
	"git.dev.alexdunmow.com/block/pluginsdk/templates"
)

//go:embed all:web/dist/assets
var distAssets embed.FS

//go:embed plugin.mod
var pluginModBytes []byte

// Register is the plugin entry point that registers the Smart Block.
// Returns an error if registration fails.
func Register(tr templates.TemplateRegistry, br blocks.BlockRegistry) error {
	// Register the Smart Block
	br.Register(SmartBlockMeta, SmartBlockFunc)
	return nil
}

// SmartBlockMeta defines the Smart Block metadata
var SmartBlockMeta = blocks.BlockMeta{
	Key:         "smart",
	Title:       "Smart Block",
	Description: "AI-powered block that generates custom content structures from natural language prompts",
	Source:      "smartblock",
	EditorJS:    "remoteEntry.js",
}

// AssetsHandler serves static assets including the Module Federation remote entry.
// This is called by the plugin loader to mount assets at /api/plugins/smartblock/
func AssetsHandler() http.Handler {
	// Create a sub-filesystem pointing to web/dist/assets
	subFS, err := fs.Sub(distAssets, "web/dist/assets")
	if err != nil {
		// Fallback to 404 handler if embed fails
		return http.NotFoundHandler()
	}

	return http.FileServer(http.FS(subFS))
}
