from typing import Optional
from datetime import datetime

from pydantic import BaseModel, Field, field_validator

from vdash.utils.sql_client import SqlClient


class CreateUserDBRequest(BaseModel):
    name: str = Field(..., min_length=1, max_length=200)
    connection_string: str = Field(...)

    @field_validator("connection_string")
    @classmethod
    def validate_connection(cls, v: str) -> str:
        ok, err = SqlClient.validate_connection_string(v)
        if not ok:
            raise ValueError(err)
        return v


class UserDBResponse(BaseModel):
    uuid: str
    connection_string: Optional[str] = None
    name: Optional[str] = None
    created_at: datetime
    updated_at: datetime


class UpdateUserDBRequest(BaseModel):
    name: str = Field(..., min_length=1, max_length=200)
