import predefinedActivityService from '../services/predefinedActivityService.js';
import PredefinedActivityImage from '../models/PredefinedActivityImage.js';
import path from 'path';
import fs from 'fs';

export const createPredefinedActivity = async (req, res) => {
    try {
        const { name, code, description, durationText, duration, imageLink, drawingData, excludeFromStats } = req.body;
        const predefinedActivity = await predefinedActivityService.createPredefinedActivity({ name, code, description, durationText, duration, imageLink, drawingData, excludeFromStats });
        res.status(201).json(predefinedActivity);
    } catch (error) {
        console.error('[createPredefinedActivity] - Error:', error);
        res.status(500).json({ error: 'Error creating predefined activity' });
    }
};

export const getAllPredefinedActivities = async (req, res) => {
    try {
        const { scope = 'all' } = req.query;
        const predefinedActivities = await predefinedActivityService.getAllPredefinedActivities(scope);
        res.status(200).json(predefinedActivities);
    } catch (error) {
        console.error('[getAllPredefinedActivities] - Error:', error);
        res.status(500).json({ error: 'Error fetching predefined activities' });
    }
};

export const getPredefinedActivityById = async (req, res) => {
    try {
        const { id } = req.params;
        const predefinedActivity = await predefinedActivityService.getPredefinedActivityById(id);
        const images = await PredefinedActivityImage.findAll({ where: { predefinedActivityId: id } });
        if (!predefinedActivity) {
            return res.status(404).json({ error: 'Predefined activity not found' });
        }
        res.status(200).json({ ...predefinedActivity.toJSON(), images });
    } catch (error) {
        console.error('[getPredefinedActivityById] - Error:', error);
        res.status(500).json({ error: 'Error fetching predefined activity' });
    }
};

export const updatePredefinedActivity = async (req, res) => {
    try {
        const { id } = req.params;
        const { name, code, description, durationText, duration, imageLink, drawingData, excludeFromStats } = req.body;
        const updatedActivity = await predefinedActivityService.updatePredefinedActivity(id, { name, code, description, durationText, duration, imageLink, drawingData, excludeFromStats });
        res.status(200).json(updatedActivity);
    } catch (error) {
        console.error('[updatePredefinedActivity] - Error:', error);
        res.status(500).json({ error: 'Error updating predefined activity' });
    }
};

export const searchPredefinedActivities = async (req, res) => {
    try {
        const { q, limit } = req.query;
        const result = await predefinedActivityService.searchPredefinedActivities(q, limit);
        res.status(200).json(result);
    } catch (error) {
        console.error('[searchPredefinedActivities] - Error:', error);
        res.status(500).json({ error: 'Error searching predefined activities' });
    }
};

export const mergePredefinedActivities = async (req, res) => {
    try {
        const { sourceId, targetId } = req.body;
        await predefinedActivityService.mergeActivities(sourceId, targetId);
        res.status(200).json({ ok: true });
    } catch (error) {
        console.error('[mergePredefinedActivities] - Error:', error);
        res.status(500).json({ error: 'Error merging predefined activities' });
    }
};

export const deduplicatePredefinedActivities = async (req, res) => {
    try {
        const result = await predefinedActivityService.deduplicateActivities();
        res.status(200).json(result);
    } catch (error) {
        console.error('[deduplicatePredefinedActivities] - Error:', error);
        res.status(500).json({ error: 'Error deduplicating predefined activities' });
    }
};
