<?php

use App\Services\Monitoring\MonitoringFormatter;

test('bytes returns B for sub-KB values', function () {
    expect(MonitoringFormatter::bytes(0))->toBe('0 B');
    expect(MonitoringFormatter::bytes(500))->toBe('500 B');
    expect(MonitoringFormatter::bytes(1023))->toBe('1023 B');
});

test('bytes converts to KB, MB, GB, TB', function () {
    expect(MonitoringFormatter::bytes(1024))->toBe('1 KB');
    expect(MonitoringFormatter::bytes(1024 * 1024))->toBe('1 MB');
    expect(MonitoringFormatter::bytes(1024 * 1024 * 1024))->toBe('1 GB');
    expect(MonitoringFormatter::bytes(1024 ** 4))->toBe('1 TB');
});

test('bytes caps at TB for very large values', function () {
    expect(MonitoringFormatter::bytes(1024 ** 5))->toBe('1024 TB');
});

test('bytes respects precision parameter', function () {
    expect(MonitoringFormatter::bytes(1536, 0))->toBe('2 KB');
    expect(MonitoringFormatter::bytes(1536, 1))->toBe('1.5 KB');
    expect(MonitoringFormatter::bytes(1536, 3))->toBe('1.5 KB');
});

test('bytes clamps negative input to zero', function () {
    expect(MonitoringFormatter::bytes(-100))->toBe('0 B');
});

test('duration shows < 1m for sub-minute values', function () {
    expect(MonitoringFormatter::duration(0))->toBe('< 1m');
    expect(MonitoringFormatter::duration(59))->toBe('< 1m');
});

test('duration formats minutes only', function () {
    expect(MonitoringFormatter::duration(60))->toBe('1m');
    expect(MonitoringFormatter::duration(60 * 59))->toBe('59m');
});

test('duration formats hours and minutes', function () {
    expect(MonitoringFormatter::duration(3600))->toBe('1h');
    expect(MonitoringFormatter::duration(3600 + 60 * 30))->toBe('1h 30m');
});

test('duration formats days, hours, minutes', function () {
    expect(MonitoringFormatter::duration(86400))->toBe('1d');
    expect(MonitoringFormatter::duration(86400 + 3600 * 5 + 60 * 30))->toBe('1d 5h 30m');
});

test('duration skips zero components', function () {
    expect(MonitoringFormatter::duration(86400 + 60))->toBe('1d 1m');
    expect(MonitoringFormatter::duration(3600 * 2))->toBe('2h');
});

test('parseBytes round-trips whole units', function () {
    expect(MonitoringFormatter::parseBytes('1 KB'))->toBe(1024.0);
    expect(MonitoringFormatter::parseBytes('1 MB'))->toBe(1048576.0);
    expect(MonitoringFormatter::parseBytes('2 GB'))->toBe(2.0 * 1024 * 1024 * 1024);
    expect(MonitoringFormatter::parseBytes('512 B'))->toBe(512.0);
});

test('parseBytes handles plain numeric strings', function () {
    expect(MonitoringFormatter::parseBytes('4096'))->toBe(4096.0);
});

test('parseBytes ignores unknown units', function () {
    expect(MonitoringFormatter::parseBytes('10 XB'))->toBe(10.0);
});

test('parseBytes handles fractional values', function () {
    expect(MonitoringFormatter::parseBytes('1.5 KB'))->toBe(1536.0);
});
