from django.db import models
from django.core.exceptions import ValidationError
from django.core.validators import MaxLengthValidator

#For Date Application
from datetime import date

#For Generating Random Numbers
import string
import random



class AboutSection(models.Model):
    
    about_title = models.TextField(
        "Luminant Cross Title",
        help_text="Enter the About Title text here.",
        default="Enter the About Title ",
        blank=True,
        null=True,
        validators=[MaxLengthValidator(500)]
    )
    
    
    mission = models.TextField(
        "Luminant Cross Mission",
        help_text="Enter the Mission text here.",
        default="Enter the Mission",
        blank=False,
        null=False,
        validators=[MaxLengthValidator(500)]
    )
    
    vision = models.TextField(
        "Luminant Cross Vision",
        help_text="Enter the 'Vision' text here.",
        default="Enter the Vision",
        blank=False,
        null=False,
        validators=[MaxLengthValidator(500)]
    )
    
    description = models.TextField(
        "Luminant Cross Description",
        help_text="Enter the 'About Description' text here.",
        default="Enter the Description",
        blank=False,
        null=False,
        validators=[MaxLengthValidator(500)]
    )

    
    about_page_background_image = models.ImageField(
        upload_to='about_background_images/',
        verbose_name="About Page Background Image",
        help_text="Upload a background image for the About page.",
        blank=True,  # Optional
        null=True    # Optional
    )
    
    def save(self, *args, **kwargs):
        if not self.pk and AboutSection.objects.exists():
            raise ValidationError("Only one About instance is allowed.")
        return super().save(*args, **kwargs)

    def __str__(self):
        return self.vision[:50]  # Show just the beginning of the vision in the admin list

class HomepageSection(models.Model):

    homepage_background_image = models.ImageField(
        upload_to='homepage_background_images/',
        verbose_name="Homepage Page Background Image",
        help_text="Upload a background image for the Homepage.",
        blank=True,  # Optional
        null=True    # Optional
    )
    
    homepage_daily_scripture = models.TextField(default="Do not be anxious about anything, but in every situation, by prayer and petition, with thanksgiving, present your requests to God. And the peace of God, which transcends all understanding, will guard your hearts and your minds in Christ Jesus")
    homepage_daily_scripture_bible_reading = models.TextField(default="Philippians 4:6-7 (NIV)")
    
    
    def save(self, *args, **kwargs):
        if not self.pk and HomepageSection.objects.exists():
            raise ValidationError("Only one Homepage instance is allowed.")
        return super().save(*args, **kwargs)

class Church(models.Model):
    name = models.CharField(max_length=200, unique=True)
    church_logo = models.ImageField(upload_to='church_logos/', null=True, blank=True)
    about = models.TextField()
    expectations = models.TextField(null=True, blank=True)
    address = models.CharField(max_length=200)
    city = models.CharField(max_length=100)
    state = models.CharField(max_length=100)
    country = models.CharField(max_length=100)
    postal_code = models.CharField(max_length=20, null=True, blank=True)
    latitude = models.TextField( null=True, blank=True)
    longitude = models.TextField( null=True, blank=True)
    email = models.EmailField()
    phone = models.CharField(max_length=20, null=True, blank=True)
    website = models.URLField(null=True, blank=True)
    social_media = models.TextField(null=True, blank=True)
    created_at = models.DateTimeField(auto_now_add=True)
    
    is_approved = models.BooleanField(default=False, null=False, blank=False)
    
    def __str__(self):
        return self.name

class ServiceTime(models.Model):
    DAY_CHOICES = [
        ('Sunday', 'Sunday'),
        ('Monday', 'Monday'),
        ('Tuesday', 'Tuesday'),
        ('Wednesday', 'Wednesday'),
        ('Thursday', 'Thursday'),
        ('Friday', 'Friday'),
        ('Saturday', 'Saturday'),
    ]
    
    church = models.ForeignKey(Church, on_delete=models.CASCADE, related_name='service_times')
    day = models.CharField(max_length=10, choices=DAY_CHOICES)
    start_time = models.TimeField()
    end_time = models.TimeField()
    service_type = models.CharField(max_length=100, null=True, blank=True)
    
    def __str__(self):
        return f"{self.church.name} - {self.day} {self.start_time} to {self.end_time}"

class Leader(models.Model):
    church = models.ForeignKey(Church, on_delete=models.CASCADE, related_name='leaders')
    name = models.CharField(max_length=100)
    position = models.CharField(max_length=100)
    description = models.TextField(null=True, blank=True)
    leader_image = models.ImageField(upload_to='leader_images/', null=True, blank=True)
    
    def __str__(self):
        return f"{self.name} ({self.position}) at {self.church.name}"
  
class ChurchComment(models.Model):
    unique_id = models.CharField(max_length=20, editable=False, unique=False)    
    church = models.ForeignKey(Church, on_delete=models.CASCADE, related_name='church_comment')
    senders_name = models.CharField(max_length=100)
    senders_comment = models.TextField()
    senders_rating = models.PositiveSmallIntegerField()
    date_submitted = models.DateField(default=date.today)  

    def __str__(self):
        return f"Comment by {self.senders_name} ({self.senders_rating}/10) on {self.date_submitted}"