const video_product_model =  require('../Model/video_product_model');
class video_product_controllers {
    async getProductData(params) {
        try {
            // 参数验证
            if (!params?.book_id) {
                throw  'book_id ID is required';
            }

            const book_id = params.book_id;
            const filterData = await video_product_model.findOne({ product_id: book_id });

            if (!filterData) {
                return {
                    success: false,
                    data: null
                };
            }

            return {
                success: true,
                data: filterData
            };
        } catch (error) {

            throw error;
        }
    }

    async createProductData(tableData) {
        try {
            // 数据验证
            if (!tableData || Object.keys(tableData).length === 0) {
                throw 'Invalid table data';
            }

            // 检查必要字段
            const requiredFields = ['book_platform', 'product_name', 'genre', 'product_id', 'words']; // 替换为实际必要字段
            for (const field of requiredFields) {
                if (tableData[field]==null||tableData[field]==undefined) {
                    throw `Missing required field: ${field}`;
                }
            }

            // 添加创建时间戳
            // const dataToCreate = {
            //     ...tableData
            //     // created_at: new Date(),
            //     // updated_at: new Date()
            // };

            const id = await video_product_model.createData(tableData);


            return {
                success: true,
                data: { id }
            };
        } catch (error) {
            throw error;
        }
    }

}

module.exports =  new video_product_controllers();