// import origin_data_model from '../Model/origin_data_model';
const origin_data_model = require("../Model/origin_data_model")

class origin_data_controllers {

    async getOriginData(params) {
        try {
            // 参数验证
            if (!params?.materialId) {
                throw  'origin_data_controllers:Video ID is required';
            }

            const materialId = params.materialId;
            const filterData = await origin_data_model.findOne({ materialId: materialId });

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

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

            throw error;
        }
    }


    async getOriginDataByStatus(params) {
        try {
            // 参数验证
            if (params.status==undefined||params.status==null) {
                throw  'origin_data_controllers:status is required';
            }

            const status = params.status;
            const filterData = await origin_data_model.findOne({ status: status });

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

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

            throw error;
        }
    }


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

            // 检查必要字段
            const requiredFields = ['video_id', 'video_link', 'title', 'publish_time', 'kepp_num', 'comment_num', 'like_num', 'shared_num','is_guajian','guajian_link']; // 替换为实际必要字段
            for (const field of requiredFields) {
                if (tableData[field]==undefined||tableData[field]==null) {
                    throw `Missing required field: ${field}`;
                }
            }

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

            const id = await origin_data_model.createOriginData(tableData);


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


    async updateOriginData(params, tableData) {
        try {
            // 参数验证
            if (!params?.id) {
                throw 'ID is required';
            }

            if (!tableData || Object.keys(tableData).length === 0) {
                throw 'No data to update';
            }

            const id = params.id;

            // 检查数据是否存在
            // const existingData = await origin_data_model.findOne({ video_id:video_id });
            // if (!existingData) {
            //     throw 'Filter data not found';
            // }

            // 添加更新时间戳
            // const dataToUpdtableDataate = {
            //     ...,
            // };

            // 使用乐观锁更新
            const affectedRows = await origin_data_model.updateOriginData(
                id,
                tableData // 假设使用版本控制
            );

            if (affectedRows === 0) {
                throw 'Data was modified by another process';
            }
            return {
                success: true,
                message: 'Filter data updated successfully'
            };
        } catch (error) {
            throw error;
        }
    }
}

module.exports = new origin_data_controllers();