const Umre = require('../models/Umre');
const db = require('../config/database');

// Get all umre categories
exports.getAllCategories = async (req, res) => {
  try {
    const [categories] = await db.query(
      'SELECT * FROM umre_categories ORDER BY display_order ASC'
    );
    res.json(categories);
  } catch (error) {
    console.error(error);
    res.status(500).json({ message: 'Sunucu hatası' });
  }
};

// Create umre category
exports.createCategory = async (req, res) => {
  try {
    const { name, slug, description, image_url, status, display_order } = req.body;
    const [result] = await db.query(
      'INSERT INTO umre_categories (name, slug, description, image_url, status, display_order) VALUES (?, ?, ?, ?, ?, ?)',
      [name, slug, description, image_url, status || 'active', display_order || 0]
    );
    res.status(201).json({ message: 'Kategori oluşturuldu', id: result.insertId });
  } catch (error) {
    console.error(error);
    res.status(500).json({ message: 'Sunucu hatası' });
  }
};

// Update umre category
exports.updateCategory = async (req, res) => {
  try {
    const { name, slug, description, image_url, status, display_order } = req.body;
    const [result] = await db.query(
      'UPDATE umre_categories SET name = ?, slug = ?, description = ?, image_url = ?, status = ?, display_order = ? WHERE id = ?',
      [name, slug, description, image_url, status, display_order, req.params.id]
    );
    if (result.affectedRows === 0) {
      return res.status(404).json({ message: 'Kategori bulunamadı' });
    }
    res.json({ message: 'Kategori güncellendi' });
  } catch (error) {
    console.error(error);
    res.status(500).json({ message: 'Sunucu hatası' });
  }
};

// Delete umre category
exports.deleteCategory = async (req, res) => {
  try {
    const [result] = await db.query('DELETE FROM umre_categories WHERE id = ?', [req.params.id]);
    if (result.affectedRows === 0) {
      return res.status(404).json({ message: 'Kategori bulunamadı' });
    }
    res.json({ message: 'Kategori silindi' });
  } catch (error) {
    console.error(error);
    res.status(500).json({ message: 'Sunucu hatası' });
  }
};

// Get all umre sub-categories
exports.getAllSubCategories = async (req, res) => {
  try {
    const [subCategories] = await db.query(
      `SELECT sc.*, c.name as category_name, c.slug as category_slug
       FROM umre_sub_categories sc
       LEFT JOIN umre_categories c ON sc.category_id = c.id
       ORDER BY sc.display_order ASC`
    );
    res.json(subCategories);
  } catch (error) {
    console.error(error);
    res.status(500).json({ message: 'Sunucu hatası' });
  }
};

// Create umre sub-category
exports.createSubCategory = async (req, res) => {
  try {
    const { category_id, name, slug, description, image_url, status, display_order } = req.body;
    const [result] = await db.query(
      'INSERT INTO umre_sub_categories (category_id, name, slug, description, image_url, status, display_order) VALUES (?, ?, ?, ?, ?, ?, ?)',
      [category_id, name, slug, description, image_url, status || 'active', display_order || 0]
    );
    res.status(201).json({ message: 'Alt kategori oluşturuldu', id: result.insertId });
  } catch (error) {
    console.error(error);
    res.status(500).json({ message: 'Sunucu hatası' });
  }
};

// Update umre sub-category
exports.updateSubCategory = async (req, res) => {
  try {
    const { category_id, name, slug, description, image_url, status, display_order } = req.body;
    const [result] = await db.query(
      'UPDATE umre_sub_categories SET category_id = ?, name = ?, slug = ?, description = ?, image_url = ?, status = ?, display_order = ? WHERE id = ?',
      [category_id, name, slug, description, image_url, status, display_order, req.params.id]
    );
    if (result.affectedRows === 0) {
      return res.status(404).json({ message: 'Alt kategori bulunamadı' });
    }
    res.json({ message: 'Alt kategori güncellendi' });
  } catch (error) {
    console.error(error);
    res.status(500).json({ message: 'Sunucu hatası' });
  }
};

// Delete umre sub-category
exports.deleteSubCategory = async (req, res) => {
  try {
    const [result] = await db.query('DELETE FROM umre_sub_categories WHERE id = ?', [req.params.id]);
    if (result.affectedRows === 0) {
      return res.status(404).json({ message: 'Alt kategori bulunamadı' });
    }
    res.json({ message: 'Alt kategori silindi' });
  } catch (error) {
    console.error(error);
    res.status(500).json({ message: 'Sunucu hatası' });
  }
};

// Get umre tours with filters
exports.getAllUmreTours = async (req, res) => {
  try {
    const filters = {
      status: req.query.status,
      featured: req.query.featured === 'true',
      sub_category_id: req.query.sub_category_id,
      category_id: req.query.category_id
    };

    const tours = await Umre.getAll(filters);
    res.json(tours);
  } catch (error) {
    console.error(error);
    res.status(500).json({ message: 'Sunucu hatası' });
  }
};

// Get umre tour by ID
exports.getUmreTourById = async (req, res) => {
  try {
    const tour = await Umre.getById(req.params.id);
    if (!tour) {
      return res.status(404).json({ message: 'Umre turu bulunamadı' });
    }
    res.json(tour);
  } catch (error) {
    console.error(error);
    res.status(500).json({ message: 'Sunucu hatası' });
  }
};

// Get umre tour by slug
exports.getUmreTourBySlug = async (req, res) => {
  try {
    const tour = await Umre.getBySlug(req.params.slug);
    if (!tour) {
      return res.status(404).json({ message: 'Umre turu bulunamadı' });
    }
    res.json(tour);
  } catch (error) {
    console.error(error);
    res.status(500).json({ message: 'Sunucu hatası' });
  }
};

// Create umre tour
exports.createUmreTour = async (req, res) => {
  try {
    const tourId = await Umre.create(req.body);
    res.status(201).json({
      message: 'Umre turu oluşturuldu',
      id: tourId
    });
  } catch (error) {
    console.error(error);
    res.status(500).json({ message: 'Sunucu hatası' });
  }
};

// Update umre tour
exports.updateUmreTour = async (req, res) => {
  try {
    const updated = await Umre.update(req.params.id, req.body);
    if (!updated) {
      return res.status(404).json({ message: 'Umre turu bulunamadı' });
    }
    res.json({ message: 'Umre turu güncellendi' });
  } catch (error) {
    console.error(error);
    res.status(500).json({ message: 'Sunucu hatası' });
  }
};

// Delete umre tour
exports.deleteUmreTour = async (req, res) => {
  try {
    const deleted = await Umre.delete(req.params.id);
    if (!deleted) {
      return res.status(404).json({ message: 'Umre turu bulunamadı' });
    }
    res.json({ message: 'Umre turu silindi' });
  } catch (error) {
    console.error(error);
    res.status(500).json({ message: 'Sunucu hatası' });
  }
};

// Get featured umre tours
exports.getFeaturedUmreTours = async (req, res) => {
  try {
    const limit = parseInt(req.query.limit) || 6;
    const tours = await Umre.getFeatured(limit);
    res.json(tours);
  } catch (error) {
    console.error(error);
    res.status(500).json({ message: 'Sunucu hatası' });
  }
};

// Get upcoming umre tours (within 2 months)
exports.getUpcomingUmreTours = async (req, res) => {
  try {
    const limit = parseInt(req.query.limit) || 6;
    const tours = await Umre.getUpcoming(limit);
    res.json(tours);
  } catch (error) {
    console.error(error);
    res.status(500).json({ message: 'Sunucu hatası' });
  }
};
