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

promo = FileSystemStorage(location='/media/promo')
events = FileSystemStorage(location='/media/events')
# 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)
    published = models.BooleanField(default=False)


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

    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 = ['-last_ran', '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 Place(models.Model):
    PLACE_TYPE = (
            ('Pc', 'Precinct'),
            ('Mu', 'Municipality'),
            ('Ci', 'City'),
            ('Co', 'County'),
            ('Ld', 'Legislative District'),
            ('St', 'State'),
        )
    name = models.CharField(max_length=31, unique=True)
    place_type = models.CharField(max_length=31,blank=True, null=True)
    num_of_divs = models.CharField(max_length=4, blank=True, null=True)
    div_type = models.CharField(max_length=7, blank=True, null=True)
    connection = models.ForeignKey("Place", on_delete=models.CASCADE)
    connection_type = models.CharField(max_length=31, choices=PLACE_TYPE, default='Ci')
    calendar = models.ForeignKey(Calendar, on_delete=models.CASCADE)
    notes = models.TextField(blank=True, null=True)

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

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


class Official(models.Model):
    POSITION_TYPE = (
            ('Gv', 'Governor'),
            ('Sc', 'Secretary'),
            ('Re', 'Representative'),
            ('Sn', 'Senator'),
            ('Sr', 'State Rep'),
            ('Ss', 'State Senator'),
            ('Cc', 'County Commissioner'),
            ('Cm', 'Council Member'),
            ('Ju', 'Judge'),
            ('Bm', 'Board Member'),
        )
        
    name = models.CharField(max_length=31, unique=True)
    boss = models.ForeignKey("Place", on_delete=models.CASCADE)
    employer = models.ForeignKey("Organization", on_delete=models.CASCADE)
    position = models.CharField(max_length=31, choices=POSITION_TYPE, default='Ci')
    notes = models.TextField(blank=True, null=True)

    website = models.CharField(max_length=31, blank=True, null=True)
    email = models.CharField(max_length=31, blank=True, null=True)
    bluesky = models.CharField(max_length=31, blank=True, null=True)
    instagram = models.CharField(max_length=31, blank=True, null=True)
    youtube = models.CharField(max_length=31, blank=True, null=True)
    upscroll = models.CharField(max_length=31, blank=True, null=True)

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

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


class Organization(models.Model):
    ORG_TYPE = (
            ('Gv', 'Government'),
            ('Fb', 'Food & Beverage'),
            ('Re', 'Retail'),
            ('Se', 'Service'),
            ('Vn', 'Venue'),
            ('Ud', 'Undefined'),
        )

    name = models.CharField(max_length=63)
    website = models.CharField(max_length=127, blank=True, null=True)
    org_type = models.CharField(max_length=31, choices=ORG_TYPE, default='Re')
    cal = models.ForeignKey(Calendar, on_delete=models.CASCADE, blank=True, null=True, related_name="org_cal")

    short_desc = models.CharField(max_length=63, blank=True, null=True)
    long_desc = models.TextField(blank=True, null=True)
    gmap_link = models.CharField(max_length=253, blank=True, null=True)
    tags = models.ManyToManyField(Tags, blank=True)

    address_complete = models.CharField(max_length=127, blank=True, null=True)
    address_numbers = models.CharField(max_length=63, blank=True, null=True)
    address_type = models.CharField(max_length=31, blank=True, null=True)
    barrio = models.CharField(max_length=127, blank=True, null=True)
    city = models.CharField(max_length=127, blank=True, null=True)
    state = models.CharField(max_length=127, blank=True, null=True)
    city_lnk = models.ForeignKey(Place, on_delete=models.CASCADE, blank=True, null=True, related_name="org_city")
    state_lnk = models.ForeignKey(Place, on_delete=models.CASCADE, blank=True, null=True, related_name="org_state" )
    zip_code = models.CharField(max_length=15, blank=True, null=True)

    phone_number = models.CharField(max_length=255, blank=True, null=True)
    business_email = models.CharField(max_length=63, blank=True, null=True)

    is_member= models.BooleanField(default=False)
    is_501c = models.BooleanField(default=False)
    is_venue= models.BooleanField(default=False)
    has_map = models.BooleanField(default=False)

    latitude = models.FloatField(blank=True, null=True)
    longitude = models.FloatField(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 = (
        ('Ce', 'Civic Engagement'),
        ('Co', '🍿'),
        ('Da', '🪇'),
        ('Ed', '🍎'),
        ('Ex' ,'💪'),
        ('Gv', '🛠️'),
        ('Mu', '🎶'),
        ('Na', '🍃'),
        ('Ot', '🤔'),
        ('Th', '🎭'),
        ('Va', 'Visual Art'),
    )
    EVENT_STATE = (
        ('live', 'Live & Direct'),
        ('virt', 'Virtually Served'),
        ('both', 'How you prefer.')
    )

    show_title = models.CharField(max_length=127, blank=True, null=True)
    show_link = models.URLField(blank=True, null=True)
    show_date = models.DateTimeField()

    calendar = models.ManyToManyField(Calendar, blank=True, related_name="events")
    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') 
    event_image = models.ImageField(upload_to="events", blank=True)
    event_image_url = models.URLField(blank=True, null=True)

    guests = models.CharField(max_length=255, blank=True, null=True)
    more_details = models.JSONField(blank=True, null=True)
    tags = models.ManyToManyField(Tags, blank=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', '🎨'),
            ('Bv', '🍻'),
            ('Fo', '🥗'),
            ('Ev', '🎟️'),
            ('Re', '🛍️'),
            ('Sv', '📌'),
            ('Ma', '🪢'),
            ('Ca', '🔍'),
            ('Jo', '📝'),
            ('Ja', '✒️'),
            ('Sp', '⚙️'),
            ('An', '🩺'),
            ('Su', '🧩'),
        )

    title = models.CharField(max_length=63)
    organization = models.ForeignKey(Organization, on_delete=models.CASCADE)
    calendar = models.ManyToManyField(Calendar, blank=True)
    promo_type = models.CharField(max_length=15, choices=PROMO_TYPE, default='Ar')
    promo_image = models.ImageField(upload_to="promo", blank=True)
    short_text = models.CharField(max_length=255,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)

    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 Person(models.Model):
#     username = models.CharField(max_length=63, blank=True, null=True)
#     email = models.CharField(max_length=63, blank=True, null=True)
#     membership = models.CharField()
#     business = models.OneToOneField()
#     calendar = models.ManyToManyField(Calendar, blank=True, null=True)
#     event_prefs = models.CharField()
#     ad_prefs = models.CharField()
#     api_key = model.CharField()


#     class Meta:
#         verbose_name_plural = "People"
#         ordering = ['username']

#     def __unicode__(self):
#         return "%s" % self.username

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


# class ProfileStat(models.Model):
#     user_profile = models.OneToOneField()
#     num_of_cals = models.SmallIntegerField()
#     num_of_ads = models.SmallIntegerField()
#     num_of_api_calls = models.SmallIntegerField()

#     class Meta:
#         verbose_name_plural = "Profile Stats"
#         ordering = ['user_profile']

#     def __unicode__(self):
#         return "%s" % self.user_profile

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


# 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}"
