# -*- coding: utf-8 -*-
from __future__ import unicode_literals

import datetime

from django.db import models


class Offer(models.Model):
    """Description of an offer available to subscribers.

    Implementation notes:
    configuration_type store the model name of the configuration backend
    (ex VPNConfiguration).
    The choices list is dynamically generated at start in the __init__
    """

    def __init__(self, *args, **kwargs):
        from coin.configuration.models import Configuration
        super(Offer, self).__init__(*args, **kwargs)
        """
        Génère automatiquement la liste de choix possibles de types
        de configurations en fonction des classes enfants de Configuration
        """
        self._meta.get_field_by_name('configuration_type')[0]._choices = (
            Configuration.get_configurations_choices_list())

    name = models.CharField(max_length=255, blank=False, null=False,
                            verbose_name="nom de l'offre")
    configuration_type = models.CharField(max_length=50,
                            null=True,
                            choices = (('',''),),
                            verbose_name='type de configuration',
                            help_text="Type de configuration à utiliser avec cette offre")
    billing_period = models.IntegerField(blank=False, null=False, default=1,
                                         verbose_name='période de facturation',
                                         help_text='en mois')
    period_fees = models.DecimalField(max_digits=5, decimal_places=2,
                                      blank=False, null=False,
                                      verbose_name='montant par période de '
                                                   'facturation',
                                      help_text='en €')
    initial_fees = models.DecimalField(max_digits=5, decimal_places=2,
                                      blank=False, null=False,
                                      verbose_name='frais de mise en service',
                                      help_text='en €')

    def get_configuration_type_display(self):
        """
        Renvoi le nom affichable du type de configuration
        """
        for item in Configuration.get_configurations_choices_list():
            if item and self.configuration_type in item:
                return item[1]
        return self.configuration_type

    def __unicode__(self):
        return '%s : %s - %d€ / %im' % (
            self.get_configuration_type_display(),
            self.name,
            self.period_fees,
            self.billing_period)

    class Meta:
        verbose_name = 'offre'


class OfferSubscription(models.Model):
    """Only contains administrative details about a subscription, not
    technical.  Nothing here should end up into the LDAP backend.

    Implementation notes: the Configuration model (which actually implementing the backend
    (technical configuration for the technology)) relate to this class
    with a OneToOneField
    """
    subscription_date = models.DateField(
        null=False,
        blank=False,
        default=datetime.date.today,
        verbose_name="date de souscription à l'offre")
    # TODO: for data retention, prevent deletion of a subscription object
    # while the resign date is recent enough (e.g. one year in France).
    resign_date = models.DateField(
        null=True,
        blank=True,
        verbose_name='date de résiliation')
    # TODO: move this to offers?
    commitment = models.IntegerField(blank=False, null=False,
                                     verbose_name="période d'engagement",
                                     help_text = 'en mois',
                                     default=0)
    member = models.ForeignKey('members.Member', verbose_name='membre')
    offer = models.ForeignKey('Offer', verbose_name='offre')

    def __unicode__(self):
        return '%s - %s - %s' % (self.member, self.offer.name,
                                       self.subscription_date)

    class Meta:
        verbose_name = 'abonnement'

