package com.kbs.kbsintranett;

import com.google.gson.annotations.SerializedName;
import java.io.Serializable;
import java.util.Arrays;
import java.util.List;

public class WpPost implements Serializable {
    @SerializedName("title")
    public Rendered title;

    @SerializedName("excerpt")
    public Rendered excerpt;

    @SerializedName("content")
    public Rendered content;

    @SerializedName("date")
    public String date;

    @SerializedName("_embedded")
    public Embedded embedded;

    public static class Rendered implements Serializable {
        @SerializedName("rendered")
        public String renderedString;
    }

    public static class Embedded implements Serializable {
        @SerializedName("wp:featuredmedia")
        public List<Media> mediaList;

        @SerializedName("wp:term")
        public List<List<Term>> termList;

        // NYTT: Forfatter-liste
        @SerializedName("author")
        public List<Author> authorList;
    }

    public static class Media implements Serializable {
        @SerializedName("source_url")
        public String sourceUrl;
    }

    public static class Term implements Serializable {
        @SerializedName("name")
        public String name;
    }

    // NYTT: Forfatter-klasse
    public static class Author implements Serializable {
        @SerializedName("name")
        public String name;
    }

    public String getTitleStr() {
        return title != null ? title.renderedString : "Uten tittel";
    }

    public String getExcerptStr() {
        return excerpt != null ?
                android.text.Html.fromHtml(excerpt.renderedString, android.text.Html.FROM_HTML_MODE_COMPACT).toString().trim() : "";
    }

    public String getContentStr() {
        return content != null ? content.renderedString : "";
    }

    public String getFeaturedImageUrl() {
        if (embedded != null && embedded.mediaList != null && !embedded.mediaList.isEmpty()) {
            return embedded.mediaList.get(0).sourceUrl;
        }
        return null;
    }

    // NYTT: Hent forfatternavn
    public String getAuthorName() {
        if (embedded != null && embedded.authorList != null && !embedded.authorList.isEmpty()) {
            return embedded.authorList.get(0).name;
        }
        return "Ukjent"; // Fallback
    }

    public String getCategoryName() {
        if (embedded != null && embedded.termList != null && !embedded.termList.isEmpty()) {
            List<Term> categories = embedded.termList.get(0);
            if (categories == null || categories.isEmpty()) return "";

            List<String> priorityCategories = Arrays.asList(
                    "Avtaler og invitasjoner", "BHT", "Bilhold", "Cordel",
                    "Ferieavvikling", "Fest og moro", "Generell drift",
                    "HMS", "IT og sikkerhet", "Miljøfyrtårn", "Møtereferat", "SMX"
            );

            for (Term term : categories) {
                if (priorityCategories.contains(term.name)) return term.name;
            }
            for (Term term : categories) {
                if (term.name.contains("Alle ansatte")) return "Til info";
            }
            return categories.get(0).name;
        }
        return "";
    }
}