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:"
)

type PDFAProperty struct {
	Name        string `xmp:"pdfaProperty:name"`
	ValueType   string `xmp:"pdfaProperty:valueType"`
	Category    string `xmp:"pdfaProperty:category"`
	Description string `xmp:"pdfaProperty:description"`
}

type PDFASchema struct {
	Schema       string         `xmp:"pdfaSchema:schema"`
	NamespaceURI string         `xmp:"pdfaSchema:namespaceURI"`
	Prefix       string         `xmp:"pdfaSchema:prefix"`
	Property     []PDFAProperty `xmp:"pdfaSchema:property"`
}

type PDFAExtensionData struct {
	Schemas [1]PDFASchema `xmp:"pdfaExtension:schemas,bag"`
}

type PDFAExtensionModel struct {
	PDFAExtensionData

	xmpbase.XmpBase
}

func (x PDFAExtensionModel) Can(nsName string) bool {
	return nsName == "pdfaExtension"
}

func (x PDFAExtensionModel) Namespaces() xmp.NamespaceList {
	return xmp.NamespaceList{
		{
			Name:    "pdfaSchema",
			URI:     "http://www.aiim.org/pdfa/ns/schema#",
			Factory: defaultFactory,
		},
		{
			Name:    "pdfaExtension",
			URI:     "http://www.aiim.org/pdfa/ns/extension/",
			Factory: defaultFactory,
		},
		{
			Name:    "pdfaProperty",
			URI:     "http://www.aiim.org/pdfa/ns/property#",
			Factory: defaultFactory,
		},
	}
}

func defaultFactory(name string) xmp.Model {
	return nil
}

func addFacturXXmpData(data []byte) ([]byte, error) {
	// --- Register Factur-X namespace ---
	xmp.Register(&xmp.Namespace{
		Name:    "fx",
		URI:     FacturXUri,
		Factory: defaultFactory,
	})

	doc := &xmp.Document{}
	err := xmp.Unmarshal(data, doc)
	if err != nil {
		return nil, err
	}

	model := &PDFAExtensionModel{
		PDFAExtensionData: PDFAExtensionData{
			Schemas: [1]PDFASchema{
				{
					Schema:       "Factur-X PDFA Extension Schema",
					NamespaceURI: "urn:factur-x:pdfa:CrossIndustryDocument:invoice:1p0#",
					Prefix:       "fx",
					Property: []PDFAProperty{
						{
							Name:        "DocumentType",
							ValueType:   "Text",
							Category:    "external",
							Description: "Factur-X document type",
						},
						{
							Name:        "DocumentFileName",
							ValueType:   "Text",
							Category:    "external",
							Description: "Attached XML file name",
						},
						{
							Name:        "Version",
							ValueType:   "Text",
							Category:    "external",
							Description: "Factur-X version",
						},
						{
							Name:        "ConformanceLevel",
							ValueType:   "Text",
							Category:    "external",
							Description: "Factur-X profile",
						},
					},
				},
			},
		},
	}
	_, err = doc.AddModel(model)
	if err != nil {
		return nil, err
	}

	doc.FindModel(&xmp.Namespace{
		Name:    "",
		URI:     "",
		Factory: nil,
	})

	// --- 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)
}
