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

# User Schemas
class UserBase(BaseModel):
    email: EmailStr
    username: str
    full_name: Optional[str] = None

class UserCreate(UserBase):
    password: str = Field(..., min_length=8)

class UserLogin(BaseModel):
    username: str
    password: str

class UserResponse(UserBase):
    id: int
    is_active: bool
    is_verified: bool
    created_at: datetime
    
    class Config:
        from_attributes = True

class UserProfile(BaseModel):
    id: int
    email: str
    username: str
    full_name: Optional[str]
    is_active: bool
    created_at: datetime
    api_keys_count: int
    emails_sent: int
    
    class Config:
        from_attributes = True

# Token Schemas
class Token(BaseModel):
    access_token: str
    token_type: str = "bearer"
    expires_in: int

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

# API Key Schemas
class ApiKeyCreate(BaseModel):
    name: str = Field(..., min_length=1, max_length=100)

class ApiKeyResponse(BaseModel):
    id: int
    key_name: str
    api_key: str
    is_active: bool
    created_at: datetime
    last_used_at: Optional[datetime]
    usage_count: int
    
    class Config:
        from_attributes = True

class ApiKeyInfo(BaseModel):
    id: int
    key_name: str
    is_active: bool
    created_at: datetime
    last_used_at: Optional[datetime]
    usage_count: int
    
    class Config:
        from_attributes = True

# SMTP Config Schemas
class SMTPConfigCreate(BaseModel):
    name: str
    host: str
    port: int = Field(..., ge=1, le=65535)
    username: str
    password: str
    use_tls: bool = True
    is_default: bool = False

class SMTPConfigUpdate(BaseModel):
    name: Optional[str] = None
    host: Optional[str] = None
    port: Optional[int] = Field(None, ge=1, le=65535)
    username: Optional[str] = None
    password: Optional[str] = None
    use_tls: Optional[bool] = None
    is_default: Optional[bool] = None

class SMTPConfigResponse(BaseModel):
    id: int
    name: str
    host: str
    port: int
    username: str
    use_tls: bool
    is_default: bool
    is_active: bool
    created_at: datetime
    
    class Config:
        from_attributes = True

# Email Schemas
class EmailSend(BaseModel):
    to_email: EmailStr
    subject: str = Field(..., min_length=1)
    body: str = Field(..., min_length=1)
    from_name: Optional[str] = None
    is_html: bool = False

class EmailSendWithApiKey(BaseModel):
    api_key: str
    to_email: EmailStr
    subject: str = Field(..., min_length=1)
    body: str = Field(..., min_length=1)
    from_email: Optional[EmailStr] = None
    from_name: Optional[str] = None
    is_html: bool = False

class EmailResponse(BaseModel):
    success: bool
    message: str
    message_id: Optional[str] = None

class EmailLogResponse(BaseModel):
    id: int
    from_email: str
    to_email: str
    subject: str
    status: str
    sent_at: datetime
    error_message: Optional[str]
    
    class Config:
        from_attributes = True

# Dashboard Stats
class DashboardStats(BaseModel):
    total_emails_sent: int
    emails_sent_today: int
    active_api_keys: int
    smtp_configs: int
    recent_emails: List[EmailLogResponse]
    api_usage: List[dict]

# Error Response
class ErrorResponse(BaseModel):
    detail: str
    error_code: Optional[str] = None
