# -*- coding: utf-8 -*-

from sqlalchemy import (
    Column,
    DateTime,
    ForeignKey,
    Integer,
    String,
    func,
    )
from sqlalchemy.orm import relationship

from .support import db

class Person(db.Model):
    """
    A person, with a bit more info than just the 'person' entity. Multiple 'person' entities
    can link to a single person.
    """
    __tablename__ = "people"

    id          = Column(Integer, primary_key=True)
    name        = Column(String(50), index=True, nullable=False, unique=True)
    gender_id   = Column(Integer, ForeignKey('genders.id'))
    race_id     = Column(Integer, ForeignKey('races.id'))

    created_at   = Column(DateTime(timezone=True), index=True, unique=False, nullable=False, server_default=func.now())
    updated_at   = Column(DateTime(timezone=True), server_default=func.now(), onupdate=func.current_timestamp())

    # Associations
    gender      = relationship("Gender", lazy=False)
    race        = relationship("Race", lazy=False)

    def __repr__(self):
        return "<Person id=%s, name=\"%s\">" % (self.id, self.name.encode('utf-8'))


class Gender(db.Model):
    __tablename__ = "genders"

    id        = Column(Integer, primary_key=True)
    name      = Column(String(150), index=True, nullable=False, unique=True)

    def __repr__(self):
        return "<Gender name='%s'>" % (self.name)

    def abbr(self):
        return self.name[0].upper()

    @classmethod
    def male(cls):
        return Gender.query.filter(Gender.name == 'Male').one()

    @classmethod
    def female(cls):
        return Gender.query.filter(Gender.name == 'Female').one()

    @classmethod
    def create_defaults(cls):
        genders = []
        for s in cls.SYMBOLS.keys():
            g = Gender()
            g.name = s.strip()
            genders.append(g)

        return genders


class Race(db.Model):
    __tablename__ = "races"

    id        = Column(Integer, primary_key=True)
    name      = Column(String(50), index=True, nullable=False, unique=True)

    def __repr__(self):
        return "<Race name='%s'>" % (self.name)

    def abbr(self):
        return self.name[0].upper()

    @classmethod
    def create_defaults(self):
        text = """
        Black
        White
        Coloured
        Asian
        Indian
        Other
        """

        races = []
        for s in text.strip().split("\n"):
            g = Race()
            g.name = s.strip()
            races.append(g)

        return races

@@ -1669,24 +1669,123 @@
 aults(cls):%0A
+        text = %22%22%22%0A        Female%0A        Male%0A        Other: Transgender, Transsexual%0A        %22%22%22%0A
         gend
@@ -1814,25 +1814,31 @@
  in 
-cls.SYMBOLS.keys(
+text.strip().split(%22%5Cn%22
 ):%0A 

Fix gender seeds.