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


class DataSourceBase(BaseModel):
    name: str = Field(..., max_length=100)
    source_type: str = Field(..., max_length=50)
    host: Optional[str] = Field(None, max_length=200)
    port: Optional[int] = None
    database_name: Optional[str] = Field(None, max_length=100)
    username: Optional[str] = Field(None, max_length=100)
    extra_params: Optional[str] = None
    status: Optional[str] = "active"
    dept_id: Optional[int] = None


class DataSourceCreate(DataSourceBase):
    password: Optional[str] = None


class DataSourceUpdate(BaseModel):
    name: Optional[str] = Field(None, max_length=100)
    source_type: Optional[str] = Field(None, max_length=50)
    host: Optional[str] = Field(None, max_length=200)
    port: Optional[int] = None
    database_name: Optional[str] = Field(None, max_length=100)
    username: Optional[str] = Field(None, max_length=100)
    password: Optional[str] = None
    extra_params: Optional[str] = None
    dept_id: Optional[int] = None


class DataSourceOut(DataSourceBase):
    id: int
    created_by: Optional[int] = None
    created_at: datetime
    updated_at: datetime

    class Config:
        from_attributes = True


class DataSourceTest(BaseModel):
    source_type: str
    host: Optional[str] = None
    port: Optional[int] = None
    database_name: Optional[str] = None
    username: Optional[str] = None
    password: Optional[str] = None
    extra_params: Optional[str] = None
