from typing import Optional
from sqlmodel import Field, SQLModel, Field
from pydantic import validator
from datetime import datetime, date
from fastapi import HTTPException
import re


class AppUser(SQLModel, table=True):
    """Create an SQLModel for users"""

    id: Optional[int] = Field(default=None, primary_key=True)
    username: str
    first_name: str
    last_name: str
    email: str
    role_id: int
    team_id: Optional[int] = None
    start_date: date
    created_at: datetime
    updated_at: datetime
    is_active: bool

    __table_args__ = {"schema": "app_db"}

    @validator("first_name", always=True)
    def valid_first_name(cls, first_name):
        assert first_name.replace(
            " ", ""
        ).isalpha(), "only alphabet letters allowed in first name"
        if first_name[0].isupper() == False:
            raise HTTPException(
                status_code=400, detail="first name should start with a capital letter"
            )
        return first_name

    @validator("last_name", always=True)
    def valid_last_name(cls, ln_input):
        assert ln_input.replace(
            " ", ""
        ).isalpha(), "only alphabet letters allowed in last name"
        return ln_input

    @validator("email", always=True)
    def valid_email(cls, email_input):
        regex = r"\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b"
        assert re.fullmatch(regex, email_input), "email format incorrect"
        return email_input

pydantic.validator fastapi.HTTPException sqlmodel.Field re.fullmatch
[((286, 323), 'sqlmodel.Field', 'Field', ([], {'default': 'None', 'primary_key': '(True)'}), '(default=None, primary_key=True)\n', (291, 323), False, 'from sqlmodel import Field, SQLModel, Field\n'), ((587, 623), 'pydantic.validator', 'validator', (['"""first_name"""'], {'always': '(True)'}), "('first_name', always=True)\n", (596, 623), False, 'from pydantic import validator\n'), ((1001, 1036), 'pydantic.validator', 'validator', (['"""last_name"""'], {'always': '(True)'}), "('last_name', always=True)\n", (1010, 1036), False, 'from pydantic import validator\n'), ((1226, 1257), 'pydantic.validator', 'validator', (['"""email"""'], {'always': '(True)'}), "('email', always=True)\n", (1235, 1257), False, 'from pydantic import validator\n'), ((1383, 1415), 're.fullmatch', 're.fullmatch', (['regex', 'email_input'], {}), '(regex, email_input)\n', (1395, 1415), False, 'import re\n'), ((852, 943), 'fastapi.HTTPException', 'HTTPException', ([], {'status_code': '(400)', 'detail': '"""first name should start with a capital letter"""'}), "(status_code=400, detail=\n    'first name should start with a capital letter')\n", (865, 943), False, 'from fastapi import HTTPException\n')]