const Hajj = require('../models/Hajj');

// Get all hajj tours
exports.getAllHajjTours = async (req, res) => {
  try {
    const filters = {
      status: req.query.status,
      featured: req.query.featured === 'true',
      vip: req.query.vip === 'true',
      registration_status: req.query.registration_status,
      upcoming: req.query.upcoming === 'true'
    };

    const tours = await Hajj.getAll(filters);
    res.json({ success: true, data: tours });
  } catch (error) {
    console.error('Error fetching hajj tours:', error);
    res.status(500).json({ success: false, message: 'Sunucu hatası', error: error.message });
  }
};

// Get hajj tour by ID
exports.getHajjTourById = async (req, res) => {
  try {
    const { id } = req.params;
    const tour = await Hajj.getById(id);

    if (!tour) {
      return res.status(404).json({ success: false, message: 'Tur bulunamadı' });
    }

    res.json({ success: true, data: tour });
  } catch (error) {
    console.error('Error fetching hajj tour by ID:', error);
    res.status(500).json({ success: false, message: 'Sunucu hatası', error: error.message });
  }
};

// Get hajj tour by slug
exports.getHajjTourBySlug = async (req, res) => {
  try {
    const { slug } = req.params;
    const tour = await Hajj.getBySlug(slug);

    if (!tour) {
      return res.status(404).json({ success: false, message: 'Tur bulunamadı' });
    }

    res.json({ success: true, data: tour });
  } catch (error) {
    console.error('Error fetching hajj tour by slug:', error);
    res.status(500).json({ success: false, message: 'Sunucu hatası', error: error.message });
  }
};

// Create hajj tour
exports.createHajjTour = async (req, res) => {
  try {
    const tour = await Hajj.create(req.body);
    res.status(201).json({ success: true, data: tour, message: 'Hac turu başarıyla oluşturuldu' });
  } catch (error) {
    console.error('Error creating hajj tour:', error);
    res.status(500).json({ success: false, message: 'Tur oluşturulamadı', error: error.message });
  }
};

// Update hajj tour
exports.updateHajjTour = async (req, res) => {
  try {
    const { id } = req.params;
    const tour = await Hajj.update(id, req.body);

    if (!tour) {
      return res.status(404).json({ success: false, message: 'Tur bulunamadı' });
    }

    res.json({ success: true, data: tour, message: 'Hac turu başarıyla güncellendi' });
  } catch (error) {
    console.error('Error updating hajj tour:', error);
    res.status(500).json({ success: false, message: 'Tur güncellenemedi', error: error.message });
  }
};

// Delete hajj tour
exports.deleteHajjTour = async (req, res) => {
  try {
    const { id } = req.params;
    const deleted = await Hajj.delete(id);

    if (!deleted) {
      return res.status(404).json({ success: false, message: 'Tur bulunamadı' });
    }

    res.json({ success: true, message: 'Hac turu başarıyla silindi' });
  } catch (error) {
    console.error('Error deleting hajj tour:', error);
    res.status(500).json({ success: false, message: 'Tur silinemedi', error: error.message });
  }
};

// Get featured hajj tours
exports.getFeaturedHajjTours = async (req, res) => {
  try {
    const limit = parseInt(req.query.limit) || 6;
    const tours = await Hajj.getAll({ status: 'active', featured: true });
    res.json({ success: true, data: tours.slice(0, limit) });
  } catch (error) {
    console.error('Error fetching featured hajj tours:', error);
    res.status(500).json({ success: false, message: 'Sunucu hatası', error: error.message });
  }
};

// Get upcoming hajj tours
exports.getUpcomingHajjTours = async (req, res) => {
  try {
    const tours = await Hajj.getAll({ status: 'active', upcoming: true });
    res.json({ success: true, data: tours });
  } catch (error) {
    console.error('Error fetching upcoming hajj tours:', error);
    res.status(500).json({ success: false, message: 'Sunucu hatası', error: error.message });
  }
};
