// app/admin/dashboard/posts/edit/[id]/page.tsx
import PostForm from '@/app/components/admin/posts/PostForm';
import { authService } from '@/services/auth-service';
import { PERMISSIONS } from '@/lib/permissions';
import { redirect } from 'next/navigation';
import { getTokenFromCookies } from '@/lib/auth-utils';
import { Metadata } from 'next';
import Link from 'next/link';

export const metadata: Metadata = { title: 'Edit Post' };

interface Props { params: Promise<{ id: string }>; }

export default async function EditPostPage({ params }: Props) {
  const { id } = await params;
  const token = await getTokenFromCookies();
  if (!token) redirect('/admin/login');
  
  const hasPermission = await authService.checkUserPermission(token, PERMISSIONS.CONTENT_UPDATE);
  if (!hasPermission) {
    return (
      <div className="max-w-5xl mx-auto">
        <div className="bg-var-danger-bg border border-var-danger-border rounded-2xl p-6 text-center">
          <h1 className="text-2xl font-bold text-var-danger-text">Access Denied</h1>
        </div>
      </div>
    );
  }
  
  return (
    <div className="space-y-6">
      <div className="flex items-center gap-3">
        <Link
          href="/admin/dashboard/posts"
          className="text-var-text-muted hover:text-var-text transition-colors"
        >
          <svg className="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
            <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M10 19l-7-7m0 0l7-7m-7 7h18" />
          </svg>
        </Link>
        <div>
          <h1 className="text-2xl font-bold text-var-text">Edit Post</h1>
          <p className="text-var-text-secondary mt-1">Update post content and settings</p>
        </div>
      </div>
      <PostForm postId={id} />
    </div>
  );
}