from django_elasticsearch_dsl import Document, fields
from django_elasticsearch_dsl.registries import registry
from .models import DiarioOficial


@registry.register_document
class DiarioOficialDocument(Document):
    numero = fields.KeywordField()
    data = fields.DateField()
    link = fields.KeywordField()
    tipo = fields.ObjectField(properties={"nome": fields.KeywordField()})

    # Campo para páginas
    paginas = fields.NestedField(
        properties={
            "id": fields.IntegerField(),
            "numero": fields.IntegerField(),
            "conteudo": fields.TextField(
                analyzer="custom_portuguese",
                fields={
                    "keyword": fields.KeywordField(),
                    "search": fields.TextField(analyzer="custom_portuguese"),
                },
            ),
        }
    )

    class Index:
        name = "diario_oficial"
        settings = {
            "number_of_shards": 1,
            "number_of_replicas": 0,
            "analysis": {
                "analyzer": {
                    "custom_portuguese": {
                        "type": "custom",
                        "tokenizer": "standard",
                        "filter": [
                            "lowercase",
                            "asciifolding",
                            "portuguese_stop",
                        ],
                    }
                },
                "filter": {
                    "portuguese_stop": {"type": "stop", "stopwords": "_portuguese_"},
                },
            },
        }

    class Django:
        model = DiarioOficial
        fields = ["id"]

    def prepare_tipo(self, instance):
        return {"nome": instance.tipo.nome if instance.tipo else "Sem Tipo"}

    def prepare_link(self, instance):
        return instance.link or ""

    def prepare_paginas(self, instance):
        # Preparar páginas ordenadas
        paginas = instance.paginas.all().order_by("numero")
        return [
            {"id": pagina.id, "numero": pagina.numero, "conteudo": pagina.conteudo}
            for pagina in paginas
        ]
