package lib

import (
	xmpbase "github.com/trimmer-io/go-xmp/models/xmp_base"
	"github.com/trimmer-io/go-xmp/xmp"
)

const (
	FacturXNamespace = "Factur-X PDFA Erweiterungsschema"
	FacturXUri       = "urn:factur-x:pdfa:CrossIndustryDocument:invoice:1p0#"
	FacturXPrefix    = "fx:"
)

func addFacturXXmpData(data []byte) ([]byte, error) {
	doc := &xmp.Document{}
	err := xmp.Unmarshal(data, doc)
	if err != nil {
		return nil, err
	}

	// --- Register Factur-X namespace ---
	ns := &xmp.Namespace{
		Name: "fx",
		URI:  FacturXUri,
		Factory: func(name string) xmp.Model {
			return &xmpbase.XmpBase{}
		},
	}
	xmp.Register(ns)
	_, err = doc.MakeModel(ns)
	if err != nil {
		return nil, err
	}

	// --- Set Factur-X properties ---
	err = doc.SetPath(xmp.PathValue{
		Path:      FacturXPrefix + "ConformanceLevel",
		Namespace: FacturXUri,
		Value:     "EN 16931",
	})
	if err != nil {
		return nil, err
	}

	err = doc.SetPath(xmp.PathValue{
		Path:      FacturXPrefix + "DocumentType",
		Namespace: FacturXUri,
		Value:     "INVOICE",
	})
	if err != nil {
		return nil, err
	}

	err = doc.SetPath(xmp.PathValue{
		Path:      FacturXPrefix + "DocumentFileName",
		Namespace: FacturXUri,
		Value:     "factur-x.xml",
	})
	if err != nil {
		return nil, err
	}

	err = doc.SetPath(xmp.PathValue{
		Path:      FacturXPrefix + "Version",
		Namespace: FacturXUri,
		Value:     "1.0",
	})
	if err != nil {
		return nil, err
	}

	return xmp.Marshal(doc)
}
