'use client';

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

function buildColumns(showSession: boolean): 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>
					{showSession && (
						<span className='market__cls-badge'>{MARKET_SESSION_LABEL[row.session] ?? row.session}</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 || '—'
		}
	];
}

// showSession — 야간 세션 조회 시에만 종목명 옆 세션 배지 표시 (정규 기본 화면은 기존과 동일)
export default function FuturesTable({ rows, showSession = false }: { rows: FuturesRow[]; showSession?: boolean })
{
	return (
		<DataTable<FuturesRow>
			caption='선물 목록 — 종목명, 현재가, 대비, 정산가, 거래량, 미결제약정, 상품'
			columns={buildColumns(showSession)}
			rows={rows}
			rowKey={(row) => row.isuCode}
			emptyMessage='표시할 선물이 없습니다.'
		/>
	);
}
