import type { RankConfigItem } from '@/types/response/donation/rankConfig';

export type FormState = Omit<RankConfigItem, 'id'>;

/** Date → "yyyy.MM.dd HH:mm" 표시용 문자열 */
export const formatInput = (date: Date) => {
	const y = date.getFullYear();
	const m = String(date.getMonth() + 1).padStart(2, '0');
	const d = String(date.getDate()).padStart(2, '0');
	const h = String(date.getHours()).padStart(2, '0');
	const min = String(date.getMinutes()).padStart(2, '0');
	return `${y}.${m}.${d} ${h}:${min}`;
};

/** 날짜를 "yyyy.MM.dd HH:mm:ss" 형식으로 포맷 (목록용) */
export const formatDateTime = (dateStr: string|null) => {
	if (!dateStr) {
		return '-';
	}
	const d = new Date(dateStr);
	const sec = String(d.getSeconds()).padStart(2, '0');
	return `${formatInput(d)}:${sec}`;
};

/** "yyyy.MM.dd HH:mm" → ISO string (서버 전송용), 실패 시 빈 문자열 */
export const parseInput = (str: string): string => {
	const match = str.match(/^(\d{4})\.(\d{2})\.(\d{2})\s+(\d{2}):(\d{2})$/);
	if (!match) {
		return '';
	}
	const [, y, m, d, h, min] = match;
	const date = new Date(+y, +m - 1, +d, +h, +min);
	if (isNaN(date.getTime())) {
		return '';
	}
	return date.toISOString();
};

export const createEmptyForm = (): FormState => ({
	title: '후원 순위',
	theme: 0,
	period: 3,
	startAt: null,
	endAt: null,
	isShowAmount: true,
	maxRankCount: 5,
	nameMode: null,
	isActive: true,
	titleFontFamily: null,
	titleFontSizePx: 18,
	titleFontColor: '#FFFFFF',
	nameDisplayType: 0,
	isShowDonationCount: false,
	isShowGradeIcon: false,
	isShowMemberIcon: false,
	rank1FontFamily: null,
	rank1FontSizePx: 15,
	rank1FontColor: '#FFD700',
	rank2FontFamily: null,
	rank2FontSizePx: 15,
	rank2FontColor: '#C0C0C0',
	rank3FontFamily: null,
	rank3FontSizePx: 15,
	rank3FontColor: '#CD7F32'
});