const express = require('express');
const router = express.Router();
const { Service, Portfolio } = require('../models');
const { Op } = require('sequelize');

// Get all services
router.get('/', async (req, res) => {
  try {
    const category = req.query.category;
    const featured = req.query.featured;

    let whereClause = { isActive: true };
    
    if (category && category !== 'all') {
      whereClause.category = category;
    }
    
    if (featured === 'true') {
      whereClause.featured = true;
    }

    const services = await Service.findAll({
      where: whereClause,
      order: [['featured', 'DESC'], ['order', 'ASC']]
    });

    res.json({
      success: true,
      services
    });
  } catch (error) {
    console.error('Services API error:', error);
    res.status(500).json({
      success: false,
      message: 'Error fetching services'
    });
  }
});

// Get single service
router.get('/:id', async (req, res) => {
  try {
    const service = await Service.findByPk(req.params.id);
    
    if (!service || !service.isActive) {
      return res.status(404).json({
        success: false,
        message: 'Service not found'
      });
    }

    // Get related services
    const relatedServices = await Service.findAll({
      where: {
        id: { [Op.ne]: service.id },
        category: service.category,
        isActive: true
      },
      limit: 3
    });

    res.json({
      success: true,
      service,
      relatedServices
    });
  } catch (error) {
    console.error('Service detail API error:', error);
    res.status(500).json({
      success: false,
      message: 'Error fetching service'
    });
  }
});

// Get service categories
router.get('/meta/categories', async (req, res) => {
  try {
    const categories = await Service.distinct('category', { isActive: true });
    
    // Get count for each category
    const categoriesWithCount = await Promise.all(
      categories.map(async (category) => {
        const count = await Service.countDocuments({ 
          category, 
          isActive: true 
        });
        return { category, count };
      })
    );

    res.json({
      success: true,
      categories: categoriesWithCount
    });
  } catch (error) {
    console.error('Service categories API error:', error);
    res.status(500).json({
      success: false,
      message: 'Error fetching categories'
    });
  }
});

// Search services
router.get('/search/:term', async (req, res) => {
  try {
    const searchTerm = req.params.term;
    const limit = parseInt(req.query.limit) || 10;

    const services = await Service.findAll({
      where: {
        [Op.and]: [
          { isActive: true },
          {
            [Op.or]: [
              { name: { [Op.iLike]: `%${searchTerm}%` } },
              { description: { [Op.iLike]: `%${searchTerm}%` } },
              { tags: { [Op.contains]: [searchTerm] } }
            ]
          }
        ]
      },
      limit: limit
    });

    res.json({
      success: true,
      services,
      searchTerm,
      count: services.length
    });
  } catch (error) {
    console.error('Service search API error:', error);
    res.status(500).json({
      success: false,
      message: 'Error searching services'
    });
  }
});

module.exports = router;