from langchain_ollama import ChatOllama
from langchain_core.prompts import PromptTemplate
from langchain_core.output_parsers import StrOutputParser

llm1 = ChatOllama(model="llama3.2", temperature=0)
llm2 = ChatOllama(model="llama3.2", temperature=0)
llm3 = ChatOllama(model="llama3.2", temperature=0)


spec_to_schema = PromptTemplate.from_template(
    """You are a senior backend engineer.
From the following product spec, extract a minimal JSON schema with fields and types.
Only return JSON. No commentary.

Write all the code using markdown code blocks.  

Spec:
{spec}
"""
) | llm1 | StrOutputParser()

schema_to_routes = PromptTemplate.from_template(
    """You are a senior Go developer.
Given the JSON schema below, design REST routes and sketch idiomatic Go handlers for CRUD.
Keep it concise, production-oriented, and show code snippets.

Write all the code using markdown code blocks.  

Schema JSON:
{schema_json}
"""
) | llm2 | StrOutputParser()

commit_message = PromptTemplate.from_template(
    """You are a pragmatic developer.
Write a single-line conventional commit message summarizing the new API based on the schema and routes.

Schema:
{schema_json}

Routes and handlers:
{routes}
"""
) | llm3 | StrOutputParser()


spec_text = """We need a Products API.
Fields: id (uuid), name (string, required), description (string), price (float, > 0), stock (int, >= 0).
We must support list with pagination, create, get by id, update, delete.
"""

schema_json = spec_to_schema.invoke({"spec": spec_text})
routes = schema_to_routes.invoke({"schema_json": schema_json})
commit = commit_message.invoke({"schema_json": schema_json, "routes": routes})


result_content = f"""# Prompt Chaining Result

## SCHEMA (Generated by Ollama3.2)
{schema_json}

## ROUTES & HANDLERS (Go) (Generated by GPT-5-mini)
{routes}

## COMMIT (Generated by GPT-4o-mini)
{commit}

---
**Pipeline Models:**
- Step 1: Ollama3.2
- Step 2: Ollama3.x
- Step 3: Ollama3.x 

"""

with open("prompt_chaining_result.md", "w", encoding="utf-8") as f:
    f.write(result_content)

print("Resultado salvo em prompt_chaining_result.md")
