from datetime import datetime
from pydantic import BaseModel, Field


class TargetBase(BaseModel):
    name: str
    host: str
    port: int = 5432
    dbname: str
    username: str
    sslmode: str = "prefer"
    use_pg_stat_statements: bool = True
    owner_user_ids: list[int] = Field(default_factory=list)
    tags: dict = Field(default_factory=dict)


class TargetCreate(TargetBase):
    password: str
    discover_all_databases: bool = False


class TargetConnectionTestRequest(BaseModel):
    host: str
    port: int = 5432
    dbname: str
    username: str
    password: str
    sslmode: str = "prefer"


class TargetUpdate(BaseModel):
    name: str | None = None
    host: str | None = None
    port: int | None = None
    dbname: str | None = None
    username: str | None = None
    password: str | None = None
    sslmode: str | None = None
    use_pg_stat_statements: bool | None = None
    owner_user_ids: list[int] | None = None
    tags: dict | None = None


class TargetOut(TargetBase):
    id: int
    created_at: datetime

    model_config = {"from_attributes": True}


class TargetOwnerOut(BaseModel):
    user_id: int
    email: str
    role: str


class TargetOwnersUpdate(BaseModel):
    user_ids: list[int] = Field(default_factory=list)
