'use client';

// D6 §5.1 — 게시글행(PostRow) 열 구성 빌더 + PostDataTable 편의 래퍼.
// 제목(말줄임) + 댓글수 | 작성자 | 작성일 | 조회 | 추천 (도미노 패턴, 우측 정렬 카운트).
// 작성일/조회는 우선순위 낮음 → mobile drop.
import Link from 'next/link';
import DataTable, { DataTableColumn } from '@/components/table/DataTable';
import Post from '@/types/forum/post';
import { formatDate } from '@/lib/utils/client';
import { formatNumber } from '@/lib/utils/stock';

export function buildPostColumns(): DataTableColumn<Post>[] {
	return [
		{
			key: 'subject',
			header: '제목',
			align: 'left',
			priority: 'high',
			cellClassName: 'data-table__cell--name',
			cell: (row) => (
				<Link href={`/post/${row.id}`} className='post-cell'>
					{row.boardPrefix && (
						<span className='post-cell__prefix'>[{row.boardPrefix.name}]</span>
					)}
					<span className='post-cell__subject'>{row.subject}</span>
					{row.comments > 0 && (
						<span className='post-cell__comments'>[{row.comments}]</span>
					)}
				</Link>
			)
		},
		{
			key: 'author',
			header: '작성자',
			align: 'left',
			priority: 'medium',
			cell: (row) => row.name || row.sid || '-'
		},
		{
			key: 'createdAt',
			header: '작성일',
			align: 'right',
			priority: 'low',
			cell: (row) => formatDate(row.createdAt)
		},
		{
			key: 'views',
			header: '조회',
			align: 'right',
			priority: 'low',
			cell: (row) => formatNumber(row.views)
		},
		{
			key: 'likes',
			header: '추천',
			align: 'right',
			priority: 'high',
			cell: (row) => formatNumber(row.likes)
		}
	];
}

interface PostDataTableProps {
	rows: Post[];
	caption?: string;
	emptyMessage?: string;
}

// 게시글 목록 편의 래퍼 — 기본 열 구성으로 DataTable 렌더
export default function PostDataTable({ rows, caption, emptyMessage }: PostDataTableProps)
{
	return (
		<DataTable<Post>
			caption={caption ?? '게시글 목록 — 제목, 작성자, 작성일, 조회수, 추천수'}
			columns={buildPostColumns()}
			rows={rows}
			rowKey={(row) => row.id}
			emptyMessage={emptyMessage ?? '등록된 글이 없습니다.'}
		/>
	);
}
