import uuid
from typing import Literal

from pydantic import BaseModel, ConfigDict, field_validator


class MerchantCreateRequest(BaseModel):
    """name/description/logo_url for the merchant brand itself.
    owner_id is only meaningful when an admin creates this on behalf of
    a supplier — a supplier caller always ends up owning it themselves
    regardless of what (if anything) they pass here; see
    app.api.v1.endpoints.merchants.create_merchant.

    business_type defaults to "restaurant" — pass "grocery" to create a
    grocery store instead. Everything downstream (branches, catalog,
    cart, checkout) works identically either way; see the Merchant
    model docstring."""

    name: str
    description: str | None = None
    logo_url: str | None = None
    owner_id: uuid.UUID | None = None
    business_type: Literal["restaurant", "grocery"] = "restaurant"

    @field_validator("name")
    @classmethod
    def _not_blank(cls, v: str) -> str:
        v = v.strip()
        if not v:
            raise ValueError("Name is required.")
        return v


class MerchantUpdateRequest(BaseModel):
    """Partial — only fields actually sent are changed. owner_id is
    deliberately absent: transferring ownership isn't supported here,
    same as it never was on create for a supplier caller."""

    name: str | None = None
    description: str | None = None
    logo_url: str | None = None
    business_type: Literal["restaurant", "grocery"] | None = None

    @field_validator("name")
    @classmethod
    def _not_blank(cls, v: str | None) -> str | None:
        if v is not None:
            v = v.strip()
            if not v:
                raise ValueError("Name is required.")
        return v


class MerchantOut(BaseModel):
    model_config = ConfigDict(from_attributes=True)

    id: uuid.UUID
    owner_id: uuid.UUID
    name: str
    description: str | None
    logo_url: str | None
    business_type: str
