// app/[slug]/page.tsx
import { renderPostContent } from '@/components/WPRenderer/WPRenderer';

// ISR: regenerate every 60 seconds
export const revalidate = 60;

interface PageProps {
    params: {
        slug: string;
    };
}

export default async function PostPage({ params }: PageProps) {
    const { slug } = await params;
    const baseUrl =
        process.env.NEXT_PUBLIC_API_URL || 'http://localhost:3000';

    const res = await fetch(`${baseUrl}/api/posts/${slug}`, {
        next: { revalidate: 60 },
    });

    if (!res.ok) {
        console.log(`${baseUrl}/api/posts/${slug}`)
        // 🚨 THROW — this is critical
        throw new Error('Failed to fetch post');
    }

    const post = await res.json();

    return (
        <div className="container mx-auto py-8 max-w-5xl">
            <article className="prose lg:prose-xl max-w-none">
                <h1>{post.post_title}</h1>

                {post.post_type === 'post' && (
                    <div className="text-gray-600 mb-6">
                        <time>
                            {new Date(post.post_date).toLocaleString('ru-RU')}
                        </time>
                    </div>
                )}

                {renderPostContent(post.post_content)}
            </article>
        </div>
    );
}
