'use client';

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

function buildColumns(showSession: boolean): DataTableColumn<OptionsRow>[] {
	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: 'right',
			header: '권리',
			align: 'center',
			priority: 'high',
			cell: (row) => <span className='market__cls-badge'>{OPTION_RIGHT_TYPE_LABEL[row.rightType] ?? row.rightType}</span>
		},
		{
			key: 'strike',
			header: '행사가',
			align: 'right',
			priority: 'medium',
			cell: (row) => formatDecimal(row.strikePrice)
		},
		{
			key: 'close',
			header: '현재가',
			align: 'right',
			priority: 'high',
			cell: (row) => formatDecimal(row.close)
		},
		{
			key: 'iv',
			header: '내재변동성',
			align: 'right',
			priority: 'low',
			cell: (row) => formatDecimal(row.impliedVolatility)
		},
		{
			key: 'volume',
			header: '거래량',
			align: 'right',
			priority: 'medium',
			cell: (row) => formatNumber(row.volume)
		},
		{
			key: 'oi',
			header: '미결제약정',
			align: 'right',
			priority: 'low',
			cell: (row) => formatNumber(row.openInterest)
		}
	];
}

// showSession — 야간 세션 조회 시에만 종목명 옆 세션 배지 표시 (정규 기본 화면은 기존과 동일)
export default function OptionsTable({ rows, showSession = false }: { rows: OptionsRow[]; showSession?: boolean })
{
	return (
		<DataTable<OptionsRow>
			caption='옵션 목록 — 종목명, 권리유형, 행사가, 현재가, 내재변동성, 거래량, 미결제약정'
			columns={buildColumns(showSession)}
			rows={rows}
			rowKey={(row) => row.isuCode}
			emptyMessage='표시할 옵션이 없습니다.'
		/>
	);
}
