# Schemas prontos pra colar no server.py
# ========================================
# Este arquivo contém apenas os blocos de código que devem ser
# substituídos ou adicionados no server.py do projeto.
# Aplicar via Claude Code seguindo o MANUAL_APLICACAO.md.


# ============================================================
# SUBSTITUIR a tool totvs_update_product_price existente:
# ============================================================

types.Tool(
    name="totvs_update_product_price",
    description=(
        "⚠️ ESCRITA — Altera preço OU custo de produto. "
        "DEPRECATED em v3.1: prefira totvs_update_product_price_only ou "
        "totvs_update_product_cost. Esta tool ainda funciona — sem valueType "
        "assume Price; passe valueType='Cost' (ou 'C') pra custo."
    ),
    inputSchema={
        "type": "object",
        "properties": {
            "productCode": {"type": "integer"},
            "branchCode": {"type": "integer", "description": "Padrão do .env se omitido"},
            "valueType": {
                "type": "string",
                "enum": ["Price", "Cost", "P", "C"],
                "description": "Price/P = preço de venda; Cost/C = custo. Default: Price."
            },
            "valueCode": {"type": "integer", "description": "Código da tabela (1, 2, 3...)"},
            "value": {"type": "number"}
        },
        "required": ["productCode", "valueCode", "value"]
    }
),


# ============================================================
# ADICIONAR depois de totvs_update_product_price:
# ============================================================

types.Tool(
    name="totvs_update_product_price_only",
    description=(
        "⚠️ ESCRITA — Atualiza PREÇO DE VENDA do produto. "
        "valueType='Price' é injetado automaticamente. "
        "Faz upsert: tenta UPDATE; se não existe, faz CREATE. "
        "Modo simples: passe productCode + valueCode + value. "
        "Modo lote: products[]={productCode, values:[{branchCode, valueCode, value}]}. "
        "Para custo use totvs_update_product_cost."
    ),
    inputSchema={
        "type": "object",
        "properties": {
            "productCode": {"type": "integer"},
            "branchCode": {"type": "integer", "description": "Padrão do .env se omitido"},
            "valueCode": {"type": "integer", "description": "Tabela de preço (consulte priceTypes em totvs_get_context)"},
            "value": {"type": "number"},
            "products": {
                "type": "array",
                "description": "Modo lote (alternativo)",
                "items": {"type": "object"}
            }
        }
    }
),

types.Tool(
    name="totvs_update_product_cost",
    description=(
        "⚠️ ESCRITA — Atualiza CUSTO do produto. "
        "valueType='Cost' é injetado automaticamente. "
        "Faz upsert: tenta UPDATE; se não existe, faz CREATE. "
        "Para preço use totvs_update_product_price_only."
    ),
    inputSchema={
        "type": "object",
        "properties": {
            "productCode": {"type": "integer"},
            "branchCode": {"type": "integer", "description": "Padrão do .env se omitido"},
            "valueCode": {"type": "integer"},
            "value": {"type": "number"},
            "products": {"type": "array", "items": {"type": "object"}}
        }
    }
),


# ============================================================
# SUBSTITUIR a tool totvs_update_product_data existente:
# ============================================================

types.Tool(
    name="totvs_update_product_data",
    description=(
        "⚠️ ESCRITA — Atualiza dados gerais de produto (peso, NCM, CST, etc). "
        "Auto-roteamento: "
        "se passar productCode (1 produto) → PUT /products/{code}/{branchCode} (simples); "
        "se passar productCodeList ou groupCode → PUT /data (batch com filtros). "
        "Campos: weight, ncmCode, cstCode, cestCode, prefixEanGtin, measuredUnit, "
        "isInactive, isFinishedProduct, isRawMaterial, isBulkMaterial, isOwnProduction."
    ),
    inputSchema={
        "type": "object",
        "properties": {
            "productCode": {"type": "integer", "description": "Modo simples (1 produto)"},
            "productCodeList": {"type": "array", "items": {"type": "integer"}, "description": "Modo batch"},
            "branchCode": {"type": "integer"},
            "barCodeList": {"type": "array", "items": {"type": "string"}},
            "groupCode": {"type": "string"},
            "referenceCode": {"type": "string"},
            "weight": {"type": "number", "description": "Peso em kg"},
            "ncmCode": {"type": "string"},
            "measuredUnit": {"type": "string"},
            "cstCode": {"type": "string"},
            "cestCode": {"type": "string"},
            "prefixEanGtin": {"type": "string"},
            "isInactive": {"type": "boolean"},
            "isFinishedProduct": {"type": "boolean"},
            "isRawMaterial": {"type": "boolean"},
            "isBulkMaterial": {"type": "boolean"},
            "isOwnProduction": {"type": "boolean"}
        }
    }
),


# ============================================================
# ADICIONAR depois de totvs_update_product_data:
# ============================================================

types.Tool(
    name="totvs_update_product_simple",
    description=(
        "⚠️ ESCRITA — Atualiza dados de UM produto específico. "
        "Endpoint validado em produção (MOOUI alterar_peso.py). "
        "Use pra: peso, NCM, CST, flags. Para múltiplos produtos use totvs_update_product_data."
    ),
    inputSchema={
        "type": "object",
        "properties": {
            "productCode": {"type": "integer"},
            "branchCode": {"type": "integer"},
            "weight": {"type": "number"},
            "ncmCode": {"type": "string"},
            "cstCode": {"type": "string"},
            "cestCode": {"type": "string"},
            "prefixEanGtin": {"type": "string"},
            "isInactive": {"type": "boolean"},
            "isFinishedProduct": {"type": "boolean"},
            "isRawMaterial": {"type": "boolean"},
            "isBulkMaterial": {"type": "boolean"},
            "isOwnProduction": {"type": "boolean"}
        },
        "required": ["productCode"]
    }
),

types.Tool(
    name="totvs_update_product_branch_info_batch",
    description=(
        "⚠️ ESCRITA — Atualiza dados em LOTE de produtos numa filial. "
        "Endpoint PUT /branch-info/{branchCode}. "
        "Útil pra ativar/desativar muitos produtos."
    ),
    inputSchema={
        "type": "object",
        "properties": {
            "branchCode": {"type": "integer"},
            "productCodeList": {"type": "array", "items": {"type": "integer"}},
            "barCodeList": {"type": "array", "items": {"type": "string"}},
            "groupCode": {"type": "string"},
            "referenceCode": {"type": "string"},
            "isInactive": {"type": "boolean"},
            "isFinishedProduct": {"type": "boolean"},
            "isRawMaterial": {"type": "boolean"},
            "isBulkMaterial": {"type": "boolean"},
            "isOwnProduction": {"type": "boolean"}
        }
    }
),


# ============================================================
# SUBSTITUIR a tool totvs_search_prices existente:
# ============================================================

types.Tool(
    name="totvs_search_prices",
    description=(
        "Consulta preços de produtos. priceCodeList é OBRIGATÓRIO. "
        "Retorna items[] com prices[]={branchCode, priceCode, priceName, price, "
        "promotionalPrice, promotionalInformation, informationOtherPromotions}. "
        "Para alterar use totvs_update_product_price_only ou totvs_update_product_cost. "
        "Use option.prices=[{branchCode, priceCodeList, isPromotionalPrice, isScheduledPrice}] "
        "pra incluir blocos de promoção/agenda. "
        "Use fields=['productCode','prices.price'] para reduzir tokens."
    ),
    inputSchema={
        "type": "object",
        "properties": {
            "productCodeList": {"type": "array", "items": {"type": "integer"}},
            "referenceCodeList": {"type": "array", "items": {"type": "string"}},
            "priceCodeList": {"type": "array", "items": {"type": "integer"},
                "description": "Códigos de tabela (consulte priceTypes em totvs_get_context)"},
            "branchCodeList": {"type": "array", "items": {"type": "integer"}},
            "change": {
                "type": "object",
                "description": "Filtro de alterações no período",
                "properties": {
                    "startDate": {"type": "string"},
                    "endDate": {"type": "string"},
                    "inProduct": {"type": "boolean"},
                    "inPrice": {"type": "boolean"},
                    "inPromotionalPrice": {"type": "boolean"},
                    "inScheduledPrice": {"type": "boolean"},
                    "inDigitalPromotionPrice": {"type": "boolean"},
                    "branchPriceCodeList": {"type": "array", "items": {"type": "integer"}},
                    "priceCodeList": {"type": "array", "items": {"type": "integer"}}
                }
            },
            "option": {
                "type": "object",
                "description": "Opções de retorno",
                "properties": {
                    "prices": {
                        "type": "array",
                        "items": {
                            "type": "object",
                            "properties": {
                                "branchCode": {"type": "integer"},
                                "priceCodeList": {"type": "array", "items": {"type": "integer"}},
                                "isPromotionalPrice": {"type": "boolean"},
                                "isScheduledPrice": {"type": "boolean"}
                            }
                        }
                    },
                    "digitalPromotionPrices": {
                        "type": "object",
                        "properties": {
                            "branchCodeList": {"type": "array", "items": {"type": "integer"}},
                            "isInformationOtherDigitalPromotions": {"type": "boolean"}
                        }
                    }
                }
            },
            "page": {"type": "integer", "default": 1},
            "pageSize": {"type": "integer", "default": 100},
            "fields": {"type": "array", "items": {"type": "string"}}
        }
    }
),


# ============================================================
# SUBSTITUIR a tool totvs_get_context existente:
# ============================================================

types.Tool(
    name="totvs_get_context",
    description=(
        "Retorna dados de referência da empresa: filiais, tabelas de preço/custo "
        "EXISTENTES, operações, condições de pagamento. "
        "Por padrão retorna SLIM (~5KB com top 5 de cada lista). "
        "Use verbose=true pra retornar tudo (pode ser grande, ~50-300KB). "
        "Use isto pra descobrir priceCode/costCode válidos antes de chamar "
        "totvs_search_prices ou totvs_update_product_price_only."
    ),
    inputSchema={
        "type": "object",
        "properties": {
            "verbose": {
                "type": "boolean",
                "default": False,
                "description": "Se true, retorna cache completo. Default: slim."
            }
        }
    }
),
