from typing import Optional, List
from pydantic import BaseModel, HttpUrl
from enum import Enum
from datetime import datetime

class ProtocolType(str, Enum):
    OIDC = "OIDC"
    SIMPLE_API = "SIMPLE_API"

class ApplicationBase(BaseModel):
    app_name: str
    icon_url: Optional[str] = None
    protocol_type: ProtocolType = ProtocolType.SIMPLE_API
    redirect_uris: Optional[str] = None # Stored as JSON string or comma-separated for simplicity in MVP, or validate as list
    notification_url: Optional[str] = None

class ApplicationCreate(ApplicationBase):
    pass

class ApplicationUpdate(BaseModel):
    app_name: Optional[str] = None
    icon_url: Optional[str] = None
    protocol_type: Optional[ProtocolType] = None
    redirect_uris: Optional[str] = None
    notification_url: Optional[str] = None
    password: Optional[str] = None
    verification_code: Optional[str] = None

class ApplicationTransferRequest(BaseModel):
    target_mobile: str
    password: str
    verification_code: str

class RegenerateSecretRequest(BaseModel):
    password: str
    verification_code: str

class ApplicationResponse(ApplicationBase):
    id: int
    app_id: str
    owner_id: int
    # We do NOT return app_secret_hash
    created_at: datetime
    updated_at: datetime

    class Config:
        from_attributes = True

class ApplicationList(BaseModel):
    total: int
    items: List[ApplicationResponse]

class ApplicationSecretDisplay(BaseModel):
    app_id: str
    app_secret: str
    access_token: Optional[str] = None

class ViewSecretRequest(BaseModel):
    password: str

class AppSyncRequest(BaseModel):
    mode: str = "ALL"  # "ALL" or "SELECTED"
    user_ids: Optional[List[int]] = []
    init_email: bool = False
    email_domain: Optional[str] = None
    verification_code: str