#!perl
use Cassandane::Tiny;

sub test_calendarevent_set_created
    :needs_component_httpd :needs_component_jmap :min_version_3_7
{
    my ($self) = @_;
    my $jmap = $self->{jmap};

    my $t = DateTime->now();
    $t->set_time_zone('Etc/UTC');
    my $start = $t->strftime('%Y-%m-%dT%H:%M:%S');
    $t->add(DateTime::Duration->new(days => -2));
    my $past = $t->strftime('%Y-%m-%dT%H:%M:%SZ');
    $t->add(DateTime::Duration->new(days => 4));
    my $future = $t->strftime('%Y-%m-%dT%H:%M:%SZ');

    my $res = $jmap->CallMethods([
        ['CalendarEvent/set', {
            create => {
                eventNoCreated => {
                    calendarIds => {
                        'Default' => JSON::true,
                    },
                    start => $start,
                    timeZone => 'Etc/UTC',
                    duration => 'PT1H',
                    title => 'eventNoCreated',
                },
                eventCreatedInPast => {
                    calendarIds => {
                        'Default' => JSON::true,
                    },
                    start => $start,
                    timeZone => 'Etc/UTC',
                    duration => 'PT1H',
                    title => 'eventCreatedInPast',
                    created => $past,
                },
                eventCreatedInFuture => {
                    calendarIds => {
                        'Default' => JSON::true,
                    },
                    start => $start,
                    timeZone => 'Etc/UTC',
                    duration => 'PT1H',
                    title => 'eventCreatedInPast',
                    created => $future,
                },
            },
        }, 'R1'],
        ['CalendarEvent/get', {
            ids => [ '#eventNoCreated' ],
            properties => ['created', 'title'],
        }, 'R2'],
        ['CalendarEvent/get', {
            ids => [ '#eventCreatedInPast' ],
            properties => ['created', 'title'],
        }, 'R3'],
        ['CalendarEvent/get', {
            ids => [ '#eventCreatedInFuture' ],
            properties => ['created', 'title'],
        }, 'R4'],
    ]);

    xlog "Event with no created property get set to now";
    my $created = $res->[1][1]{list}[0]{created};
    $self->assert(($past lt $created) and ($created lt $future));
    $self->assert_str_equals($created,
        $res->[0][1]{created}{eventNoCreated}{created});
    my $eventNoCreatedId = $res->[1][1]{list}[0]{id};

    xlog "Event with past created preserves value";
    $created = $res->[2][1]{list}[0]{created};
    $self->assert_str_equals($past, $created);
    $self->assert_null($res->[0][1]{created}{eventCreatedInPast}{created});

    xlog "Event with future created gets clamped to now";
    $created = $res->[3][1]{list}[0]{created};
    $self->assert(($past lt $created) and ($created lt $future));
    $self->assert_str_equals($created,
        $res->[0][1]{created}{eventCreatedInFuture}{created});

    xlog "Can update created value";
    $res = $jmap->CallMethods([
        ['CalendarEvent/set', {
            update => {
                $eventNoCreatedId => {
                    created => $past,
                },
            },
        }, 'R1'],
        ['CalendarEvent/get', {
            ids => [ $eventNoCreatedId ],
            properties => ['created'],
        }, 'R2'],
    ]);
    $self->assert(exists $res->[0][1]{updated}{$eventNoCreatedId});
    $self->assert_str_equals($past, $res->[1][1]{list}[0]{created});
}
