import { PluginInstance, PluginContext } from '../../backend/src/core/types';
import { ImageOptimizer } from './backend/Optimizer';
import manifest from './manifest.json';

const plugin: PluginInstance = {
    manifest: manifest as any,
    async activate(ctx: PluginContext): Promise<void> {
        // Register for the upload:transform hook
        ctx.hooks.register('upload:transform', async (payload: any) => {
            const { buffer, mimetype, category } = payload;

            // Only process common image formats
            const imageTypes = ['image/jpeg', 'image/png', 'image/webp', 'image/avif'];
            if (!imageTypes.includes(mimetype)) {
                return null; // Skip non-images
            }

            ctx.logger.info(`[SmartVision] Optimizing image for category: ${category}`);

            const result = await ImageOptimizer.optimize(buffer, category);

            return {
                buffer: result.buffer,
                mimetype: result.mimetype
            };
        });

        ctx.logger.info('SmartVision Optimizer activated');
    },
    async deactivate(ctx: PluginContext): Promise<void> {
        ctx.logger.info('SmartVision Optimizer deactivated');
    }
};

export default plugin;
