from django.db import models
from django.core.files.storage import FileSystemStorage
from django.contrib.auth.models import User

fs = FileSystemStorage(location='/media/flyers')
# Create your models here.

class Calendar(models.Model):
    name = models.CharField(max_length=31, unique=True)
    shortcode = models.CharField(max_length=3, unique=True)
    desc = models.TextField(blank=True, null=True)

    def __unicode__(self):
        return "%s" % self.shortcode

    def __str__(self):
        return u'%s' % self.shortcode


class Scraper(models.Model):
    name = models.CharField(max_length=63, unique=True)
    website = models.CharField(max_length=63, blank=True, null=True)
    calendar = models.ForeignKey(Calendar, on_delete=models.CASCADE)
    items = models.IntegerField(blank=True, null=True)
    new_items = models.IntegerField(blank=True, null=True)
    last_ran = models.DateTimeField(blank=True, null=True)

    class Meta:
        verbose_name_plural = "Scrapers"
        ordering = ['name',]

    def __unicode__(self):
        return "%s" % self.name

    def __str__(self):
        return u'%s' % self.name


class Tags(models.Model):
    name = models.CharField(max_length=31, unique=True)
    desc = models.TextField(blank=True, null=True)

    def __unicode__(self):
        return "%s" % self.name

    def __str__(self):
        return u'%s' % self.name


class Organization(models.Model):
    MEMBER_TYPE = (
            ('Nm', 'Non-Member'),
            ('Na', 'Nano Member'),
            ('Mm', 'Micro Member'),
            ('Sm', 'Small Business Member'),
            ('Lb', 'Local Business Member'),
            ('Rb', 'Regional Business Member'),
        )
    ORG_TYPE = (
            ('Fo', 'Food'),
            ('Re', 'Retail'),
            ('Se', 'Service'),
            ('Ud', 'Undefined'),
        )
    name = models.CharField(max_length=63)
    website = models.CharField(max_length=126, blank=True, null=True)
    membership = models.CharField(max_length=31, choices=MEMBER_TYPE, default='0')
    org_type = models.CharField(max_length=31, choices=ORG_TYPE, default='3')

    stripe_email = models.CharField(max_length=63, blank=True, null=True)
    ein = models.CharField(max_length=15, blank=True, null=True)
    is_venue= models.BooleanField(default=False)
    is_501c = models.BooleanField(default=False)

    short_desc = models.CharField(max_length=63, blank=True, null=True)
    long_desc = models.TextField(blank=True, null=True)

    contact_name = models.CharField(max_length=63, blank=True, null=True)
    contact_email = models.CharField(max_length=63, blank=True, null=True)

    phone_number = models.CharField(max_length=255, blank=True, null=True)
    address = models.CharField(max_length=63, blank=True, null=True)
    city = models.CharField(max_length=31, blank=True, null=True)
    state = models.CharField(max_length=15, blank=True, null=True)
    zip_code = models.CharField(max_length=15, blank=True, null=True)

    tags = models.ManyToManyField(Tags, blank=True, null=True)

    class Meta:
        unique_together = ("name", "is_venue")
        verbose_name_plural = "Organizations"
        ordering = ['name']

    def __unicode__(self):
        return "%s" % self.name

    def __str__(self):
        return u'%s' % self.name


class Event(models.Model):
    EVENT_TYPE = (
        ('Ot', 'Other'),
        ('Mu', 'Music'),
        ('Va', 'Visual Art'),
        ('Gv', 'Government'),
        ('Ce', 'Civic Engagement'),
        ('Ed', 'Educational'),
        ('Ma', 'Mutual Aid'),
        ('Th', 'Theater'),
        ('Co', 'Comedy'),
    )
    EVENT_STATE = (
        ('live', 'Live & Direct'),
        ('virt', 'Virtually Served'),
        ('both', 'How you prefer.')
    )
    calendar = models.ForeignKey(Calendar, on_delete=models.CASCADE, blank=True, null=True)
    scraper = models.ForeignKey(Scraper, on_delete=models.CASCADE, null=True)
    venue = models.ForeignKey(Organization, on_delete=models.CASCADE)
    live = models.CharField(max_length=4, choices=EVENT_STATE, default='live')
    event_type = models.CharField(max_length=15, choices=EVENT_TYPE, default='Mu') 
    show_title = models.CharField(max_length=127, blank=True, null=True)
    show_link = models.URLField(blank=True, null=True)
    guests = models.CharField(max_length=255, blank=True, null=True)
    show_date = models.DateTimeField()
    show_day = models.DateField(blank=True, null=True)
    img_link = models.CharField(max_length=255, blank=True, null=True)
    flyer_img = models.ImageField(upload_to=fs, blank=True, null=True)
    more_details = models.JSONField(blank=True, null=True)

    tags = models.ManyToManyField(Tags, blank=True, null=True)

    class Meta:
        verbose_name_plural = "Events"
        # unique_together = ("show_title", "show_date", "venue")
        ordering = ['show_date', 'show_title']

    def __unicode__(self):
        return "%s" % self.show_title

    def __str__(self):
        return u'%s' % self.show_title


class Promo(models.Model):
    PROMO_TYPE = (
            ('Ar', 'Art'),
            ('Fo', 'Food'),
            ('Ev', 'Event'),
            ('Re', 'Retail'),
            ('Ma', 'Mutual Aid'),
            ('Ca', 'Classifieds'),
            ('Jo', 'Job Opening'),
            ('Sp', 'Startup Pitch'),
            ('An', 'Academia Nuts'),
            ('Ja', 'Journal Article'),
            ('Su', 'Survey Questions')
        )
    title = models.CharField(max_length=63)
    organization = models.ForeignKey(Organization, on_delete=models.CASCADE)
    promo_type = models.CharField(max_length=15, choices=PROMO_TYPE, default='0')
    overlay_image = models.ImageField(upload_to="overlays", blank=True)
    classified_image = models.ImageField(upload_to="classifieds", blank=True)
    embed_link = models.CharField(max_length=126, blank=True, null=True)
    short_text = models.TextField(blank=True, null=True)
    long_text = models.TextField(blank=True, null=True)
    target_link = models.URLField(blank=True, null=True)
    notes = models.TextField(blank=True, null=True)
    published = models.BooleanField(default=False)

    tags = models.ManyToManyField(Tags, blank=True, null=True)

    class Meta:
        verbose_name_plural = "Promo"
        ordering = ['published', 'organization', 'title',]

    def __unicode__(self):
        return "%s" % self.title

    def __str__(self):
        return u'%s' % self.title


# class UserThrottle(models.Model):
#     user = models.ForeignKey(User, on_delete=models.CASCADE)
#     scope = models.CharField(max_length=20, choices=(
#         ('admin', 'Admin'),
#         ('platinum', 'Platinum'),
#         ('gold', 'Gold'),
#         ('silver', 'Silver'),
#         ('free', 'Free'),
#     ))
#     calls = models.IntegerField(default=0)
#     limit = models.IntegerField(default=0)

#     def __str__(self):
#         return f"{self.user.username}: {self.scope}"


# class UserScope(models.Model):
#     user = models.ForeignKey(User, on_delete=models.CASCADE)
#     scope = models.CharField(max_length=20, choices=(
#         ('admin', 'Admin'),
#         ('platinum', 'Platinum'),
#         ('gold', 'Gold'),
#         ('silver', 'Silver'),
#         ('free', 'Free'),
#     ))

#     def __str__(self):
#         return f"{self.user.username}: {self.scope}"