import express from 'express';
import { authenticateToken } from '../middleware/auth.js';
import Icon from '../models/Icon.js';
import Project from '../models/Project.js';

const router = express.Router();

// Get all icons for a project
router.get('/project/:projectId', authenticateToken, async (req, res, next) => {
  try {
    const project = await Project.findOne({
      where: { id: req.params.projectId, userId: req.user.id }
    });

    if (!project) {
      return res.status(404).json({
        status: 'error',
        code: 'PROJECT_NOT_FOUND',
        message: 'Project not found',
        data: null
      });
    }

    const icons = await Icon.findAll({
      where: { projectId: req.params.projectId },
      order: [['createdAt', 'DESC']]
    });

    res.status(200).json({
      status: 'success',
      code: 'ICONS_RETRIEVED',
      message: 'Icons retrieved successfully',
      data: { icons }
    });
  } catch (error) {
    next(error);
  }
});

// Create icon
router.post('/', authenticateToken, async (req, res, next) => {
  try {
    const { projectId, name, category, svgData, sizes, tags } = req.body;

    const project = await Project.findOne({
      where: { id: projectId, userId: req.user.id }
    });

    if (!project) {
      return res.status(404).json({
        status: 'error',
        code: 'PROJECT_NOT_FOUND',
        message: 'Project not found',
        data: null
      });
    }

    if (!name || !svgData) {
      return res.status(400).json({
        status: 'error',
        code: 'VALIDATION_ERROR',
        message: 'Name and svgData are required',
        data: null
      });
    }

    const icon = await Icon.create({
      projectId,
      name,
      category,
      svgData,
      sizes: sizes || ['16', '24', '32', '48'],
      tags: tags || []
    });

    res.status(201).json({
      status: 'success',
      code: 'ICON_CREATED',
      message: 'Icon created successfully',
      data: { icon }
    });
  } catch (error) {
    next(error);
  }
});

// Update icon
router.put('/:id', authenticateToken, async (req, res, next) => {
  try {
    const icon = await Icon.findByPk(req.params.id);

    if (!icon) {
      return res.status(404).json({
        status: 'error',
        code: 'ICON_NOT_FOUND',
        message: 'Icon not found',
        data: null
      });
    }

    const project = await Project.findOne({
      where: { id: icon.projectId, userId: req.user.id }
    });

    if (!project) {
      return res.status(403).json({
        status: 'error',
        code: 'FORBIDDEN',
        message: 'You do not have permission to modify this icon',
        data: null
      });
    }

    const { name, category, svgData, sizes, tags } = req.body;

    await icon.update({
      name: name || icon.name,
      category: category || icon.category,
      svgData: svgData || icon.svgData,
      sizes: sizes || icon.sizes,
      tags: tags || icon.tags
    });

    res.status(200).json({
      status: 'success',
      code: 'ICON_UPDATED',
      message: 'Icon updated successfully',
      data: { icon }
    });
  } catch (error) {
    next(error);
  }
});

// Delete icon
router.delete('/:id', authenticateToken, async (req, res, next) => {
  try {
    const icon = await Icon.findByPk(req.params.id);

    if (!icon) {
      return res.status(404).json({
        status: 'error',
        code: 'ICON_NOT_FOUND',
        message: 'Icon not found',
        data: null
      });
    }

    const project = await Project.findOne({
      where: { id: icon.projectId, userId: req.user.id }
    });

    if (!project) {
      return res.status(403).json({
        status: 'error',
        code: 'FORBIDDEN',
        message: 'You do not have permission to modify this icon',
        data: null
      });
    }

    await icon.destroy();

    res.status(200).json({
      status: 'success',
      code: 'ICON_DELETED',
      message: 'Icon deleted successfully',
      data: null
    });
  } catch (error) {
    next(error);
  }
});

export default router;
