// app/admin/dashboard/posts/new/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: 'New Post',
  description: 'Create a new blog post.',
};

export default async function NewPostPage() {
  const token = await getTokenFromCookies();
  if (!token) redirect('/admin/login');

  const hasPermission = await authService.checkUserPermission(token, PERMISSIONS.CONTENT_CREATE);
  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>
          <p className="text-var-danger-text mt-2">You don&apos;t have permission to create posts.</p>
        </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">New Post</h1>
          <p className="text-var-text-secondary mt-1">Create a new blog post</p>
        </div>
      </div>
      <PostForm />
    </div>
  );
}