const express = require('express');
const router = express.Router();
const { checkScreenshotsExist, generateScreenshots, buildResponsePayload } = require('../utils/screenshot');
const { viewerUrls } = require('../config/config');

/**
 * T-Shirt screenshot handler (4 views: front, back, right, left)
 * Route: GET /tshirt/:designId
 */
router.get('/:designId', async (req, res) => {
  try {
    const designId = req.params.designId;
    console.log('T-Shirt screenshot request for design:', designId);

    const filenames = {
      thumb_front: `${designId}-front-thumbnail.png`,
      thumb_back: `${designId}-back-thumbnail.png`,
      thumb_right: `${designId}-right-thumbnail.png`,
      thumb_left: `${designId}-left-thumbnail.png`
    };

    // Check if screenshots already exist
    if (checkScreenshotsExist(Object.values(filenames))) {
      console.log('Screenshots already exist, returning cached versions');
      return res.status(200).jsonp(buildResponsePayload(filenames));
    }

    // Generate new screenshots
    const url = `${viewerUrls.crewsportswear}${designId}/`;
    const clipConfigs = [
      {
        filename: filenames.thumb_front,
        clip: { x: 120, y: 37.11, width: 470, height: 520 }
      },
      {
        filename: filenames.thumb_back,
        clip: { x: 776.967, y: 37.11, width: 470, height: 520 }
      },
      {
        filename: filenames.thumb_right,
        clip: { x: 120, y: 557, width: 470, height: 520 }
      },
      {
        filename: filenames.thumb_left,
        clip: { x: 776.967, y: 557, width: 470, height: 520 }
      }
    ];

    await generateScreenshots(url, clipConfigs);

    res.status(200).jsonp(buildResponsePayload(filenames));
  } catch (error) {
    console.error('T-Shirt screenshot error:', error);
    res.status(500).json({ 
      error: 'Failed to generate screenshots',
      message: error.message 
    });
  }
});

/**
 * T-Shirt All-Over Print screenshot handler (2 views: front, back)
 * Route: GET /tshirt/allover/:designId
 */
router.get('/allover/:designId', async (req, res) => {
  try {
    const designId = req.params.designId;
    console.log('T-Shirt All-Over Print screenshot request for design:', designId);

    const filenames = {
      thumb_front: `${designId}-front-thumbnail.png`,
      thumb_back: `${designId}-back-thumbnail.png`
    };

    // Check if screenshots already exist
    if (checkScreenshotsExist(Object.values(filenames))) {
      console.log('Screenshots already exist, returning cached versions');
      return res.status(200).jsonp(buildResponsePayload(filenames));
    }

    // Generate new screenshots
    const url = `${viewerUrls.crewsportswear}${designId}/`;
    const clipConfigs = [
      {
        filename: filenames.thumb_front,
        clip: { x: 56.5, y: 40, width: 470, height: 520 }
      },
      {
        filename: filenames.thumb_back,
        clip: { x: 716.78, y: 40, width: 470, height: 520 }
      }
    ];

    await generateScreenshots(url, clipConfigs);

    res.status(200).jsonp(buildResponsePayload(filenames));
  } catch (error) {
    console.error('T-Shirt All-Over Print screenshot error:', error);
    res.status(500).json({ 
      error: 'Failed to generate screenshots',
      message: error.message 
    });
  }
});

module.exports = router;
