'use client';

/**
 * 게시글 상세 페이지
 */

import { useParams } from 'next/navigation';
import { useMemo } from 'react';
import { format } from 'date-fns';
import { BoardDetail } from '@/components/board/BoardDetail';
import type { Post, Comment } from '@/components/board/types';
import { MOCK_BOARDS } from '@/components/board/types';

// 현재 로그인 사용자 ID (실제로는 auth context에서 가져옴)
const CURRENT_USER_ID = 'user1';

// Mock 데이터 생성 (실제로는 API에서 가져옴)
const generateMockPost = (id: string): Post => {
  const boards = MOCK_BOARDS.filter((b) => b.id !== 'all');
  const board = boards[0];

  return {
    id,
    boardId: board.id,
    boardName: board.name,
    title: '제목',
    content: `
      <p>게시글 내용입니다.</p>
      <p>이것은 <strong>테스트용</strong> 콘텐츠입니다.</p>
      <p><img src="https://via.placeholder.com/600x300" alt="IMG" /></p>
      <p>내용</p>
    `,
    authorId: 'user1',
    authorName: '홍길동',
    authorDepartment: '개발팀',
    authorPosition: '과장',
    isPinned: false,
    allowComments: true,
    viewCount: 123,
    attachments: [
      {
        id: 'file-1',
        fileName: 'abc.pdf',
        fileSize: 1024000,
        fileUrl: '/files/abc.pdf',
        mimeType: 'application/pdf',
      },
    ],
    createdAt: format(new Date(2025, 8, 3, 12, 23), "yyyy-MM-dd'T'HH:mm:ss"),
    updatedAt: format(new Date(2025, 8, 3, 12, 23), "yyyy-MM-dd'T'HH:mm:ss"),
  };
};

const generateMockComments = (postId: string): Comment[] => [
  {
    id: 'comment-1',
    postId,
    authorId: 'user2',
    authorName: '이름 직책',
    authorDepartment: '부서명',
    authorPosition: '',
    content: '댓글 내용',
    createdAt: format(new Date(2025, 8, 3, 15, 33), "yyyy-MM-dd'T'HH:mm:ss"),
    updatedAt: format(new Date(2025, 8, 3, 15, 33), "yyyy-MM-dd'T'HH:mm:ss"),
  },
  {
    id: 'comment-2',
    postId,
    authorId: 'user1', // 본인 댓글
    authorName: '이름 직책',
    authorDepartment: '부서명',
    authorPosition: '',
    content: '댓글 내용',
    createdAt: format(new Date(2025, 8, 3, 15, 33), "yyyy-MM-dd'T'HH:mm:ss"),
    updatedAt: format(new Date(2025, 8, 3, 15, 33), "yyyy-MM-dd'T'HH:mm:ss"),
  },
];

export default function BoardDetailPage() {
  const params = useParams();
  const postId = params.id as string;

  // Mock 데이터 (실제로는 API에서 가져옴)
  const post = useMemo(() => generateMockPost(postId), [postId]);
  const comments = useMemo(() => generateMockComments(postId), [postId]);

  return (
    <BoardDetail
      post={post}
      comments={comments}
      currentUserId={CURRENT_USER_ID}
    />
  );
}