#!perl
use Cassandane::Tiny;

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

    xlog "Create standalone instances";
    my $res = $jmap->CallMethods([
        ['CalendarEvent/set', {
            create => {
                instance1 => {
                    calendarIds => {
                        'Default' => JSON::true,
                    },
                    '@type' => 'Event',
                    uid => 'event1uid',
                    title => 'instance1',
                    start => '2021-01-01T11:11:11',
                    timeZone => 'Europe/Berlin',
                    duration => 'PT1H',
                    recurrenceId => '2021-01-01T01:01:01',
                    recurrenceIdTimeZone => 'Europe/London',
                },
                instance2 => {
                    calendarIds => {
                        'Default' => JSON::true,
                    },
                    '@type' => 'Event',
                    uid => 'event1uid',
                    title => 'instance2',
                    start => '2021-02-02T02:02:02',
                    timeZone => 'Europe/Berlin',
                    duration => 'PT1H',
                    recurrenceId => '2021-02-02T02:02:02',
                    recurrenceIdTimeZone => 'Europe/London',
                },
            },
        }, 'R1'],
    ]);
    my $instance1Id = $res->[0][1]{created}{instance1}{id};
    $self->assert_not_null($instance1Id);
    my $instance2Id = $res->[0][1]{created}{instance2}{id};
    $self->assert_not_null($instance2Id);
    my $xhref1 = $res->[0][1]{created}{instance1}{'x-href'};
    $self->assert_not_null($xhref1);
    my $xhref2 = $res->[0][1]{created}{instance2}{'x-href'};
    $self->assert_not_null($xhref2);
    $self->assert_str_equals($xhref1, $xhref2);
    my $state = $res->[0][1]{newState};

    xlog "Destroy first standalone instance";
    $res = $jmap->CallMethods([
        ['CalendarEvent/set', {
            destroy => [ $instance1Id ],
        }, 'R1'],
        ['CalendarEvent/get', {
            ids => [$instance1Id],
            properties => ['title', 'recurrenceId', 'recurrenceIdTimeZone'],
        }, 'R2'],
        ['CalendarEvent/get', {
            ids => [$instance2Id],
            properties => ['title', 'recurrenceId', 'recurrenceIdTimeZone'],
        }, 'R3'],
        ['CalendarEvent/changes', {
            sinceState => $state,
        }, 'R4'],
    ]);
    $self->assert_deep_equals([$instance1Id], $res->[0][1]{destroyed});
    $self->assert_deep_equals([$instance1Id], $res->[1][1]{notFound});
    $self->assert_str_equals('instance2', $res->[2][1]{list}[0]{title});

    $self->assert_str_not_equals($state, $res->[0][1]{newState});
    $self->assert_str_not_equals($state, $res->[1][1]{state});
    $self->assert_str_not_equals($state, $res->[2][1]{state});
    $self->assert_str_not_equals($state, $res->[3][1]{newState});
    $self->assert_deep_equals([], $res->[3][1]{created});
    $self->assert_deep_equals([], $res->[3][1]{updated});
    $self->assert_deep_equals([$instance1Id], $res->[3][1]{destroyed});
    $state = $res->[3][1]{newState};

    xlog "Assert CalDAV resource still exists";
    $res = $caldav->Request('GET', $xhref1);
    $self->assert(not $res->{content} =~ m/SUMMARY:instance1/);
    $self->assert($res->{content} =~ m/SUMMARY:instance2/);

    xlog "Destroy second standalone instance";
    $res = $jmap->CallMethods([
        ['CalendarEvent/set', {
            destroy => [ $instance2Id ],
        }, 'R1'],
        ['CalendarEvent/get', {
            ids => [$instance2Id],
            properties => ['title', 'recurrenceId', 'recurrenceIdTimeZone'],
        }, 'R2'],
        ['CalendarEvent/changes', {
            sinceState => $state,
        }, 'R2'],
    ]);
    $self->assert_deep_equals([$instance2Id], $res->[0][1]{destroyed});
    $self->assert_deep_equals([$instance2Id], $res->[1][1]{notFound});

    $self->assert_str_not_equals($state, $res->[0][1]{newState});
    $self->assert_str_not_equals($state, $res->[1][1]{state});
    $self->assert_str_not_equals($state, $res->[2][1]{newState});
    $self->assert_deep_equals([], $res->[2][1]{created});
    $self->assert_deep_equals([], $res->[2][1]{updated});
    $self->assert_deep_equals([$instance2Id], $res->[2][1]{destroyed});
    $state = $res->[3][1]{newState};

    xlog "Assert CalDAV resource is gone";
    # Can't use CalDAV talk for GET on non-existent URLs
    my $xml = <<EOF;
<?xml version="1.0"?>
<a:propfind xmlns:a="DAV:">
 <a:prop><a:resourcetype/></a:prop>
</a:propfind>
EOF
    $res = $caldav->Request('PROPFIND', 'Default', $xml,
        'Content-Type' => 'application/xml',
        'Depth' => '1'
    );
    $self->assert_does_not_match(qr{event1uid}, $res);
}
