#!perl
use Cassandane::Tiny;

sub test_calendarevent_organizer_noattendees
    :min_version_3_5 :needs_component_jmap
{
    my ($self) = @_;
    my $jmap = $self->{jmap};
    my $caldav = $self->{caldav};

    xlog "Create event via CalDAV";
    my ($event1Id, $ical) = $self->icalfile('organizer_noattendees');
    my $event = $self->putandget_vevent($event1Id, $ical);
    my $wantParticipants = {
        'bf8360ce374961f497599431c4bacb50d4a67ca1' => {
            '@type' => 'Participant',
            name => 'Organizer',
            roles => {
                'owner' => JSON::true,
            },
            sendTo => {
                imip => 'mailto:organizer@local',
            },
            expectReply => JSON::false,
            participationStatus => 'needs-action',
        },
    };
    my $wantReplyTo = {
        imip => 'mailto:organizer@local',
    },
    $self->assert_deep_equals($wantParticipants, $event->{participants});
    $self->assert_deep_equals($wantReplyTo, $event->{replyTo});

    xlog "Update event via JMAP";
    my $res = $jmap->CallMethods([
        ['CalendarEvent/set', {
            update => {
                $event1Id => {
                    participants => $wantParticipants,
                    replyTo => $wantReplyTo,
                },
            },
        }, 'R1'],
        ['CalendarEvent/get', {
            ids => [$event1Id],
            properties => ['participants', 'replyTo', 'x-href'],
        }, 'R2'],
    ]);
    $self->assert(exists $res->[0][1]{updated}{$event1Id});
    $self->assert_deep_equals($wantParticipants, $res->[1][1]{list}[0]{participants});
    $self->assert_deep_equals($wantReplyTo, $res->[1][1]{list}[0]{replyTo});

    my $xhref1 = $res->[1][1]{list}[0]{'x-href'};
    $self->assert_not_null($xhref1);

    xlog "Validate no ATTENDEE got added";
    $res = $caldav->Request('GET', $xhref1);
    $self->assert($res->{content} =~ m/ORGANIZER/);
    $self->assert(not($res->{content} =~ m/ATTENDEE/));

    xlog "Create event with owner-only participant via JMAP";
    $res = $jmap->CallMethods([
        ['CalendarEvent/set', {
            create => {
                event2 => {
                    calendarIds => {
                        'Default' => JSON::true,
                    },
                    title => "title",
                    "start"=> "2015-11-07T09:00:00",
                    "duration"=> "PT2H",
                    "timeZone" => "Europe/London",
                    replyTo => $wantReplyTo,
                    participants => $wantParticipants,
                },
            },
        }, 'R1'],
        ['CalendarEvent/get', {
            ids => ['#event2'],
            properties => ['participants', 'replyTo'],
        }, 'R2'],
    ]);
    $self->assert_deep_equals($wantParticipants, $res->[1][1]{list}[0]{participants});
    $self->assert_deep_equals($wantReplyTo, $res->[1][1]{list}[0]{replyTo});

    my $xhref2 = $res->[0][1]{created}{event2}{'x-href'};
    $self->assert_not_null($xhref2);

    xlog "Validate an ATTENDEE got added";
    $res = $caldav->Request('GET', $xhref2);
    $self->assert($res->{content} =~ m/ORGANIZER/);
    $self->assert($res->{content} =~ m/ATTENDEE/);
}
