from pydantic import BaseModel, EmailStr, Field, field_validator

from app.core.password_policy import validate_password_strength


class RegisterIn(BaseModel):
    email: EmailStr
    password: str = Field(min_length=8)

    @field_validator("email", "password", mode="before")
    @classmethod
    def strip_whitespace(cls, value: str) -> str:
        if isinstance(value, str):
            return value.strip()
        return value

    @field_validator("password")
    @classmethod
    def password_policy(cls, value: str) -> str:
        return validate_password_strength(value)


class LoginIn(BaseModel):
    email: EmailStr
    password: str = Field(min_length=8)

    @field_validator("email", "password", mode="before")
    @classmethod
    def strip_whitespace(cls, value: str) -> str:
        if isinstance(value, str):
            return value.strip()
        return value


class VerifyEmailIn(BaseModel):
    token: str


class ForgotPasswordIn(BaseModel):
    email: EmailStr


class ResendVerificationIn(BaseModel):
    email: EmailStr


class ResetPasswordIn(BaseModel):
    token: str
    new_password: str = Field(min_length=8)

    @field_validator("new_password")
    @classmethod
    def password_policy(cls, value: str) -> str:
        return validate_password_strength(value)


class AuthUserOut(BaseModel):
    id: str
    email: EmailStr
    role: str
    is_superuser: bool
    status: str


class LoginOut(BaseModel):
    access_token: str
    token_type: str = "bearer"
    expires_in: int = 900
    user: AuthUserOut
