import '../style.scss';
import '@/app/styles/data-table.scss';
import { Metadata } from 'next';
import MarketNav from '../_components/MarketNav';
import MeetingCalendarTable from '../_components/MeetingCalendarTable';
import Pager from '../_components/Pager';
import { fetchMeetingCalendar } from '@/lib/api/seibro';
import { formatNumber } from '@/lib/utils/stock';

export const metadata: Metadata = {
	title: '주총 캘린더 — 개미투자',
	description: '주주총회 일정 — 총회일·종류(정기/임시)·전자투표·안건수 (SEIBro 수집)'
};

const PER_PAGE = 30;

type Props = {
	searchParams: Promise<{
		page?: string;
	}>;
};

export default async function MarketMeetingsPage({ searchParams }: Props)
{
	const query = await searchParams;
	const page = Math.max(Number(query.page) || 1, 1);

	const res = await fetchMeetingCalendar({ page, perPage: PER_PAGE });
	const data = res.success ? res.data : null;

	return (
		<div className='market'>
			<div className='market__head'>
				<h1 className='market__title'>주총 캘린더</h1>
				<span className='market__basis'>총회일 최신순 · 전자투표·안건수 (SEIBro)</span>
			</div>

			<MarketNav />

			{!res.success ? (
				<div className='market__error' role='alert'>
					<strong>주총 일정을 불러오지 못했습니다.</strong>
					잠시 후 다시 시도해 주세요.
				</div>
			) : !data || data.list.length === 0 ? (
				<div className='market__empty'>표시할 주총 일정이 없습니다.</div>
			) : (
				<>
					<p className='market__total'>전체 {formatNumber(data.total)}건</p>
					<div className='market__table-wrap'>
						<MeetingCalendarTable rows={data.list} />
					</div>
					<Pager
						total={data.total}
						page={page}
						perPage={PER_PAGE}
						basePath='/market/meetings'
						query={{}}
					/>
				</>
			)}
		</div>
	);
}
