/** @odoo-module **/

import { Component, onWillStart, useState } from "@odoo/owl";
import { registry } from "@web/core/registry";
import { useService } from "@web/core/utils/hooks";

class SeoDashboard extends Component {
    static template = "otoolkit_seo_content.SeoDashboard";
    static props = ["*"];

    setup() {
        this.orm = useService("orm");
        this.action = useService("action");
        this.state = useState({
            data: null,
            loading: true,
        });
        onWillStart(async () => {
            await this.loadDashboardData();
        });
    }

    async loadDashboardData() {
        this.state.loading = true;
        try {
            this.state.data = await this.orm.call(
                "otk.seo.content",
                "get_dashboard_data",
                []
            );
        } catch (e) {
            console.error("Failed to load dashboard data:", e);
            this.state.data = null;
        }
        this.state.loading = false;
    }

    getScoreColor(score) {
        if (score >= 70) return "success";
        if (score >= 40) return "warning";
        return "danger";
    }

    getStateLabel(state) {
        const labels = {
            draft: "Draft",
            generating: "Generating",
            images_pending: "Images Pending",
            review: "Review",
            approved: "Approved",
            published: "Published",
            archived: "Archived",
        };
        return labels[state] || state;
    }

    getTypeLabel(type) {
        const labels = {
            blog_post: "Blog Post",
            product_desc: "Product Desc",
            category_desc: "Category Desc",
            landing_page: "Landing Page",
            social_post: "Social Post",
        };
        return labels[type] || type;
    }

    getStateColor(state) {
        const colors = {
            draft: "#6c757d",
            generating: "#17a2b8",
            images_pending: "#ffc107",
            review: "#007bff",
            approved: "#28a745",
            published: "#20c997",
            archived: "#adb5bd",
        };
        return colors[state] || "#6c757d";
    }

    onClickTotalContent() {
        this.action.doAction({
            type: "ir.actions.act_window",
            name: "All Content",
            res_model: "otk.seo.content",
            view_mode: "list,form",
            views: [[false, "list"], [false, "form"]],
        });
    }

    onClickStaleContent() {
        this.action.doAction({
            type: "ir.actions.act_window",
            name: "Stale Content",
            res_model: "otk.seo.content",
            view_mode: "list,form",
            views: [[false, "list"], [false, "form"]],
            domain: [["is_stale", "=", true]],
        });
    }

    onClickState(state) {
        this.action.doAction({
            type: "ir.actions.act_window",
            name: this.getStateLabel(state),
            res_model: "otk.seo.content",
            view_mode: "list,form",
            views: [[false, "list"], [false, "form"]],
            domain: [["state", "=", state]],
        });
    }

    onClickRecent(contentId) {
        this.action.doAction({
            type: "ir.actions.act_window",
            res_model: "otk.seo.content",
            res_id: contentId,
            view_mode: "form",
            views: [[false, "form"]],
        });
    }
}

registry.category("actions").add("seo_content_dashboard", SeoDashboard);
