from django.db import models
from PIL import Image
from io import BytesIO
from django.core.files.base import ContentFile


class LandingContent(models.Model):
    landing_image = models.ImageField(default='landing_pic.webp', upload_to='landing_assets')
    first_banner = models.ImageField(default='banner_img1.webp', upload_to='landing_assets')
    second_banner = models.ImageField(default='banner_img2.webp', upload_to='landing_assets')
    third_banner = models.ImageField(default='banner_img3.webp', upload_to='landing_assets')

    class Meta:
        verbose_name = 'Landing Page Content'
        verbose_name_plural = 'Landing Page Contents'

    def __str__(self):
        return 'Landing Page Imagery'

    def save(self, *args, **kwargs):
        super().save(*args, **kwargs)

        # Define max size for images
        max_size = (1440, 1440)

        # List of image fields to process
        image_fields = ['landing_image', 'first_banner', 'second_banner', 'third_banner']

        for field_name in image_fields:
            image_field = getattr(self, field_name)

            if image_field and hasattr(image_field, 'path'):  # Ensure image exists
                img = Image.open(image_field.path)

                if img.height > max_size[1] or img.width > max_size[0]:
                    img.thumbnail(max_size)
                    img.save(image_field.path)

class AboutContent(models.Model):
    whatWeDo_content = models.TextField()
    whatWeDo_picture = models.ImageField(default='about_what_pic.webp', upload_to='about_assets')
    cd_picture = models.ImageField(default='cd_image.webp', upload_to='about_assets')
    cd_message = models.TextField()
    whoWeAre_banner = models.ImageField(default='banner.webp', upload_to='about_assets')
    ec_content = models.TextField()
    ec_picture = models.ImageField(default='banner.webp', upload_to='about_assets')
    ec_show = models.BooleanField(default=False)

    class Meta:
        verbose_name = 'About Us Page Content'
        verbose_name_plural = 'About Us Page Content'

    def __str__(self):
        return 'About Page Content + Imagery'
    
    def save(self, *args, **kwargs):
        super().save(*args, **kwargs)

        # Define max size for images
        max_size = (1440, 1440)

        # List of image fields to process
        image_fields = ['whatWeDo_picture', 'cd_picture', 'whoWeAre_banner', 'ec_picture']

        for field_name in image_fields:
            image_field = getattr(self, field_name)

            if image_field and hasattr(image_field, 'path'):  # Ensure image exists
                img = Image.open(image_field.path)

                if img.height > max_size[1] or img.width > max_size[0]:
                    img.thumbnail(max_size)
                    img.save(image_field.path)

class WaterContent(models.Model):
    water_landing_picture = models.ImageField(default='water_banner.webp', upload_to='WEP_assets')
    water_landing_content = models.TextField()
    training_picture = models.ImageField(default='training_banner.webp', upload_to='WEP_assets')
    training_content = models.TextField()
    applied_research_picture = models.ImageField(default='applied_research_banner.webp', upload_to='WEP_assets')
    applied_research_content = models.TextField()
    fellowships_picture = models.ImageField(default='fellowships_picture.webp', upload_to='WEP_assets')
    fellowships_content = models.TextField()

    class Meta:
        verbose_name = 'Water Pages Content'
        verbose_name_plural = 'Water Pages Content'

    def __str__(self):
        return 'Water Pages + Imagery'
    
    def save(self, *args, **kwargs):
        super().save(*args, **kwargs)

        # Define max size for images
        max_size = (1440, 1440)

        # List of image fields to process
        image_fields = ['water_landing_picture', 'training_picture', 'applied_research_picture', 'fellowships_picture']

        for field_name in image_fields:
            image_field = getattr(self, field_name)

            if image_field and hasattr(image_field, 'path'):  # Ensure image exists
                img = Image.open(image_field.path)

                if img.height > max_size[1] or img.width > max_size[0]:
                    img.thumbnail(max_size)
                    img.save(image_field.path)

class EnvironmentContent(models.Model):
    env_picture = models.ImageField(default='environment_banner.webp', upload_to='WEP_assets')
    env_content = models.TextField()
    climate_change_picture = models.ImageField(default='climate_banner.webp', upload_to='WEP_assets')
    climate_change_content = models.TextField()
    WEF_picture = models.ImageField(default='WEF_banner.webp', upload_to='WEP_assets')
    WEF_content = models.TextField()

    class Meta:
        verbose_name = 'Environment Pages Content'
        verbose_name_plural = 'Environment Pages Content'

    def __str__(self):
        return 'Environment Pages + Imagery'
    
    def save(self, *args, **kwargs):
        super().save(*args, **kwargs)

        # Define max size for images
        max_size = (1440, 1440)

        # List of image fields to process
        image_fields = ['env_picture', 'climate_change_picture', 'WEF_picture']

        for field_name in image_fields:
            image_field = getattr(self, field_name)

            if image_field and hasattr(image_field, 'path'):  # Ensure image exists
                img = Image.open(image_field.path)

                if img.height > max_size[1] or img.width > max_size[0]:
                    img.thumbnail(max_size)
                    img.save(image_field.path)

class PeaceContent(models.Model):
    peace_picture = models.ImageField(default='peace_banner.webp', upload_to='WEP_assets')
    peace_content = models.TextField()
    MEDRC_model_picture = models.ImageField(default='MEDRC_model_banner.webp', upload_to='WEP_assets')
    MEDRC_model_content = models.TextField()
    history_picture = models.ImageField(default='history_banner.webp', upload_to='WEP_assets')
    history_content = models.TextField()
    
    class Meta:
        verbose_name = 'Peace Pages Content'
        verbose_name_plural = 'Peace Pages Content'

    def __str__(self):
        return 'Peace Pages + Imagery'
    
    def save(self, *args, **kwargs):
        super().save(*args, **kwargs)

        # Define max size for images
        max_size = (1440, 1440)

        # List of image fields to process
        image_fields = ['peace_picture', 'MEDRC_model_picture', 'history_picture']

        for field_name in image_fields:
            image_field = getattr(self, field_name)

            if image_field and hasattr(image_field, 'path'):  # Ensure image exists
                img = Image.open(image_field.path)

                if img.height > max_size[1] or img.width > max_size[0]:
                    img.thumbnail(max_size)
                    img.save(image_field.path)

class Publications(models.Model):
    publication_name = models.CharField(max_length=100, default=None)
    publication_water = models.BooleanField(default=False)
    publication_environment = models.BooleanField(default=False)
    publication_peace = models.BooleanField(default=False)
    publication_blurb = models.CharField(max_length=250, blank=True)
    pdf = models.FileField(default='MEDRC_Publication', upload_to='publications')
    publication_cover = models.ImageField(upload_to='publications')

    class Meta:
        verbose_name = 'Publication'
        verbose_name_plural = 'Publications'

    def __str__(self):
        return self.publication_name + ' publication'

    def save(self, *args, **kwargs):
        super().save(*args, **kwargs)
        if self.publication_cover:
            img = Image.open(self.publication_cover.path)
            output_size = (320, 400)
            img.thumbnail(output_size)
            img.save(self.publication_cover.path) # this overrides the larger image
