from django.db import models
from django.urls import reverse
from django.utils.text import slugify
from django_prose_editor.fields import ProseEditorField

from .base import validate_image_size


class CloudProvider(models.Model):
    name = models.CharField(max_length=100)
    slug = models.SlugField(unique=True)
    description = ProseEditorField()
    website = models.URLField()
    linkedin = models.URLField(blank=True)
    phone = models.CharField(max_length=25, blank=True, null=True)
    email = models.EmailField(max_length=254, blank=True, null=True)
    address = models.TextField(max_length=250, blank=True, null=True)
    logo = models.ImageField(
        upload_to="cloud_provider_logos/",
        validators=[validate_image_size],
        null=True,
        blank=True,
    )
    order = models.IntegerField(default=0)
    is_featured = models.BooleanField(default=False)
    disable_listing = models.BooleanField(default=False)

    class Meta:
        ordering = ["order"]

    def __str__(self):
        return self.name

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

    def get_absolute_url(self):
        return reverse("services:provider_detail", kwargs={"slug": self.slug})


class ConsultingPartner(models.Model):
    name = models.CharField(max_length=200)
    slug = models.SlugField(unique=True)
    description = ProseEditorField()
    logo = models.ImageField(
        upload_to="partner_logos/",
        validators=[validate_image_size],
        null=True,
        blank=True,
    )
    website = models.URLField(blank=True)
    linkedin = models.URLField(blank=True)
    phone = models.CharField(max_length=25, blank=True, null=True)
    email = models.EmailField(max_length=254, blank=True, null=True)
    address = models.TextField(max_length=250, blank=True, null=True)

    services = models.ManyToManyField(
        "services.Service", related_name="consulting_partners", blank=True
    )
    cloud_providers = models.ManyToManyField(
        CloudProvider, related_name="consulting_partners", blank=True
    )

    order = models.IntegerField(default=0)
    is_featured = models.BooleanField(default=False)
    disable_listing = models.BooleanField(default=False)

    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)

    class Meta:
        ordering = ["order"]

    def __str__(self):
        return self.name

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

    def get_absolute_url(self):
        return reverse("services:partner_detail", kwargs={"slug": self.slug})
