'use client';

import { useRouter } from 'next/navigation';
import { PageState } from '@/components/common/feedback/PageState';

/**
 * 403 页面（规则 §5.3.7 写操作未授权明确反馈）
 *
 * 后端返回 403 时使用：写操作中记录存在但 DataScope 不覆盖。
 * 与 404 区分：用户主动行为，需要明确"这条你没权限改"，避免盲目重试。
 *
 * 特殊 code 展示：
 * - SYSTEM_RECORD_READ_ONLY：系统内置记录只读
 */
export function ForbiddenPage({
  title,
  description,
  errorCode,
  onBack,
}: {
  title?: string;
  description?: string;
  errorCode?: string;
  onBack?: () => void;
}) {
  const router = useRouter();
  const handleBack = onBack || (() => router.back());

  const isSystemReadonly = errorCode === 'SYSTEM_RECORD_READ_ONLY';
  const resolvedTitle =
    title ?? (isSystemReadonly ? '系统内置记录，只读' : '无权访问');
  const resolvedDesc =
    description ??
    (isSystemReadonly
      ? '这条记录是系统内置配置，只有 system:admin 角色可以修改。'
      : '您没有权限执行此操作。如需协助，请联系管理员。');

  return (
    <div className="flex min-h-[60vh] flex-col items-center justify-center gap-3 p-6">
      <div className="w-full max-w-md">
        <PageState
          variant="error"
          layout="card"
          title={resolvedTitle}
          description={resolvedDesc}
          actionLabel="返回"
          onAction={handleBack}
        />
      </div>
      {errorCode && (
        <div className="rounded bg-gray-100 px-3 py-1 font-mono text-xs text-gray-600">
          {errorCode}
        </div>
      )}
    </div>
  );
}
