from pydantic import BaseModel, EmailStr
from typing import Optional
from datetime import datetime


class UserCreate(BaseModel):
    email: EmailStr
    password: str
    name: Optional[str] = None
    phone: Optional[str] = None
    country: str = "Mongolia"
    referred_by: Optional[str] = None  # Referral code of the user who referred


class UserUpdate(BaseModel):
    """Schema for updating user profile"""
    name: Optional[str] = None
    phone: Optional[str] = None
    country: Optional[str] = None


class UserResponse(BaseModel):
    id: int
    email: str
    name: Optional[str] = None
    phone: Optional[str] = None
    country: str
    is_active: bool
    is_admin: bool = False
    is_dealer: bool = False
    cc_balance: float = 0.0  # Float to support fractional CC (e.g., 0.1 CC)
    referral_code: Optional[str] = None  # User's unique referral code
    email_verified: bool = False
    phone_verified: bool = False
    created_at: datetime

    class Config:
        from_attributes = True


class CarViewResponse(BaseModel):
    id: int
    user_id: int
    car_id: int
    cc_paid: int
    created_at: datetime

    class Config:
        from_attributes = True


class PurchaseViewRequest(BaseModel):
    car_id: int


class Token(BaseModel):
    access_token: str
    token_type: str = "bearer"


class TokenData(BaseModel):
    email: Optional[str] = None
