'use client';
"use client";

import { useState } from "react";

const API =
process.env.NEXT_PUBLIC_API_URL ||
"http://127.0.0.1:8000";

export default function ProductionAIChat(){

const [prompt,setPrompt]=useState("");
const [messages,setMessages]=useState([]);

async function send(){

if(!prompt.trim()) return;

const user={
role:"user",
content:prompt
};

setMessages(v=>[...v,user]);

const res=await fetch(API+"/api/workspace/chat/message",{
method:"POST",
headers:{
"Content-Type":"application/json"
},
body:JSON.stringify({
message:prompt
})
});

const data=await res.json();

setMessages(v=>[
...v,
{
role:"assistant",
content:
data.message ||
data.response ||
JSON.stringify(data)
}
]);

setPrompt("");

}

return(

<div className="flex flex-col h-full bg-[#111827]">

<div className="flex-1 overflow-auto p-4 space-y-3">

{messages.map((m,i)=>(

<div
key={i}
className={
m.role==="assistant"
?
"bg-slate-700 rounded p-3"
:
"bg-blue-700 rounded p-3"
}
>

{m.content}

</div>

))}

</div>

<div className="border-t border-slate-700 p-3 flex gap-2">

<input
className="flex-1 rounded bg-slate-900 p-2"
value={prompt}
onChange={e=>setPrompt(e.target.value)}
placeholder="Ask Traveler Dev..."
/>

<button
onClick={send}
className="bg-blue-600 px-4 rounded"
>

Send

</button>

</div>

</div>

);

}
