/**
 * Date formatting utilities for Japanese locale
 */

/**
 * Format date to Japanese format (YYYY年MM月DD日)
 * @param date - Date object or ISO string
 * @returns Formatted Japanese date string
 * @example
 * formatJapaneseDate(new Date('2026-02-18')) // "2026年2月18日"
 */
export function formatJapaneseDate(date: Date | string): string {
  const dateObj = typeof date === 'string' ? new Date(date) : date;
  
  // Check for invalid date
  if (isNaN(dateObj.getTime())) {
    return '';
  }
  
  const year = dateObj.getFullYear();
  const month = dateObj.getMonth() + 1; // getMonth() returns 0-11
  const day = dateObj.getDate();
  
  return `${year}年${month}月${day}日`;
}

/**
 * Format relative time in Japanese (X時間前, X日前, etc.)
 * @param date - Date object or ISO string
 * @returns Formatted relative time string in Japanese
 * @example
 * formatRelativeTime(new Date(Date.now() - 3600000)) // "1時間前"
 * formatRelativeTime(new Date(Date.now() - 86400000)) // "1日前"
 */
export function formatRelativeTime(date: Date | string): string {
  const dateObj = typeof date === 'string' ? new Date(date) : date;
  
  // Check for invalid date
  if (isNaN(dateObj.getTime())) {
    return '';
  }
  
  const now = new Date();
  const diffMs = now.getTime() - dateObj.getTime();
  const diffSeconds = Math.floor(diffMs / 1000);
  const diffMinutes = Math.floor(diffSeconds / 60);
  const diffHours = Math.floor(diffMinutes / 60);
  const diffDays = Math.floor(diffHours / 24);
  const diffWeeks = Math.floor(diffDays / 7);
  const diffMonths = Math.floor(diffDays / 30);
  const diffYears = Math.floor(diffDays / 365);
  
  // Future dates
  if (diffMs < 0) {
    return formatJapaneseDate(dateObj);
  }
  
  // Just now (less than 1 minute)
  if (diffMinutes < 1) {
    return 'たった今';
  }
  
  // Minutes ago (1-59 minutes)
  if (diffMinutes < 60) {
    return `${diffMinutes}分前`;
  }
  
  // Hours ago (1-23 hours)
  if (diffHours < 24) {
    return `${diffHours}時間前`;
  }
  
  // Days ago (1-6 days)
  if (diffDays < 7) {
    return `${diffDays}日前`;
  }
  
  // Weeks ago (1-3 weeks)
  if (diffWeeks < 4) {
    return `${diffWeeks}週間前`;
  }
  
  // Months ago (1-11 months)
  if (diffMonths < 12) {
    return `${diffMonths}ヶ月前`;
  }
  
  // Years ago (1+ years)
  return `${diffYears}年前`;
}

/**
 * Format date with time in Japanese format (YYYY年MM月DD日 HH:MM)
 * @param date - Date object or ISO string
 * @returns Formatted Japanese date and time string
 * @example
 * formatJapaneseDatetime(new Date('2026-02-18T14:30:00')) // "2026年2月18日 14:30"
 */
export function formatJapaneseDatetime(date: Date | string): string {
  const dateObj = typeof date === 'string' ? new Date(date) : date;
  
  // Check for invalid date
  if (isNaN(dateObj.getTime())) {
    return '';
  }
  
  const datePart = formatJapaneseDate(dateObj);
  const hours = String(dateObj.getHours()).padStart(2, '0');
  const minutes = String(dateObj.getMinutes()).padStart(2, '0');
  
  return `${datePart} ${hours}:${minutes}`;
}
