from __future__ import annotations

from pydantic import BaseModel, EmailStr, Field, field_validator


class BootstrapCompanyRead(BaseModel):
    name: str
    code: str
    admin_email: str


class BootstrapConnectionRead(BaseModel):
    host: str
    port: int


class BootstrapDatabaseRead(BaseModel):
    driver: str
    host: str
    port: int
    name: str
    username: str
    password_configured: bool


class BootstrapCacheRead(BaseModel):
    enabled: bool
    url: str


class BootstrapStateRead(BaseModel):
    initialized: bool
    company: BootstrapCompanyRead
    web: BootstrapConnectionRead
    server: BootstrapConnectionRead
    database: BootstrapDatabaseRead
    redis: BootstrapCacheRead


class BootstrapSetupPayload(BaseModel):
    company_name: str = Field(min_length=2, max_length=80)
    company_code: str = Field(default="", max_length=32)
    admin_email: EmailStr | None = None

    postgres_host: str = Field(min_length=1, max_length=255)
    postgres_port: int = Field(default=5432, ge=1, le=65535)
    postgres_db: str = Field(min_length=1, max_length=128)
    postgres_user: str = Field(min_length=1, max_length=128)
    postgres_password: str = Field(min_length=1, max_length=255)

    redis_url: str | None = Field(default=None, max_length=255)

    @field_validator(
        "company_name",
        "company_code",
        "postgres_host",
        "postgres_db",
        "postgres_user",
        "postgres_password",
        "redis_url",
        mode="before",
    )
    @classmethod
    def strip_string(cls, value: str | None) -> str | None:
        if value is None:
            return None
        return value.strip()
