const std = @import("std");

const gpa = std.heap.page_allocator;

pub fn secs_to_time(secs: u64) ![]const u8 {
    var list = try std.ArrayList(u8).initCapacity(gpa, 4);
    defer list.deinit(gpa);
    var writer = list.writer(gpa);
    var seconds = secs;

    const minute = 1 * 60;
    const hour = minute * 60;
    const day = hour * 24;
    const week = day * 7;

    const weeks = seconds / week;
    seconds = seconds % week;
    const days = seconds / day;
    seconds = seconds % day;
    const hours = seconds / hour;
    seconds = seconds % hour;
    const minutes = seconds / minute;
    seconds = seconds % minute;

    if (weeks > 0) try writer.print("{} {s}{s}, ", .{weeks, "week", if (weeks > 1) "s" else ""});
    if (days > 0) try writer.print("{} {s}{s}, ", .{days, "day", if (days > 1) "s" else ""});
    if (hours > 0) try writer.print("{} {s}{s}, ", .{hours, "hour", if (hours > 1) "s" else ""});
    if (minutes > 0) try writer.print("{} {s}{s}", .{minutes, "minute", if (minutes > 1) "s" else ""});

    if (weeks == 0 and days == 0 and hours == 0 and minutes == 0) {
        try writer.print("{} {s}{s}", .{seconds, "second", if (seconds > 1) "s" else ""});
    }

    return try list.toOwnedSlice(gpa);
}
