/**
 * 廠商管理相關的業務邏輯 Hook
 */

import { useState } from "react";
import { toast } from "sonner";
import type { Supplier, SupplyProduct } from "../types/vendor";
import type { Product } from "../types/product";

export function useVendors(initialSuppliers: Supplier[], allProducts: Product[]) {
    const [suppliers, setSuppliers] = useState<Supplier[]>(initialSuppliers);

    const addSupplier = (supplier: Omit<Supplier, "id">) => {
        const newSupplier: Supplier = {
            ...supplier,
            id: `sup-${Date.now()}`,
            commonProducts: [],
            supplyProducts: [],
        };
        setSuppliers((prev) => [...prev, newSupplier]);
        toast.success("廠商已新增");
    };

    const updateSupplier = (id: string, updatedSupplier: Omit<Supplier, "id">) => {
        setSuppliers((prev) =>
            prev.map((s) => (s.id === id ? { ...s, ...updatedSupplier } : s))
        );
        toast.success("廠商資料已更新");
    };

    const deleteSupplier = (id: string) => {
        setSuppliers((prev) => prev.filter((s) => s.id !== id));
        toast.success("廠商已刪除");
    };

    // 新增供貨商品
    const addSupplyProduct = (supplierId: string, productId: string, lastPrice?: number) => {
        const product = allProducts.find(p => p.id === productId);
        if (!product) return;

        setSuppliers((prev) =>
            prev.map((s) => {
                if (s.id === supplierId) {
                    const newSupplyProduct: SupplyProduct = {
                        id: `sp-${Date.now()}`,
                        productId: product.id,
                        productName: product.name,
                        unit: product.unit,
                        lastPrice,
                    };
                    return {
                        ...s,
                        supplyProducts: [...s.supplyProducts, newSupplyProduct],
                    };
                }
                return s;
            })
        );
        toast.success("供貨商品已新增");
    };

    // 更新供貨商品
    const updateSupplyProduct = (supplierId: string, productId: string, lastPrice?: number) => {
        setSuppliers((prev) =>
            prev.map((s) => {
                if (s.id === supplierId) {
                    return {
                        ...s,
                        supplyProducts: s.supplyProducts.map((sp) =>
                            sp.productId === productId ? { ...sp, lastPrice } : sp
                        ),
                    };
                }
                return s;
            })
        );
        toast.success("供貨商品已更新");
    };

    // 移除供貨商品
    const removeSupplyProduct = (supplierId: string, productId: string) => {
        setSuppliers((prev) =>
            prev.map((s) => {
                if (s.id === supplierId) {
                    return {
                        ...s,
                        supplyProducts: s.supplyProducts.filter((sp) => sp.productId !== productId),
                    };
                }
                return s;
            })
        );
        toast.success("供貨商品已移除");
    };

    return {
        suppliers,
        addSupplier,
        updateSupplier,
        deleteSupplier,
        addSupplyProduct,
        updateSupplyProduct,
        removeSupplyProduct,
    };
}
