'use client';

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

function buildColumns(): DataTableColumn<EsgSecurityRow>[] {
	return [
		{
			key: 'name',
			header: '종목명',
			align: 'left',
			priority: 'high',
			cellClassName: 'data-table__cell--name',
			cell: (row) => <span className='stock-cell__name'>{row.name}</span>
		},
		{
			key: 'close',
			header: '현재가',
			align: 'right',
			priority: 'high',
			cell: (row) => formatDecimal(row.close)
		},
		{
			key: 'change',
			header: '등락률',
			align: 'right',
			priority: 'high',
			cell: (row) => <PriceChange rate={row.changeRate} />
		},
		{
			key: 'shares',
			header: '상장주식수',
			align: 'right',
			priority: 'low',
			cell: (row) => formatNumber(row.listedShares)
		},
		{
			key: 'volume',
			header: '거래량',
			align: 'right',
			priority: 'medium',
			cell: (row) => formatNumber(row.volume)
		}
	];
}

export default function EsgSecuritiesTable({ rows }: { rows: EsgSecurityRow[] })
{
	return (
		<DataTable<EsgSecurityRow>
			caption='ESG 증권 목록 — 종목명, 현재가, 등락률, 상장주식수, 거래량'
			columns={buildColumns()}
			rows={rows}
			rowKey={(row, index) => `${row.name}-${index}`}
			emptyMessage='표시할 ESG 증권이 없습니다.'
		/>
	);
}
