import '../style.scss';
import '@/app/styles/data-table.scss';
import { Metadata } from 'next';
import MarketNav from '../_components/MarketNav';
import LockupCalendarTable from '../_components/LockupCalendarTable';
import Pager from '../_components/Pager';
import { fetchLockupCalendar } 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 MarketLockupPage({ searchParams }: Props)
{
	const query = await searchParams;
	const page = Math.max(Number(query.page) || 1, 1);

	const res = await fetchLockupCalendar({ 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'>
						<LockupCalendarTable rows={data.list} />
					</div>
					<Pager
						total={data.total}
						page={page}
						perPage={PER_PAGE}
						basePath='/market/lockup'
						query={{}}
					/>
				</>
			)}
		</div>
	);
}
