// std.event (PLAN-HTTPZ S5): the OS-neutral Loop — idle timeout costs
// nothing, read readiness carries the registration's udata, write
// interest reports an empty send buffer immediately, EOF is a flag not
// an errno, and the deadline helpers do monotonic timeout math.
#import "modules/std.sx";

main :: () -> i32 {
    pair : [2]i32 = ---;
    if socket.socketpair(socket.AF_UNIX, socket.SOCK_STREAM, 0, @pair[0]) != 0 {
        print("socketpair failed\n");
        return 1;
    }
    a := pair[0];
    b := pair[1];

    loop, ie := event.Loop.init();
    if ie { print("loop init failed\n"); return 1; }
    are := false;
    loop.add_read(a, 41) catch { are = true; };
    if are { print("add_read failed\n"); return 1; }

    evs : [8]event.Event = ---;

    // Idle: a bounded wait returns 0 events, and roughly honors the bound.
    t0 := event.deadline_in(40);
    n, we := loop.wait(.{ ptr = @evs[0], len = 8 }, 40);
    if we { print("wait failed\n"); return 1; }
    if n != 0 { print("idle wait: expected 0 events, got {}\n", n); return 1; }
    if !event.expired(t0) { print("idle wait returned before its timeout\n"); return 1; }
    print("idle wait: 0 events, deadline expired\n");

    // Readable: the event carries fd, udata, and the pending byte count.
    msg := "ping";
    socket.write(b, msg.ptr, 4);
    n2, we2 := loop.wait(.{ ptr = @evs[0], len = 8 }, 1000);
    if we2 { print("wait failed\n"); return 1; }
    if n2 != 1 { print("after write: expected 1 event, got {}\n", n2); return 1; }
    if !evs[0].readable { print("event not readable\n"); return 1; }
    if evs[0].fd != a { print("event names the wrong fd\n"); return 1; }
    if evs[0].udata != 41 { print("udata did not round-trip\n"); return 1; }
    if evs[0].nbytes != 4 { print("expected 4 pending bytes, got {}\n", evs[0].nbytes); return 1; }
    print("readable: fd/udata/nbytes all correct\n");

    // Write interest on an empty send buffer reports writable at once.
    awe := false;
    loop.add_write(b, 42) catch { awe = true; };
    if awe { print("add_write failed\n"); return 1; }
    buf : [16]u8 = ---;
    socket.read(a, @buf[0], 16);   // drain so only the WRITE event fires
    n3, we3 := loop.wait(.{ ptr = @evs[0], len = 8 }, 1000);
    if we3 { print("wait failed\n"); return 1; }
    if n3 != 1 { print("write interest: expected 1 event, got {}\n", n3); return 1; }
    if !evs[0].writable { print("event not writable\n"); return 1; }
    if evs[0].udata != 42 { print("write udata did not round-trip\n"); return 1; }
    print("writable: immediate, udata correct\n");
    loop.del_write(b);

    // Peer close: readable with the eof flag set.
    socket.close(b);
    n4, we4 := loop.wait(.{ ptr = @evs[0], len = 8 }, 1000);
    if we4 { print("wait failed\n"); return 1; }
    if n4 != 1 { print("after close: expected 1 event, got {}\n", n4); return 1; }
    if !evs[0].eof { print("after close: eof flag not set\n"); return 1; }
    print("peer close: eof flagged\n");

    loop.del_read(a);
    socket.close(a);
    loop.close();
    print("event loop ok\n");
    return 0;
}
