import type { GoalConfigItem } from '@/types/response/donation/goalConfig';
import { GOAL_TARGET_AMOUNT_DEFAULT } from '@/constants/donation';

export type FormState = Omit<GoalConfigItem, '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 GOAL_STYLES = [
	{ value: 0, label: 'Simple' },
	{ value: 1, label: 'Gradient' },
	{ value: 2, label: 'Neon' }
];

export const createEmptyForm = (): FormState => ({
	title: '후원 목표',
	style: 0,
	startAmount: 0,
	targetAmount: GOAL_TARGET_AMOUNT_DEFAULT,
	startAt: formatInput(new Date()),
	endAt: formatInput(new Date(Date.now() + 86400000)),
	isShowPercent: true,
	barColor: '#4f46e5',
	barBackgroundColor: '#e0e7ff',
	barHeightPx: 24,
	titleFontFamily: null,
	titleFontSizePx: 16,
	titleFontColor: '#ffffff',
	amountFontFamily: null,
	amountFontSizePx: 14,
	amountFontColor: '#ffffff',
	isActive: true
});