"""Typed resource capability contracts. Provider implementation lists remain catalog-derived."""

from typing import Annotated

from pydantic import BaseModel, ConfigDict, Field, StrictBool, StrictFloat, StrictInt, StrictStr

Alias = Annotated[StrictStr, Field(pattern=r"^[a-z][a-z0-9_-]{0,63}$")]
Cell = StrictStr | StrictBool | StrictInt | StrictFloat
Rows = Annotated[
    list[Annotated[list[Cell], Field(min_length=1, max_length=100)]],
    Field(min_length=1, max_length=1000),
]


class ResourceInput(BaseModel):
    model_config = ConfigDict(extra="forbid", allow_inf_nan=False)
    metadata: dict | None = None


class ReadRows(ResourceInput):
    spreadsheet: Alias
    offset: Annotated[StrictInt, Field(ge=0, le=1_000_000)] = 0
    limit: Annotated[StrictInt, Field(ge=1, le=1000)] = 100


class AppendRows(ResourceInput):
    spreadsheet: Alias
    rows: Rows


class UpdateRows(AppendRows):
    offset: Annotated[StrictInt, Field(ge=0, le=1_000_000)]


class FileInput(ResourceInput):
    storage: Alias
    key: Annotated[StrictStr, Field(min_length=1, max_length=1024)]


class PutFile(FileInput):
    content_base64: Annotated[StrictStr, Field(max_length=349528)]
    content_type: Annotated[StrictStr, Field(max_length=200)] = "application/octet-stream"


class ListFiles(ResourceInput):
    storage: Alias
    prefix: Annotated[StrictStr, Field(max_length=1024)] = ""
    limit: Annotated[StrictInt, Field(ge=1, le=1000)] = 100
    cursor: Annotated[StrictStr, Field(max_length=4096)] | None = None


RESOURCE_INPUTS: dict[str, type[ResourceInput]] = {
    "spreadsheet.rows.read": ReadRows,
    "spreadsheet.rows.append": AppendRows,
    "spreadsheet.rows.update": UpdateRows,
    "storage.file.put": PutFile,
    "storage.file.get": FileInput,
    "storage.file.delete": FileInput,
    "storage.file.exists": FileInput,
    "storage.file.list": ListFiles,
}


def resource_specs() -> dict:
    return {
        verb: {
            "id": verb,
            "status": "implemented",
            "invocable": True,
            "method": f"POST /api/projects/{{project_id}}/capabilities/{verb}",
            "request_type": "integration.execute",
            "runtime": "core_resolve_cloud_execute",
            "input_schema": model.model_json_schema(),
            "output_schema": {
                "type": "object",
                "properties": {
                    "capability": {"const": verb},
                    "status": {"enum": ["queued", "rejected"]},
                    "reason_code": {"type": "string"},
                },
            },
            "codegen": (
                "Use the public host-relative BFF method with an already-bound resource alias. "
                "Execution is queued, not a provider result. Do not invent returned rows, "
                "files or resource IDs. Metadata is audit-only. Missing setup is a capability gap. "
                "Google ranges and USER_ENTERED formulas require explicit provider actions."
            ),
        }
        for verb, model in RESOURCE_INPUTS.items()
    }
