from sqlite3.dbapi2 import Timestamp, adapt
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 User(SQLModel, table=True):
    id: Optional[int] = Field(default=None, primary_key=True)
    short_name: 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

    @validator("short_name", always=True)
    def valid_short_name(cls, sn_input):
        assert sn_input.isalpha(), "only alphabet letters allowed in short name"
        assert sn_input.islower(), "short name contains small letters only"
        return sn_input

    @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
[((287, 324), 'sqlmodel.Field', 'Field', ([], {'default': 'None', 'primary_key': '(True)'}), '(default=None, primary_key=True)\n', (292, 324), False, 'from sqlmodel import Field, SQLModel, Field\n'), ((547, 583), 'pydantic.validator', 'validator', (['"""short_name"""'], {'always': '(True)'}), "('short_name', always=True)\n", (556, 583), False, 'from pydantic import validator\n'), ((812, 848), 'pydantic.validator', 'validator', (['"""first_name"""'], {'always': '(True)'}), "('first_name', always=True)\n", (821, 848), False, 'from pydantic import validator\n'), ((1226, 1261), 'pydantic.validator', 'validator', (['"""last_name"""'], {'always': '(True)'}), "('last_name', always=True)\n", (1235, 1261), False, 'from pydantic import validator\n'), ((1451, 1482), 'pydantic.validator', 'validator', (['"""email"""'], {'always': '(True)'}), "('email', always=True)\n", (1460, 1482), False, 'from pydantic import validator\n'), ((1608, 1640), 're.fullmatch', 're.fullmatch', (['regex', 'email_input'], {}), '(regex, email_input)\n', (1620, 1640), False, 'import re\n'), ((1077, 1168), '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", (1090, 1168), False, 'from fastapi import HTTPException\n')]