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

exports.getAllTours = async (req, res) => {
  try {
    const { status, featured, sub_category_id, category_id } = req.query;

    const filters = {};
    if (status) filters.status = status;
    if (featured !== undefined) filters.featured = featured === 'true';
    if (sub_category_id) filters.sub_category_id = sub_category_id;
    if (category_id) filters.category_id = category_id;

    const tours = await Tour.getAll(filters);
    res.json(tours);
  } catch (error) {
    console.error('Get all tours error:', error);
    res.status(500).json({ message: 'Turlar alınırken hata oluştu' });
  }
};

exports.getTourById = async (req, res) => {
  try {
    const tour = await Tour.getById(req.params.id);
    if (!tour) {
      return res.status(404).json({ message: 'Tur bulunamadı' });
    }
    res.json(tour);
  } catch (error) {
    console.error('Get tour error:', error);
    res.status(500).json({ message: 'Tur alınırken hata oluştu' });
  }
};

exports.getTourBySlug = async (req, res) => {
  try {
    const tour = await Tour.getBySlug(req.params.slug);
    if (!tour) {
      return res.status(404).json({ message: 'Tur bulunamadı' });
    }
    res.json(tour);
  } catch (error) {
    console.error('Get tour by slug error:', error);
    res.status(500).json({ message: 'Tur alınırken hata oluştu' });
  }
};

exports.getFeaturedTours = async (req, res) => {
  try {
    const limit = parseInt(req.query.limit) || 6;
    const tours = await Tour.getFeatured(limit);
    res.json(tours);
  } catch (error) {
    console.error('Get featured tours error:', error);
    res.status(500).json({ message: 'Öne çıkan turlar alınırken hata oluştu' });
  }
};

exports.createTour = async (req, res) => {
  try {
    const {
      sub_category_id, airline_id, title, slug, description, short_description,
      main_image, gallery, duration, tour_date, return_date, accommodation_type, route,
      tour_dates, registration_status, important_dates, hotel_info,
      pricing, included_services, excluded_services, itinerary,
      max_people, featured, status
    } = req.body;

    if (!sub_category_id || !title || !slug) {
      return res.status(400).json({ message: 'Alt kategori, başlık ve slug alanları zorunludur' });
    }

    const tourId = await Tour.create({
      sub_category_id,
      airline_id,
      title,
      slug,
      description,
      short_description,
      main_image,
      gallery,
      duration,
      tour_date,
      return_date,
      accommodation_type,
      route,
      tour_dates,
      registration_status,
      important_dates,
      hotel_info,
      pricing,
      included_services,
      excluded_services,
      itinerary,
      max_people,
      featured,
      status
    });

    res.status(201).json({
      message: 'Tur başarıyla oluşturuldu',
      tourId
    });
  } catch (error) {
    console.error('Create tour error:', error);
    if (error.code === 'ER_DUP_ENTRY') {
      return res.status(400).json({ message: 'Bu slug zaten kullanımda' });
    }
    res.status(500).json({ message: 'Tur oluşturulurken hata oluştu' });
  }
};

exports.updateTour = async (req, res) => {
  try {
    const {
      sub_category_id, airline_id, title, slug, description, short_description,
      main_image, gallery, duration, tour_date, return_date, accommodation_type, route,
      tour_dates, registration_status, important_dates, hotel_info,
      pricing, included_services, excluded_services, itinerary,
      max_people, featured, status
    } = req.body;

    if (!sub_category_id || !title || !slug) {
      return res.status(400).json({ message: 'Alt kategori, başlık ve slug alanları zorunludur' });
    }

    const updated = await Tour.update(req.params.id, {
      sub_category_id,
      airline_id,
      title,
      slug,
      description,
      short_description,
      main_image,
      gallery,
      duration,
      tour_date,
      return_date,
      accommodation_type,
      route,
      tour_dates,
      registration_status,
      important_dates,
      hotel_info,
      pricing,
      included_services,
      excluded_services,
      itinerary,
      max_people,
      featured,
      status
    });

    if (!updated) {
      return res.status(404).json({ message: 'Tur bulunamadı' });
    }

    res.json({ message: 'Tur başarıyla güncellendi' });
  } catch (error) {
    console.error('Update tour error:', error);
    if (error.code === 'ER_DUP_ENTRY') {
      return res.status(400).json({ message: 'Bu slug zaten kullanımda' });
    }
    res.status(500).json({ message: 'Tur güncellenirken hata oluştu' });
  }
};

exports.deleteTour = async (req, res) => {
  try {
    const deleted = await Tour.delete(req.params.id);

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

    res.json({ message: 'Tur başarıyla silindi' });
  } catch (error) {
    console.error('Delete tour error:', error);
    res.status(500).json({ message: 'Tur silinirken hata oluştu' });
  }
};

exports.getNearestUmrahTours = async (req, res) => {
  try {
    const tours = await Tour.getNearestUmrahTours();
    res.json(tours);
  } catch (error) {
    console.error('Get nearest umrah tours error:', error);
    res.status(500).json({ message: 'Yakın tarihli umre turları alınırken hata oluştu' });
  }
};
