import type { StatusMessage } from "@/services/types";
import { addDataPoint, getRealtimeData } from "@/services/store";
import { formatTimestamp, calcPercentage } from "@/services/utils";
import { useEffect } from "react";

export type ChartData = {
    labels: string[];
    datasets: {
        label: string;
        data: number[];
        color: string;
    }[];
};

type ChartDataReturns = {
    cpuData: ChartData;
    memoryData: ChartData;
    networkData: ChartData;
};

export function useChartData(
    data: StatusMessage | StatusMessage[] | null,
): ChartDataReturns {
    const currentData = Array.isArray(data) ? data : getRealtimeData();

    useEffect(() => {
        if (!data || Array.isArray(data)) return;

        addDataPoint(data);
    }, [data]);

    return {
        cpuData: {
            labels: currentData.map((p) => formatTimestamp(p.timestamp)),
            datasets: [
                {
                    label: "Total CPU (%)",
                    data: currentData.map(({ metrics }) => metrics.cpu.usage),
                    color: "#3b82f6",
                },
                {
                    label: "User (%)",
                    data: currentData.map(
                        ({ metrics }) => metrics.cpu.breakdown.user,
                    ),
                    color: "#10b981",
                },
                {
                    label: "System (%)",
                    data: currentData.map(
                        ({ metrics }) => metrics.cpu.breakdown.system,
                    ),
                    color: "#f59e0b",
                },
                {
                    label: "I/O Wait (%)",
                    data: currentData.map(
                        ({ metrics }) => metrics.cpu.breakdown.iowait,
                    ),
                    color: "#ef4444",
                },
                {
                    label: "Steal (%)",
                    data: currentData.map(
                        ({ metrics }) => metrics.cpu.breakdown.steal,
                    ),
                    color: "#8b5cf6",
                },
                {
                    label: "Idle (%)",
                    data: currentData.map(
                        ({ metrics }) => metrics.cpu.breakdown.idle,
                    ),
                    color: "#6b7280",
                },
            ],
        },
        memoryData: {
            labels: currentData.map((p) => formatTimestamp(p.timestamp)),
            datasets: [
                {
                    label: "Memory Usage (%)",
                    data: currentData.map(({ metrics }) =>
                        calcPercentage(
                            metrics.memory.used,
                            metrics.memory.total,
                        ),
                    ),
                    color: "#10b981",
                },
                {
                    label: "Swap Usage (%)",
                    data: currentData.map(({ metrics }) =>
                        calcPercentage(
                            metrics.memory.swap_used,
                            metrics.memory.swap_total,
                        ),
                    ),
                    color: "#f59e0b",
                },
            ],
        },
        networkData: {
            labels: currentData.map((p) => formatTimestamp(p.timestamp)),
            datasets: [
                {
                    label: "Upload (bps)",
                    data: currentData.map(({ metrics }) => metrics.network.up),
                    color: "#ef4444",
                },
                {
                    label: "Download (bps)",
                    data: currentData.map(
                        ({ metrics }) => metrics.network.down,
                    ),
                    color: "#3b82f6",
                },
            ],
        },
    };
}
