#!perl
use Cassandane::Tiny;

sub test_calendarevent_set_hideattendees_itip
    :needs_component_jmap :min_version_0_0 :max_version_0_0
{
    my ($self) = @_;

    my $jmap = $self->{jmap};
    my $caldav = $self->{caldav};

    # clean notification cache
    $self->{instance}->getnotify();

    my $res = $jmap->CallMethods([
        ['CalendarEvent/set', {
            create => {
                event1 => {
                    calendarIds => {
                        Default => JSON::true,
                    },
                    uid => 'event1uidlocal',
                    title => "event1",
                    start => "2020-01-01T09:00:00",
                    timeZone => "Europe/Vienna",
                    duration => "PT1H",
                    replyTo => {
                        imip => 'mailto:cassandane@example.com',
                    },
                    participants => {
                        cassandane => {
                            roles => {
                                'owner' => JSON::true,
                                'attendee' => JSON::true,
                            },
                            sendTo => {
                                imip => 'mailto:cassandane@example.com',
                            },
                        },
                        attendee1 => {
                            roles => {
                                'attendee' => JSON::true,
                            },
                            sendTo => {
                                imip => 'mailto:attendee1@example.com',
                            },
                        },
                        attendee2 => {
                            roles => {
                                'attendee' => JSON::true,
                            },
                            sendTo => {
                                imip => 'mailto:attendee2@example.com',
                            },
                        },
                    },
                    hideAttendees => JSON::true,
                },
            },
        }, 'R1'],
    ]);
    my $eventId = $res->[0][1]{created}{event1}{id};
    $self->assert_not_null($eventId);

    my $data = $self->{instance}->getnotify();

    my $imip = {};
    foreach my $notif (@$data) {
        if (not $notif->{METHOD} eq 'imip') {
            next;
        }
        my $msg = decode_json($notif->{MESSAGE});
        $imip->{$msg->{recipient}} = $msg;
    }

    $self->assert_num_equals(2, scalar keys %{$imip});

    $self->assert(not $imip->{'attendee1@example.com'}->{ical} =~
        m/attendee2\@example.com/);
    $self->assert($imip->{'attendee1@example.com'}->{ical} =~
        m/attendee1\@example.com/);

    $self->assert(not $imip->{'attendee2@example.com'}->{ical} =~
        m/attendee1\@example.com/);
    $self->assert($imip->{'attendee2@example.com'}->{ical} =~
        m/attendee2\@example.com/);
}
