"use client"

import { useState, useEffect, useCallback } from "react"
import type { WidgetConfig, WidgetSettings } from "@/lib/types/dashboard"

const DEFAULT_WIDGETS: WidgetConfig[] = [
    { id: "quick-actions", title: "Quick Actions", visible: true, order: 0 },
    { id: "overview", title: "Overview", visible: true, order: 1, settings: { showChange: false } },
    { id: "recent-activity", title: "Recent Activity", visible: true, order: 2, settings: { itemCount: 10 } },
    { id: "top-products", title: "Top Products", visible: true, order: 3, settings: { itemCount: 5, showRevenue: true } },
    { id: "revenue-chart", title: "Revenue Chart", visible: false, order: 4, settings: { days: 7, showComparison: false } },
    { id: "low-stock", title: "Low Stock Alerts", visible: false, order: 5, settings: { threshold: 5, itemCount: 5 } },
    { id: "recent-customers", title: "Recent Customers", visible: false, order: 6, settings: { itemCount: 5, showSpent: true } },
    { id: "pending-chats", title: "Pending Chats", visible: false, order: 7, settings: { showPreview: true } },
]

const STORAGE_KEY = "dashboard-widget-layout-v4"

/**
 * useWidgetLayout - Persist and manage dashboard widget visibility, order, and settings
 */
export function useWidgetLayout() {
    const [widgets, setWidgets] = useState<WidgetConfig[]>(DEFAULT_WIDGETS)
    const [isLoaded, setIsLoaded] = useState(false)

    // Load from localStorage on mount
    useEffect(() => {
        if (typeof window === "undefined") return

        try {
            const stored = localStorage.getItem(STORAGE_KEY)
            if (stored) {
                const parsed = JSON.parse(stored) as WidgetConfig[]
                // Merge with defaults to handle new widgets added in future
                const merged = DEFAULT_WIDGETS.map(defaultWidget => {
                    const savedWidget = parsed.find(w => w.id === defaultWidget.id)
                    return savedWidget
                        ? { ...defaultWidget, ...savedWidget, settings: { ...defaultWidget.settings, ...savedWidget.settings } }
                        : defaultWidget
                })
                setWidgets(merged.sort((a, b) => a.order - b.order))
            }
        } catch (e) {
            console.warn("Failed to load widget layout:", e)
        }
        setIsLoaded(true)
    }, [])

    // Save to localStorage whenever widgets change
    useEffect(() => {
        if (!isLoaded || typeof window === "undefined") return

        try {
            localStorage.setItem(STORAGE_KEY, JSON.stringify(widgets))
        } catch (e) {
            console.warn("Failed to save widget layout:", e)
        }
    }, [widgets, isLoaded])

    const toggleWidget = useCallback((widgetId: string) => {
        setWidgets(prev =>
            prev.map(w => w.id === widgetId ? { ...w, visible: !w.visible } : w)
        )
    }, [])

    const moveWidget = useCallback((widgetId: string, direction: "up" | "down") => {
        setWidgets(prev => {
            const index = prev.findIndex(w => w.id === widgetId)
            if (index === -1) return prev

            const newIndex = direction === "up" ? index - 1 : index + 1
            if (newIndex < 0 || newIndex >= prev.length) return prev

            const newWidgets = [...prev]
            const [widget] = newWidgets.splice(index, 1)
            newWidgets.splice(newIndex, 0, widget)

            // Update order values
            return newWidgets.map((w, i) => ({ ...w, order: i }))
        })
    }, [])

    const updateWidgetSettings = useCallback((widgetId: string, newSettings: Record<string, any>) => {
        setWidgets(prev =>
            prev.map(w => w.id === widgetId
                ? { ...w, settings: { ...w.settings, ...newSettings } }
                : w
            )
        )
    }, [])


    const getWidgetSettings = useCallback(<T extends Record<string, any>>(widgetId: string): T | undefined => {
        return widgets.find(w => w.id === widgetId)?.settings as T | undefined
    }, [widgets])

    const resetLayout = useCallback(() => {
        setWidgets(DEFAULT_WIDGETS)
    }, [])

    const getVisibleWidgets = useCallback(() => {
        return widgets.filter(w => w.visible).sort((a, b) => a.order - b.order)
    }, [widgets])

    const isWidgetVisible = useCallback((widgetId: string) => {
        return widgets.find(w => w.id === widgetId)?.visible ?? true
    }, [widgets])

    // Reorder widgets by moving activeId to overId's position
    const reorderWidgets = useCallback((activeId: string, overId: string) => {
        setWidgets(prev => {
            const oldIndex = prev.findIndex(w => w.id === activeId)
            const newIndex = prev.findIndex(w => w.id === overId)

            if (oldIndex === -1 || newIndex === -1) return prev

            const newWidgets = [...prev]
            const [removed] = newWidgets.splice(oldIndex, 1)
            newWidgets.splice(newIndex, 0, removed)

            // Update order values
            return newWidgets.map((w, i) => ({ ...w, order: i }))
        })
    }, [])

    return {
        widgets,
        toggleWidget,
        moveWidget,
        reorderWidgets,
        updateWidgetSettings,
        getWidgetSettings,
        resetLayout,
        getVisibleWidgets,
        isWidgetVisible,
        isLoaded
    }
}
