'use client';

import DataTable, { DataTableColumn } from '@/components/table/DataTable';
import { DividendCalendarRow, DIVIDEND_SORT_LABEL, DIVIDEND_FIX_LABEL, DIVIDEND_SETTLE_LABEL } from '@/types/seibro';
import { codeLabel } from '@/lib/utils/market';

function buildColumns(): DataTableColumn<DividendCalendarRow>[] {
	return [
		{
			key: 'name',
			header: '종목명',
			align: 'left',
			priority: 'high',
			cellClassName: 'data-table__cell--name',
			cell: (row) => <span className='stock-cell__name'>{row.name}</span>
		},
		{
			key: 'rgtStdDt',
			header: '권리기준일',
			align: 'center',
			priority: 'high',
			cell: (row) => row.rgtStdDt
		},
		{
			key: 'xrgtDt',
			header: '권리락일',
			align: 'center',
			priority: 'medium',
			cell: (row) => row.xrgtDt ?? '—'
		},
		{
			key: 'sortCode',
			header: '배당구분',
			align: 'center',
			priority: 'high',
			cell: (row) => <span className='market__cls-badge'>{codeLabel(row.sortCode, DIVIDEND_SORT_LABEL)}</span>
		},
		{
			key: 'settleType',
			header: '결산',
			align: 'center',
			priority: 'low',
			cell: (row) => codeLabel(row.settleType, DIVIDEND_SETTLE_LABEL)
		},
		{
			key: 'fixType',
			header: '확정',
			align: 'center',
			priority: 'low',
			cell: (row) => codeLabel(row.fixType, DIVIDEND_FIX_LABEL)
		}
	];
}

export default function DividendCalendarTable({ rows }: { rows: DividendCalendarRow[] })
{
	return (
		<DataTable<DividendCalendarRow>
			caption='배당 캘린더 — 종목명, 권리기준일, 권리락일, 배당구분, 결산, 확정'
			columns={buildColumns()}
			rows={rows}
			rowKey={(row, index) => `${row.issucoCustno}-${row.rgtStdDt}-${row.sortCode}-${index}`}
			emptyMessage='표시할 배당 일정이 없습니다.'
		/>
	);
}
