# -*- coding: utf-8 -*-
from django.db import models
from django.core.exceptions import ValidationError
from django.utils.translation import ugettext_lazy as _

from django.contrib.auth.models import User
from django.contrib.sites.models import Site
from django.contrib.localflavor.us.us_states import STATE_CHOICES
from django.contrib.localflavor.us.models import PhoneNumberField

from imagekit.models import ImageModel


class ClientLogo(ImageModel):
    original_image = models.ImageField(upload_to='texting/logos')


class Category(models.Model):
    name = models.CharField(max_length=50, help_text=_('Max 50 chars. Required.'))
    
    class Meta:
        verbose_name_plural = 'categories'
      
    def __unicode__(self):
        return self.name


class Client(models.Model):
    name = models.CharField(max_length=50, help_text=_('Max 50 chars. Required.'))
    description = models.CharField(max_length=5000, help_text=_('Required.'))
    logo = models.ForeignKey(ClientLogo, null=True, blank=True)
    category = models.ForeignKey(Category, null=True, blank=True)
    featured = models.BooleanField(default=False)
    
    address = models.CharField(max_length=50, blank=True, help_text=_('Max 200 chars. Required.'))
    city = models.CharField(max_length=50, blank=True, help_text=_('Max 100 chars. Required.'))
    state = models.CharField(max_length=2, blank=True, choices=STATE_CHOICES, help_text=_('Not required.'))
    zip_code = models.CharField(max_length=5, blank=True, help_text=_('Not required.'))    
    phone = PhoneNumberField(blank=True, help_text=_('Not required.'))
    url = models.URLField(blank=True, help_text=_('Not required.'))
    
    def __unicode__(self):
        return self.name
    
    def get_absolute_url(self):
        return reverse('texting_client_detail', kwargs={'object_id': self.id})


class PromotionBase(models.Model):
    name = models.CharField(max_length=300)
    code = models.CharField(max_length=10)

    sites = models.ManyToManyField(Site)
    startdate = models.DateField()
    enddate = models.DateField()
    featured = models.BooleanField(default=False)

    class Meta:
        abstract = True

    def __unicode__(self):
        return self.name
    
    def clean (self):
        if self.startdate and self.enddate and self.startdate >= self.enddate:
            raise ValidationError(u'Start date must be before end date.')


class Offer(PromotionBase):
    client = models.ForeignKey(Client)
    texts_per_week = models.IntegerField()
    vibes_id = models.CharField(max_length=10)


class Contest(PromotionBase):
    client = models.ForeignKey(Client, null=True, blank=True)
    logo = models.ForeignKey(ClientLogo, null=True, blank=True)
