export function dateFormatter(
    dateString: string,
    withDayName: boolean = false,
): string {
    if (!dateString) return "";

    const [year, month, day] = dateString.split("-").map(Number);
    const date = new Date(year, month - 1, day);

    const options: Intl.DateTimeFormatOptions = {
        day: "numeric",
        month: "long",
        year: "numeric",
    };

    if (withDayName) {
        options.weekday = "long";
    }

    return date.toLocaleDateString("id-ID", options);
}

export function timeFormatter(timeString: string) {
    if (!timeString) return "";

    // Extract hours and minutes from time string (HH:MM:SS)
    const timeParts = timeString.split(":");
    if (timeParts.length >= 2) {
        return `${timeParts[0]}:${timeParts[1]}`;
    }

    return timeString; // Return original if format is unexpected
}

export function dateTimeFormatter(
    dateTimeString: string,
    withDayName: boolean = false,
    withSeconds: boolean = false,
): string {
    if (!dateTimeString) return "";

    // Format ISO date string: "2025-09-24T15:30:45" atau "2025-09-24 15:30:45"
    const dateTimeParts = dateTimeString.replace("T", " ").split(" ");

    if (dateTimeParts.length !== 2) return dateTimeString;

    const datePart = dateTimeParts[0]; // "2025-09-24"
    const timePart = dateTimeParts[1]; // "15:30:45"

    const formattedDate = dateFormatter(datePart, withDayName);

    let formattedTime = "";
    const timeParts = timePart.split(":");

    if (timeParts.length >= 2) {
        formattedTime =
            withSeconds && timeParts.length >= 3
                ? `${timeParts[0]}:${timeParts[1]}:${timeParts[2]}`
                : `${timeParts[0]}:${timeParts[1]}`;
    } else {
        formattedTime = timePart;
    }

    return `${formattedDate}, ${formattedTime}`;
}

export function humanReadableTime(
    dateTimeString: string,
    shortFormat: boolean = false,
): string {
    if (!dateTimeString) return "";

    // Parse date string
    const targetDate = new Date(dateTimeString);
    const now = new Date();
    const diffInMs = now.getTime() - targetDate.getTime();
    const diffInSeconds = Math.floor(diffInMs / 1000);
    const diffInMinutes = Math.floor(diffInSeconds / 60);
    const diffInHours = Math.floor(diffInMinutes / 60);
    const diffInDays = Math.floor(diffInHours / 24);
    const diffInWeeks = Math.floor(diffInDays / 7);
    const diffInMonths = Math.floor(diffInDays / 30);
    const diffInYears = Math.floor(diffInDays / 365);

    // Handle future dates
    if (diffInMs < 0) {
        const absDiffInDays = Math.abs(diffInDays);
        const absDiffInHours = Math.abs(diffInHours);
        const absDiffInMinutes = Math.abs(diffInMinutes);

        if (absDiffInDays >= 1) {
            return shortFormat
                ? `dalam ${absDiffInDays} hari`
                : `dalam ${absDiffInDays} hari lagi`;
        } else if (absDiffInHours >= 1) {
            return shortFormat
                ? `dalam ${absDiffInHours} jam`
                : `dalam ${absDiffInHours} jam lagi`;
        } else if (absDiffInMinutes >= 1) {
            return shortFormat
                ? `dalam ${absDiffInMinutes} menit`
                : `dalam ${absDiffInMinutes} menit lagi`;
        } else {
            return shortFormat ? "segera" : "beberapa saat lagi";
        }
    }

    // Handle past dates
    if (diffInYears >= 1) {
        return shortFormat
            ? `${diffInYears} tahun lalu`
            : `${diffInYears} tahun yang lalu`;
    } else if (diffInMonths >= 1) {
        return shortFormat
            ? `${diffInMonths} bulan lalu`
            : `${diffInMonths} bulan yang lalu`;
    } else if (diffInWeeks >= 1) {
        return shortFormat
            ? `${diffInWeeks} minggu lalu`
            : `${diffInWeeks} minggu yang lalu`;
    } else if (diffInDays >= 1) {
        return shortFormat
            ? `${diffInDays} hari lalu`
            : `${diffInDays} hari yang lalu`;
    } else if (diffInHours >= 1) {
        return shortFormat
            ? `${diffInHours} jam lalu`
            : `${diffInHours} jam yang lalu`;
    } else if (diffInMinutes >= 1) {
        return shortFormat
            ? `${diffInMinutes} menit lalu`
            : `${diffInMinutes} menit yang lalu`;
    } else if (diffInSeconds >= 30) {
        return shortFormat
            ? `${diffInSeconds} detik lalu`
            : `${diffInSeconds} detik yang lalu`;
    } else {
        return "baru saja";
    }
}
