#!perl
use Cassandane::Tiny;

sub test_calendarprincipal_getavailability_merged
    :min_version_3_3 :needs_component_jmap
{
    my ($self) = @_;
    my $jmap = $self->{jmap};

    my $res = $jmap->CallMethods([
        ['CalendarEvent/set', {
            create => {
                # 09:00 to 10:30: Two events adjacent to each other.
                'event-0900-1000' => {
                    calendarIds => {
                        Default => JSON::true,
                    },
                    title => "event-0900-1000",
                    start => "2020-08-01T09:00:00",
                    timeZone => "Etc/UTC",
                    duration => "PT1H",
                },
                'event-1000-1030' => {
                    calendarIds => {
                        Default => JSON::true,
                    },
                    title => "event-1000-1030",
                    start => "2020-08-01T10:00:00",
                    timeZone => "Etc/UTC",
                    duration => "PT30M",
                },
                # 05:00 to 08:00: One event completely overlapping the other.
                'event-0500-0800' => {
                    calendarIds => {
                        Default => JSON::true,
                    },
                    title => "event-0500-0800",
                    start => "2020-08-01T05:00:00",
                    timeZone => "Etc/UTC",
                    duration => "PT3H",
                },
                'event-0600-0700' => {
                    calendarIds => {
                        Default => JSON::true,
                    },
                    title => "event-06:00-07:00",
                    start => "2020-08-01T06:00:00",
                    timeZone => "Etc/UTC",
                    duration => "PT1H",
                },
                # 01:00 to 03:00: One event partially overlapping the other.
                'event-0100-0200' => {
                    calendarIds => {
                        Default => JSON::true,
                    },
                    title => "event-0100-0200",
                    start => "2020-08-01T01:00:00",
                    timeZone => "Etc/UTC",
                    duration => "PT1H",
                },
                'event-0130-0300' => {
                    calendarIds => {
                        Default => JSON::true,
                    },
                    title => "event-0130-0300",
                    start => "2020-08-01T01:30:00",
                    timeZone => "Etc/UTC",
                    duration => "PT1H30M",
                },
                # 12:00 to 13:30: Overlapping events with differing busyStatus.
                'event-1200-1300-tentative' => {
                    calendarIds => {
                        Default => JSON::true,
                    },
                    title => "event-1200-1300-tentative",
                    start => "2020-08-01T12:00:00",
                    timeZone => "Etc/UTC",
                    duration => "PT1H",
                    status => 'tentative',
                },
                'event-1200-1330-confirmed' => {
                    calendarIds => {
                        Default => JSON::true,
                    },
                    title => "event-1200-1330-confirmed",
                    start => "2020-08-01T12:00:00",
                    timeZone => "Etc/UTC",
                    duration => "PT1H30M",
                    status => 'confirmed',
                },
                'event-1200-1230-unavailable' => {
                    calendarIds => {
                        Default => JSON::true,
                    },
                    title => "event-1200-1330-unavailable",
                    start => "2020-08-01T12:00:00",
                    timeZone => "Etc/UTC",
                    duration => "PT30M",
                },
            },
        }, 'R1'],
        ['Principal/getAvailability', {
            id => 'cassandane',
            utcStart => '2020-08-01T00:00:00Z',
            utcEnd => '2020-09-01T00:00:00Z',
        }, 'R2'],
    ]);
    $self->assert_num_equals(9, scalar keys %{$res->[0][1]{created}});

    $self->assert_deep_equals([{
        utcStart => "2020-08-01T01:00:00Z",
        utcEnd => "2020-08-01T03:00:00Z",
        busyStatus => 'unavailable',
        event => undef,
    }, {
        utcStart => "2020-08-01T05:00:00Z",
        utcEnd => "2020-08-01T08:00:00Z",
        busyStatus => 'unavailable',
        event => undef,
    }, {
        utcStart => "2020-08-01T09:00:00Z",
        utcEnd => "2020-08-01T10:30:00Z",
        busyStatus => 'unavailable',
        event => undef,
    }, {
        utcStart => "2020-08-01T12:00:00Z",
        utcEnd => "2020-08-01T13:30:00Z",
        busyStatus => 'confirmed',
        event => undef,
    }, {
        utcStart => "2020-08-01T12:00:00Z",
        utcEnd => "2020-08-01T12:30:00Z",
        busyStatus => 'unavailable',
        event => undef,
    }, {
        utcStart => "2020-08-01T12:00:00Z",
        utcEnd => "2020-08-01T13:00:00Z",
        busyStatus => 'tentative',
        event => undef,
    }], $res->[1][1]{list});
}
