from django.db import models
from django.utils.translation import ugettext_lazy as _
from core.models import AddressBase, PersonBase, ContactDataBase, WinoseBaseWithTrackingModel
from django.db import transaction


# client category
class ClientCategory(WinoseBaseWithTrackingModel):
    class Meta:
        # automatic plural won't work
        verbose_name = _('client category')
        verbose_name_plural = _('client categories')
        ordering = ['name']

    name = models.CharField(
        max_length=63,
        blank=False,
        verbose_name=_('industry sector')
    )

    overview_url = 'client categories_list'
    create_url = 'client category_create'
    update_url = 'client category_update'
    detail_url = 'client category_detail'

    def __str__(self):
        return self.name


# a client is the roof top organization, mostly in
# the sense of a company, a company is basically
# an address (for e.g. the headquarter) and some
# additional information
class Client(AddressBase, WinoseBaseWithTrackingModel):
    class Meta:
        verbose_name = _('client')
        verbose_name_plural = _('clients')
        ordering = ['name']  # default ordering

    overview_url = 'clients_list'
    detail_url = 'client_detail'
    update_url = 'client_update'
    create_url = 'client_create'

    # company name
    name = models.CharField(
        max_length=127,
        verbose_name=_('name'),
        unique=True
    )

    # some custom/internal reference to the reference for that customer/client
    # in a different system, e.g. some ERP system
    reference = models.CharField(
        max_length=127,
        verbose_name=_('internal reference'),
        blank=True
    )

    def toggle_is_active(self, state=None):
        super(Client, self).toggle_is_active()
        # toggling cascades towards the clients
        for off in self.offices.all():
            off.toggle_is_active(self.is_active)

    def __str__(self):
        return self.name

    category = models.ForeignKey(to=ClientCategory, related_name='clients', verbose_name=_('category'))


# a contact person on office level
class ContactPerson(PersonBase, ContactDataBase, WinoseBaseWithTrackingModel):
    class Meta:
        verbose_name = _('contact person')
        verbose_name_plural = _('contact persons')

    overview_url = 'contact persons_list'
    create_url = 'contact person_create'
    update_url = 'contact person_update'
    detail_url = 'contact person_detail'

    # some official title or job position description
    title = models.CharField(
        max_length=63,
        verbose_name=_('position'),
        blank=True
    )

    def __str__(self):
        return '{0} {1}'.format(self.first_name, self.last_name)


# a sub division for a company or client
# it may have some general contact information in addition to an
# contact persons (there might be no such person)
class ClientOffice(AddressBase, ContactDataBase, WinoseBaseWithTrackingModel):
    class Meta:
        verbose_name_plural = _('offices')
        verbose_name = _('office')

    overview_url = 'client offices_list'
    create_url = 'client office_create'
    update_url = 'client office_update'
    detail_url = 'client office_detail'

    department = models.CharField(
        max_length=63,
        verbose_name=_('department'),
        blank=True
    )

    # again optional reference to a user specific reference
    reference = models.CharField(
        max_length=127,
        verbose_name=_('internal reference'),
        blank=True
    )

    # a client may have several structural subdivisions
    # as facilities or offices.
    client = models.ForeignKey(to=Client, related_name='offices', verbose_name=_('client'))

    # each office may have an optional contact person
    contact = models.ForeignKey(to=ContactPerson, null=True, related_name='offices', blank=True,
                                verbose_name=_('contact person'))

    def toggle_is_active(self, state=None):
        super(ClientOffice, self).toggle_is_active(state)
        # propagate towards projects (which in turn leads to the sample, which leads to the bags... :)
        for proj in self.projects.all():
            proj.toggle_is_active(self.is_active)

    def __str__(self):
        return '{0} {1}'.format(self.client, self.city)





