from django.db import models
from django.contrib.postgres.fields import ArrayField
import uuid
from User.models import BasicUser

# Get default user


def get_default_user():
    obj, created = BasicUser.objects.get_or_create(
        username="AnonymousUser",
        email="anonymoususer@example.com",
        first_name="Anonymous",
        last_name="User",
        defaults={'password': "pass"}
    )
    return obj


class Post(models.Model):
    title = models.CharField(
        verbose_name="Post Title",
        max_length=50,
        blank=False,
    )
    text = models.TextField(verbose_name="Post Body", blank=False)
    topics = ArrayField(models.CharField(  # TODO Change from array of topics to string of single topic
        verbose_name="Post Topics",
        max_length=50,
        blank=False,
    ))
    user = models.ForeignKey(
        "User.BasicUser", on_delete=models.CASCADE, editable=False, blank=True, null=True, related_name="posts")
    created_at = models.DateTimeField(auto_now_add=True, editable=False)
    post_as = models.CharField(
        verbose_name="Post as",
        max_length=50,
        blank=False,
        default="Anonymous User"
    )
    community = models.CharField(
        verbose_name="Community",
        max_length=50,
        blank=False,
        default="Miami"
    )

    class Meta:
        ordering = ("-created_at",)
        verbose_name = "Post"
        verbose_name_plural = "Posts"

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


def get_file_path(instance, filename):
    ext = filename.split('.')[-1]
    filename = "%s.%s" % (uuid.uuid4(), ext)
    return "files/"+filename


class PostComment(models.Model):
    text = models.TextField(verbose_name="Comment", blank=False)
    user = models.ForeignKey(
        "User.BasicUser", on_delete=models.CASCADE, editable=False, blank=True, null=True, related_name="comments")
    post = models.ForeignKey(
        Post, on_delete=models.CASCADE, blank=False, related_name="comments")
    created_at = models.DateTimeField(auto_now_add=True, editable=False)
    reply_as = models.CharField(
        verbose_name="Reply as",
        max_length=50,
        blank=False,
        default="Anonymous User"
    )

    class Meta:
        verbose_name = "Comment"
        verbose_name_plural = "Comments"


class PostFile(models.Model):
    file = models.FileField(
        verbose_name="File",
        upload_to=get_file_path,
        blank=False,
    )
    post = models.ForeignKey(
        Post, on_delete=models.CASCADE, blank=False, related_name="attachments")
    created_at = models.DateTimeField(auto_now_add=True, editable=False)

    class Meta:
        verbose_name = "Post File"
        verbose_name_plural = "Post Files"


class PostLikes(models.Model):
    post = models.ForeignKey(
        Post, on_delete=models.CASCADE, blank=False, related_name="likes")
    user = models.ForeignKey(
        "User.BasicUser", on_delete=models.CASCADE, blank=False)
    value = models.CharField(max_length=10, blank=False, choices=[
        ("+1", "Like"),
        ("-1", "Dislike"),
    ])

    class Meta:
        verbose_name = "Post Like"
        verbose_name_plural = "Post Likes"
