from peewee import CharField, UUIDField, SmallIntegerField, TextField, ForeignKeyField, JOIN

from lib.domain.Town import Town
from lib.domain.BaseModel import BaseModel
from lib.domain.ZipCode import ZipCode


class Applicant(BaseModel):
    class Meta:
        table_name = "applicants"

    id = UUIDField(primary_key=True)
    title = SmallIntegerField(default=0)
    first_name = CharField(null=False)
    last_name = CharField(null=False)
    street = CharField()
    house_number = CharField()
    zip_code = ForeignKeyField(ZipCode, column_name="zip_code", null=True)
    phone_number = CharField()
    mobile_number = CharField()
    email_address = CharField()
    salutation = TextField(null=False)

    @classmethod
    def select_table_data(cls):
        return (
            Applicant
            .select(Applicant.id, Applicant.first_name, Applicant.last_name, ZipCode.id, ZipCode.zip_code, Town.town)
            .join(ZipCode, join_type="LEFT JOIN")
            .join(Town, join_type="LEFT JOIN")
        )
