import uuid
from datetime import datetime
from typing import Literal

from pydantic import BaseModel, ConfigDict, Field

WorkspaceStatusLiteral = Literal["provisioning", "active", "failed"]


class WorkspaceCreate(BaseModel):
    name: str = Field(min_length=1, max_length=200)
    db_host: str = Field(min_length=1, max_length=255)
    db_port: int = Field(default=5432, ge=1, le=65535)
    db_name: str = Field(min_length=1, max_length=63)
    db_username: str = Field(min_length=1, max_length=63)
    # Write-only — never echoed back (see WorkspaceRead, which has no
    # password field at all). Stored encrypted, not in plaintext.
    db_password: str = Field(min_length=1)
    # When true, every row belonging to the caller's own tenant is
    # duplicated into the new database after the schema is applied (see
    # app/core/db_provisioning.py::copy_tenant_data) — file attachments'
    # underlying bytes on disk are not part of this (metadata only).
    copy_tenant_data: bool = False


class WorkspaceRead(BaseModel):
    model_config = ConfigDict(from_attributes=True)
    id: uuid.UUID
    tenant_id: uuid.UUID
    name: str
    db_host: str
    db_port: int
    db_name: str
    db_username: str
    status: WorkspaceStatusLiteral
    status_detail: str | None
    created_by: uuid.UUID | None
    created_at: datetime
    updated_at: datetime


class WorkspaceTestResult(BaseModel):
    success: bool
    detail: str
