const PhotoGallery = require('../models/PhotoGallery');
const GalleryPhoto = require('../models/GalleryPhoto');
const { sanitizeInput } = require('../utils/sanitize');

// Get all galleries
exports.getAllGalleries = async (req, res) => {
  try {
    const filters = {};
    if (req.query.status) filters.status = req.query.status;

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

// Get gallery by ID with photos
exports.getGalleryById = async (req, res) => {
  try {
    const gallery = await PhotoGallery.getWithPhotos(req.params.id);
    if (!gallery) {
      return res.status(404).json({ message: 'Galeri bulunamadı' });
    }
    res.json({ data: gallery });
  } catch (error) {
    console.error(error);
    res.status(500).json({ message: 'Sunucu hatası' });
  }
};

// Get gallery by slug with photos
exports.getGalleryBySlug = async (req, res) => {
  try {
    const gallery = await PhotoGallery.getBySlug(req.params.slug);
    if (!gallery) {
      return res.status(404).json({ message: 'Galeri bulunamadı' });
    }

    const photos = await GalleryPhoto.getByGalleryId(gallery.id);

    // Increment views
    await PhotoGallery.incrementViews(gallery.id);

    res.json({ data: { ...gallery, photos } });
  } catch (error) {
    console.error(error);
    res.status(500).json({ message: 'Sunucu hatası' });
  }
};

// Create gallery
exports.createGallery = async (req, res) => {
  try {
    const { title, slug, description, cover_image, status, display_order } = req.body;

    if (!title || !slug) {
      return res.status(400).json({ message: 'Başlık ve slug zorunludur' });
    }

    // Check slug uniqueness
    const slugExists = await PhotoGallery.checkSlugExists(slug);
    if (slugExists) {
      return res.status(400).json({ message: 'Bu slug zaten kullanılıyor' });
    }

    const sanitizedData = {
      title: sanitizeInput(title),
      slug: sanitizeInput(slug),
      description: description ? sanitizeInput(description) : null,
      cover_image,
      status: status || 'active',
      display_order: display_order || 0
    };

    const galleryId = await PhotoGallery.create(sanitizedData);
    res.status(201).json({
      message: 'Galeri oluşturuldu',
      id: galleryId
    });
  } catch (error) {
    console.error(error);
    res.status(500).json({ message: 'Sunucu hatası' });
  }
};

// Update gallery
exports.updateGallery = async (req, res) => {
  try {
    const { title, slug, description, cover_image, status, display_order } = req.body;

    if (!title || !slug) {
      return res.status(400).json({ message: 'Başlık ve slug zorunludur' });
    }

    // Check slug uniqueness
    const slugExists = await PhotoGallery.checkSlugExists(slug, req.params.id);
    if (slugExists) {
      return res.status(400).json({ message: 'Bu slug zaten kullanılıyor' });
    }

    const sanitizedData = {
      title: sanitizeInput(title),
      slug: sanitizeInput(slug),
      description: description ? sanitizeInput(description) : null,
      cover_image,
      status,
      display_order
    };

    const updated = await PhotoGallery.update(req.params.id, sanitizedData);
    if (!updated) {
      return res.status(404).json({ message: 'Galeri bulunamadı' });
    }

    res.json({ message: 'Galeri güncellendi' });
  } catch (error) {
    console.error(error);
    res.status(500).json({ message: 'Sunucu hatası' });
  }
};

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

// Add photo to gallery
exports.addPhoto = async (req, res) => {
  try {
    const { photo_url, title, description, display_order } = req.body;

    if (!photo_url) {
      return res.status(400).json({ message: 'Fotoğraf URL\'si zorunludur' });
    }

    const photoData = {
      gallery_id: req.params.id,
      photo_url,
      title: title ? sanitizeInput(title) : null,
      description: description ? sanitizeInput(description) : null,
      display_order: display_order || 0
    };

    const photoId = await GalleryPhoto.create(photoData);
    res.status(201).json({
      message: 'Fotoğraf eklendi',
      id: photoId
    });
  } catch (error) {
    console.error(error);
    res.status(500).json({ message: 'Sunucu hatası' });
  }
};

// Delete photo
exports.deletePhoto = async (req, res) => {
  try {
    const deleted = await GalleryPhoto.delete(req.params.photoId);
    if (!deleted) {
      return res.status(404).json({ message: 'Fotoğraf bulunamadı' });
    }
    res.json({ message: 'Fotoğraf silindi' });
  } catch (error) {
    console.error(error);
    res.status(500).json({ message: 'Sunucu hatası' });
  }
};
