"""Customer Google Sheets values API; provider ranges stay behind resource bindings."""

from src.domain.connectors.catalog._base import ConnectorActionDef, ConnectorDef, ConnectorFieldDef

SCOPE = "https://www.googleapis.com/auth/spreadsheets"


def _action(operation: str) -> ConnectorActionDef:
    writing = operation != "read"
    return ConnectorActionDef(
        key=f"{operation}_values",
        slug=f"google_sheets.{operation}_values",
        name=f"{operation.title()} values",
        description="Google values API with explicit A1 range.",
        required_scopes=(SCOPE,),
        required_capabilities=("integrations.execute",),
        implements=(f"spreadsheet.rows.{operation}",),
        input_schema={
            "type": "object",
            "additionalProperties": False,
            "required": ["range"] + (["values"] if writing else []),
            "properties": {
                "spreadsheet_id": {"type": "string"},
                "range": {"type": "string", "maxLength": 300},
                **(
                    {
                        "values": {"type": "array"},
                        "value_input_option": {"enum": ["RAW", "USER_ENTERED"]},
                    }
                    if writing
                    else {}
                ),
            },
        },
    )


CONNECTOR = ConnectorDef(
    key="google_sheets",
    name="Google Sheets",
    category="documents",
    auth_type="oauth",
    version="01.00.00",
    status="preview",
    description="Customer-bound Google Sheets. Platform sign-in never supplies its credentials.",
    docs_url="https://developers.google.com/workspace/sheets/api/reference/rest",
    config_fields=("spreadsheet_id", "spreadsheet_map"),
    secret_fields=("access_token", "refresh_token", "client_id", "client_secret"),
    config_field_defs=(
        ConnectorFieldDef(key="spreadsheet_id", label="Default spreadsheet ID"),
        ConnectorFieldDef(
            key="spreadsheet_map",
            label="Spreadsheet bindings",
            input_type="json",
            description="JSON aliases to spreadsheet_id, sheet_name, column_count, start_row.",
        ),
    ),
    secret_field_defs=tuple(
        ConnectorFieldDef(
            key=key, label=key.replace("_", " ").title(), required=True, input_type="password"
        )
        for key in ("access_token", "refresh_token", "client_id", "client_secret")
    ),
    actions=tuple(_action(op) for op in ("read", "append", "update")),
)
