import { component$, useSignal } from "@builder.io/qwik";
import { DocumentHead , RequestHandler, routeLoader$} from "@builder.io/qwik-city";
import { useNavigate } from "@builder.io/qwik-city";
import axios from "axios";
const diaryLoader = routeLoader$<{ message: string }>(async ({params, cookie, redirect}) => {
        
    try {
      console.log(params.username + "의 방명록 가져오기");
      const myInfo = await axios.get(`http://localhost:8000/api/user/me`, {
        headers: {
          Authorization: `Bearer ${cookie.get("access_token")?.value}`,
        },
      });
      if(myInfo.data.username !== params.username) {
        return redirect(302, `/diary/${params.diary_id}`);
      }
      const res = await axios.get(`http://localhost:8000/api/diary/${params.diary_id}`);
      console.log(res.data);
      return res.data;
    } catch (error : any) {
      console.error(error);
      return error.response?.data?.detail || 'Diary not found';
    }
  });
export default component$(() => {
    const navigate = useNavigate();
    const diaryData = diaryLoader();
   
    return (
        <div>
            <h1>일기 수정</h1>
        </div>
    );
});

export const head: DocumentHead = {
    title: "일기수정하기",
    meta: [
        {
            name: "description",
            content: "유저가 쓴 일기의 상세정보",
        },
    ],
};