#!perl
use Cassandane::Tiny;

# Create a CalendarEvent, then reply to it via CalendarEvent/participantReply.
#
# The jsevent and patch in an iMIP mboxevent are JSCalendar 2.0, whatever the
# request asked for. Verify that the participant identifiers in the patch are
# the ones the client chose, and not something the converter invented.

sub get_imip_notification
{
    my ($self) = @_;
    my $data = $self->{instance}->getnotify();
    my ($imip) = grep { $_->{METHOD} eq 'imip' } @$data;
    $self->assert_not_null($imip);
    return decode_json($imip->{MESSAGE});
}

sub test_calendarevent_participantreply_patch ($self)
{
    my $jmap = $self->default_user->jmap;
    my $default_cal_id = $self->default_user->calendars->default->id;

    my $event = {
        version => "2.0",
        title => "test",
        freeBusyStatus => "busy",
        showWithoutTime => JSON::false,
        start => "2022-11-23T16:45:00",
        timeZone => "Australia/Melbourne",
        duration => "PT1H",
        organizerCalendarAddress => 'mailto:cassandane@example.com',
        participants => {
            org => {
                name => "Cassandane",
                roles => {
                    'owner' => JSON::true,
                },
                calendarAddress => 'mailto:cassandane@example.com',
            },
            att1 => {
                name => "Bugs Bunny",
                roles => {
                    'attendee' => JSON::true,
                },
                calendarAddress => 'mailto:bugs%2Bcarrot@looneytunes.com',
            },
        },
    };

    xlog $self, "Create event";

    # Clean notifications
    $self->{instance}->getnotify();

    my $res = $jmap->request([
        ['CalendarEvent/set', { create => {
            1 => {
                %$event, calendarIds => { $default_cal_id => JSON::true }
            },
        }}],
    ]);
    my $event_id = "" . $res->single_sentence->as_set->created_id('1');
    $self->assert_not_null($event_id);

    xlog $self, "Assert CalendarEvent/set imip patch keeps our participant ids";
    my $payload = $self->get_imip_notification();
    $self->assert_str_equals("REQUEST", $payload->{method});
    $self->assert_cmp_deeply([ 'att1', 'org' ],
        [ sort keys %{$payload->{patch}{participants}} ]);

    xlog $self, "Call /participantReply";

    # Clean notifications
    $self->{instance}->getnotify();

    $res = $jmap->request([
        ['CalendarEvent/participantReply', {
            eventId => $event_id,
            participantEmail => 'bugs%2Bcarrot@looneytunes.com',
            updates => { participationStatus => "accepted" }
        }],
    ]);

    xlog $self, "Assert participantReply imip patch";
    $payload = $self->get_imip_notification();
    $self->assert_str_equals("REQUEST", $payload->{method});
    # JSCalendar 2.0 has no scheduleAgent, scheduleForceSend or scheduleStatus
    # on a Participant, so the SCHEDULE-FORCE-SEND and SCHEDULE-STATUS
    # parameters that 1.0 surfaced here don't appear in the patch at all.
    $self->assert_cmp_deeply({
        'participants/att1/participationStatus' => 'accepted',
    }, $payload->{patch});
}
