'use client';

import DataTable, { DataTableColumn } from '@/components/table/DataTable';
import { FuturesRow } from '@/types/market';
import { formatNumber } from '@/lib/utils/stock';
import { formatDecimal } from '@/lib/utils/market';

function buildColumns(): DataTableColumn<FuturesRow>[] {
	return [
		{
			key: 'name',
			header: '종목명',
			align: 'left',
			priority: 'high',
			cellClassName: 'data-table__cell--name',
			cell: (row) => (
				<span className='stock-cell'>
					<span className='stock-cell__name'>{row.isuName}</span>
					<span className='stock-cell__code'>{row.isuCode}</span>
				</span>
			)
		},
		{
			key: 'close',
			header: '현재가',
			align: 'right',
			priority: 'high',
			cell: (row) => formatDecimal(row.close)
		},
		{
			key: 'changeAmount',
			header: '대비',
			align: 'right',
			priority: 'high',
			cell: (row) => formatDecimal(row.changeAmount)
		},
		{
			key: 'settle',
			header: '정산가',
			align: 'right',
			priority: 'low',
			cell: (row) => formatDecimal(row.settlePrice)
		},
		{
			key: 'volume',
			header: '거래량',
			align: 'right',
			priority: 'medium',
			cell: (row) => formatNumber(row.volume)
		},
		{
			key: 'oi',
			header: '미결제약정',
			align: 'right',
			priority: 'low',
			cell: (row) => formatNumber(row.openInterest)
		},
		{
			key: 'product',
			header: '상품',
			align: 'left',
			priority: 'low',
			cell: (row) => row.productName || '—'
		}
	];
}

export default function FuturesTable({ rows }: { rows: FuturesRow[] })
{
	return (
		<DataTable<FuturesRow>
			caption='선물 목록 — 종목명, 현재가, 대비, 정산가, 거래량, 미결제약정, 상품'
			columns={buildColumns()}
			rows={rows}
			rowKey={(row) => row.isuCode}
			emptyMessage='표시할 선물이 없습니다.'
		/>
	);
}
