from autoslug import AutoSlugField
from django.db import models
from imagekit.models import ProcessedImageField
from tinymce.models import HTMLField
from core.utils import image_optimizer


# Create your models here.
class Tags(models.Model):
    aktif = (
        (True, 'Evet'),
        (False, 'Hayır'),
    )
    tag = models.CharField(max_length=254, verbose_name="Post Tagları")
    created_at = models.DateTimeField(auto_now_add=True, editable=False, verbose_name="Oluşturulma Tarihi")
    updated_at = models.DateTimeField(auto_now=True, editable=False, verbose_name="Güncelleme Tarihi")
    is_active = models.BooleanField(default=True, verbose_name='Yayındamı', choices=aktif)

    class Meta:
        ordering = ["-created_at"]
        db_table = 'tags'
        verbose_name_plural = "Post Tagları"
        verbose_name = "Post Tagı"

    def __str__(self):
        return self.tag


class Home(models.Model):
    aktif = (
        (True, 'Evet'),
        (False, 'Hayır'),
    )

    name = models.CharField(max_length=254, verbose_name="İsim Soyisim ")
    title = models.CharField(max_length=254, verbose_name="Alt Yazi")
    button1 = models.CharField(max_length=254, verbose_name="Button 1")
    button2 = models.CharField(max_length=254, verbose_name="Button 2")
    video = models.URLField(verbose_name="Video Url", null=True, blank=True,
                            default='https://www.youtube.com/watch?v=6zM4p_A0ISk')
    keywords = models.CharField(max_length=254, verbose_name="Seo Kelimeleri Aralarına Virgül Koyunuz")
    tags = models.ManyToManyField(Tags, verbose_name="Post Tagları", related_name='tags')
    image = ProcessedImageField(**image_optimizer('uploads/post', 400, 400, 90, 'avif'), null=True, blank=True)
    slug = AutoSlugField(populate_from='name', null=False, unique=True, editable=True, db_index=True, max_length=250,
                         blank=True)
    created_at = models.DateTimeField(auto_now_add=True, editable=False, verbose_name="Oluşturulma Tarihi")
    updated_at = models.DateTimeField(auto_now=True, editable=False, verbose_name="Güncelleme Tarihi")
    is_active = models.BooleanField(default=False, verbose_name='Yayındamı ?', choices=aktif)

    class Meta:
        ordering = ["created_at"]
        db_table = 'homes'
        verbose_name_plural = "Homes"
        verbose_name = "Home"
        unique_together = ('slug',)

    def get_slug(self):
        slug = self.name.replace('ı', "i").replace('İ', 'i')
        number = 1
        while Home.objects.filter(slug=slug).exists():
            slug = '{}-{}'.format(slug, number)
            number += 1
        return slug

    def save(self, *args, **kwargs):
        if not self.slug:
            self.slug = self.get_slug()
        super().save(*args, **kwargs)

    def __str__(self):
        return f"Homes:  {self.name}"


class AboutMe(models.Model):
    aktif = (
        (True, 'Evet'),
        (False, 'Hayır'),
    )

    title = models.CharField(max_length=254, verbose_name="Hakkımda")
    image = ProcessedImageField(**image_optimizer('uploads/post', 400, 400, 90, 'avif'), null=True, blank=True)
    image_sub = HTMLField(blank=True, null=True, verbose_name='Resim Altındaki Yazı İçeriği')
    cv = models.FileField(upload_to='uploads/cv', null=True, blank=True)
    birthday = models.CharField(max_length=254, verbose_name="Yaş Günü", null=True, blank=True)
    city = models.CharField(max_length=254, verbose_name="Şehir", null=True, blank=True)
    study = models.CharField(max_length=254, verbose_name="Okulun", null=True, blank=True)
    website = models.CharField(max_length=254, verbose_name="Web Sitesi", null=True, blank=True)
    phone = models.CharField(max_length=254, verbose_name="Telefon", null=True, blank=True)
    age = models.CharField(max_length=5, verbose_name="Yaş", null=True, blank=True)
    interests = models.CharField(max_length=254, verbose_name="İlgi alanları", null=True, blank=True)
    degree = models.CharField(max_length=254, verbose_name="Derecelendirme", null=True, blank=True)
    x = models.CharField(max_length=254, verbose_name="x", null=True, blank=True)
    mail = models.CharField(max_length=254, verbose_name="E-Posta", null=True, blank=True)

    done = models.IntegerField(verbose_name="Tamamlanan Projeler", null=True, blank=True)
    project_done = models.CharField(max_length=254, verbose_name="Tamalangan Projeler", null=True, blank=True)
    user_h = models.IntegerField(verbose_name="Mutlu Kullanıcı Sayısı", null=True, blank=True)
    hapy_user = models.CharField(max_length=254, verbose_name="Mutlu Kullanıcı", null=True, blank=True)
    great = models.IntegerField(verbose_name="Yorum Sayısı", null=True, blank=True)
    great_reviews = models.CharField(max_length=254, verbose_name="Yorumlar", null=True, blank=True)
    team = models.IntegerField(verbose_name="Destek Takımı Sayısı", null=True, blank=True)
    support_team = models.CharField(max_length=254, verbose_name="Destek Takımı", null=True, blank=True)

    slug = AutoSlugField(populate_from='name', null=False, unique=True, editable=True, db_index=True, max_length=250,
                         blank=True)
    created_at = models.DateTimeField(auto_now_add=True, editable=False, verbose_name="Oluşturulma Tarihi")
    updated_at = models.DateTimeField(auto_now=True, editable=False, verbose_name="Güncelleme Tarihi")
    is_active = models.BooleanField(default=False, verbose_name='Hakkımda Yayındamı ?', choices=aktif)
    counter_active = models.BooleanField(default=False, verbose_name='Sayaç Yayındamı ?', choices=aktif)

    class Meta:
        ordering = ["created_at"]
        db_table = 'about'
        verbose_name_plural = "About Me"
        verbose_name = "About"
        unique_together = ('slug',)

    def get_slug(self):
        slug = self.title.replace('ı', "i").replace('İ', 'i')
        number = 1
        while AboutMe.objects.filter(slug=slug).exists():
            slug = '{}-{}'.format(slug, number)
            number += 1
        return slug

    def save(self, *args, **kwargs):
        if not self.slug:
            self.slug = self.get_slug()
        super().save(*args, **kwargs)

    def __str__(self):
        return f"About Me:  {self.title}"


class MyService(models.Model):
    aktif = (
        (True, 'Evet'),
        (False, 'Hayır'),
    )

    title = models.CharField(max_length=254, verbose_name="Servis Başlığı ")
    content = HTMLField(blank=True, null=True, verbose_name='İçerik')
    image = ProcessedImageField(**image_optimizer('uploads/services', 256, 256, 90, 'png'), null=True, blank=True)
    slug = AutoSlugField(populate_from='name', null=False, unique=True, editable=True, db_index=True, max_length=250,
                         blank=True)
    created_at = models.DateTimeField(auto_now_add=True, editable=False, verbose_name="Oluşturulma Tarihi")
    updated_at = models.DateTimeField(auto_now=True, editable=False, verbose_name="Güncelleme Tarihi")
    is_active = models.BooleanField(default=False, verbose_name='Yayındamı ?', choices=aktif)

    class Meta:
        ordering = ["created_at"]
        db_table = 'services'
        verbose_name_plural = "Servislerim"
        verbose_name = "Servis"
        unique_together = ('slug',)

    def get_slug(self):
        slug = self.title.replace('ı', "i").replace('İ', 'i')
        number = 1
        while Home.objects.filter(slug=slug).exists():
            slug = '{}-{}'.format(slug, number)
            number += 1
        return slug

    def save(self, *args, **kwargs):
        if not self.slug:
            self.slug = self.get_slug()
        super().save(*args, **kwargs)

    def __str__(self):
        return f"Services:  {self.title}"


class MyServiceTitle(models.Model):
    aktif = (
        (True, 'Evet'),
        (False, 'Hayır'),
    )

    title = models.CharField(max_length=254, verbose_name="Servis Başlığı ")
    title_sub = models.CharField(max_length=254, verbose_name="Servis Başlığı ")
    created_at = models.DateTimeField(auto_now_add=True, editable=False, verbose_name="Oluşturulma Tarihi")
    updated_at = models.DateTimeField(auto_now=True, editable=False, verbose_name="Güncelleme Tarihi")
    is_active = models.BooleanField(default=False, verbose_name='Yayındamı ?', choices=aktif)

    class Meta:
        ordering = ["created_at"]
        db_table = 'services_title'
        verbose_name_plural = "Servislerim Başlıkları"
        verbose_name = "Servis Başlığı"

    def __str__(self):
        return f"Services:  {self.title}"


class MyResume(models.Model):
    aktif = (
        (True, 'Evet'),
        (False, 'Hayır'),
    )

    title = models.CharField(max_length=254, verbose_name="Başlık ")
    title_sub = models.CharField(max_length=254, verbose_name="Alt Başlığı ")
    education = models.CharField(max_length=100, verbose_name="Education", blank=True, null=True, default='Education')
    experience = models.CharField(max_length=100, verbose_name="Experience ", blank=True, null=True,
                                  default='Experience')
    coding_skills = models.CharField(max_length=100, verbose_name="Coding Skills ", blank=True, null=True,
                                     default='Coding Skills')
    knowledge = models.CharField(max_length=100, verbose_name="Knowledge ", blank=True, null=True, default='Knowledge')
    created_at = models.DateTimeField(auto_now_add=True, editable=False, verbose_name="Oluşturulma Tarihi")
    updated_at = models.DateTimeField(auto_now=True, editable=False, verbose_name="Güncelleme Tarihi")
    is_active = models.BooleanField(default=False, verbose_name='Yayındamı ?', choices=aktif)

    class Meta:
        ordering = ["created_at"]
        db_table = 'resumes'
        verbose_name_plural = "MyResume"
        verbose_name = "MyResume"

    def __str__(self):
        return f"MyResume:  {self.title}"


class Education(models.Model):
    aktif = (
        (True, 'Evet'),
        (False, 'Hayır'),
    )
    between_years = models.CharField(max_length=50, verbose_name="Yıllar Arasında ")
    title = models.CharField(max_length=100, verbose_name="Başlık ")
    content = models.CharField(max_length=150, verbose_name="İçerik")
    resume = models.ForeignKey(MyResume, on_delete=models.CASCADE, null=True, blank=True, related_name='edu_resume')
    created_at = models.DateTimeField(auto_now_add=True, editable=False, verbose_name="Oluşturulma Tarihi")
    updated_at = models.DateTimeField(auto_now=True, editable=False, verbose_name="Güncelleme Tarihi")
    is_active = models.BooleanField(default=False, verbose_name='Yayındamı ?', choices=aktif)

    class Meta:
        ordering = ["created_at"]
        db_table = 'educations'
        verbose_name_plural = "Education"
        verbose_name = "Education "

    def __str__(self):
        return f"Education:  {self.title}"


class Experience(models.Model):
    aktif = (
        (True, 'Evet'),
        (False, 'Hayır'),
    )
    between_years = models.CharField(max_length=50, verbose_name="Yıllar Arasında ")
    title = models.CharField(max_length=100, verbose_name="Başlık ")
    content = models.CharField(max_length=150, verbose_name="İçerik")
    resume = models.ForeignKey(MyResume, on_delete=models.CASCADE, null=True, blank=True, related_name='exp_resume')
    created_at = models.DateTimeField(auto_now_add=True, editable=False, verbose_name="Oluşturulma Tarihi")
    updated_at = models.DateTimeField(auto_now=True, editable=False, verbose_name="Güncelleme Tarihi")
    is_active = models.BooleanField(default=False, verbose_name='Yayındamı ?', choices=aktif)

    class Meta:
        ordering = ["created_at"]
        db_table = 'experience'
        verbose_name_plural = "Experience "
        verbose_name = "Experience "

    def __str__(self):
        return f"Experience:  {self.title}"


class Skill(models.Model):
    aktif = (
        (True, 'Evet'),
        (False, 'Hayır'),
    )
    title = models.CharField(max_length=100, verbose_name="Başlık")
    degree = models.IntegerField(verbose_name="Derecelendirme ")
    resume = models.ForeignKey(MyResume, on_delete=models.CASCADE, null=True, blank=True, related_name='skill_resume')
    created_at = models.DateTimeField(auto_now_add=True, editable=False, verbose_name="Oluşturulma Tarihi")
    updated_at = models.DateTimeField(auto_now=True, editable=False, verbose_name="Güncelleme Tarihi")
    is_active = models.BooleanField(default=False, verbose_name='Yayındamı ?', choices=aktif)

    class Meta:
        ordering = ["created_at"]
        db_table = 'skills'
        verbose_name_plural = "Skill"
        verbose_name = "Skill"

    def __str__(self):
        return f"Skill:  {self.title}"


class Knowledge(models.Model):
    aktif = (
        (True, 'Evet'),
        (False, 'Hayır'),
    )
    title = models.CharField(max_length=100, verbose_name="Başlık ")
    created_at = models.DateTimeField(auto_now_add=True, editable=False, verbose_name="Oluşturulma Tarihi")
    updated_at = models.DateTimeField(auto_now=True, editable=False, verbose_name="Güncelleme Tarihi")
    is_active = models.BooleanField(default=False, verbose_name='Yayındamı ?', choices=aktif)
    resume = models.ForeignKey(MyResume, on_delete=models.CASCADE, null=True, blank=True, related_name='know_resume')

    class Meta:
        ordering = ["created_at"]
        db_table = 'knowledges'
        verbose_name_plural = "Knowledge"
        verbose_name = "Knowledge"

    def __str__(self):
        return f"Knowledge:  {self.title}"

class MainMenu(models.Model):
    aktif = (
        (True, 'Evet'),
        (False, 'Hayır'),
    )
    home = models.CharField(max_length=100, verbose_name="Home ")
    about = models.CharField(max_length=100, verbose_name="About ")
    services = models.CharField(max_length=100, verbose_name="Services ")
    resume = models.CharField(max_length=100, verbose_name="Resume ")
    portfolio = models.CharField(max_length=100, verbose_name="Portfolio ")
    contact = models.CharField(max_length=100, verbose_name="Contact ")
    created_at = models.DateTimeField(auto_now_add=True, editable=False, verbose_name="Oluşturulma Tarihi")
    updated_at = models.DateTimeField(auto_now=True, editable=False, verbose_name="Güncelleme Tarihi")
    is_active = models.BooleanField(default=False, verbose_name='Yayındamı ?', choices=aktif)

    class Meta:
        ordering = ["created_at"]
        db_table = 'mane_menu'
        verbose_name_plural = "MainMenu"
        verbose_name = "MainMenu"

    def __str__(self):
        return f"MainMenu:  {self.home}"
"""
education = models.ForeignKey(Education, on_delete=models.CASCADE, null=True, blank=True)
experience = models.ForeignKey(Experience, on_delete=models.CASCADE, null=True, blank=True)
knowledge = models.ForeignKey(Knowledge, on_delete=models.CASCADE, null=True, blank=True)
skill = models.ForeignKey(Skill, on_delete=models.CASCADE, null=True, blank=True)
"""
