#!perl
use Cassandane::Tiny;

sub test_contact_copy_state
{
    my ($self) = @_;
    my $jmap = $self->{jmap};
    my $carddav = $self->{carddav};
    my $admintalk = $self->{adminstore}->get_client();
    my $service = $self->{instance}->get_service("http");

    xlog $self, "create shared account";
    $admintalk->create("user.other");

    my $othercarddav = Net::CardDAVTalk->new(
        user => "other",
        password => 'pass',
        host => $service->host(),
        port => $service->port(),
        scheme => 'http',
        url => '/',
        expandurl => 1,
    );

    xlog $self, "share addressbook";
    $admintalk->setacl("user.other.#addressbooks.Default",
                       "cassandane" => 'lrswipkxtecdn') or die;

    my $card =  {
        "addressbookId" => "Default",
        "firstName"=> "foo",
        "lastName"=> "bar",
    };

    xlog $self, "create card";
    my $res = $jmap->CallMethods([
        ['Contact/set', {
            create => {"1" => $card}
         }, "R1"],
        ['Contact/get', {
            accountId => 'other',
            ids => ['foo'],  # Just fetching current state for 'other'
        }, 'R2'],
    ]);
    $self->assert_not_null($res->[0][1]{created});
    my $cardId = $res->[0][1]{created}{"1"}{id};
    my $fromState = $res->[0][1]->{newState};
    $self->assert_not_null($fromState);
    my $state = $res->[1][1]->{state};
    $self->assert_not_null($state);

    xlog $self, "move card";
    $res = $jmap->CallMethods([
        ['Contact/copy', {
            fromAccountId => 'cassandane',
            accountId => 'other',
            ifFromInState => $fromState,
            ifInState => $state,
            create => {
                1 => {
                    id => $cardId,
                    addressbookId => "Default",
                },
            },
            onSuccessDestroyOriginal => JSON::true,
            destroyFromIfInState => $fromState,
         }, "R1"],
        ['Contact/get', {
            accountId => 'other',
            ids => ['#1'],
            properties => ['firstName'],
        }, 'R2'],
    ]);
    $self->assert_not_null($res->[0][1]{created});
    my $oldState = $res->[0][1]->{oldState};
    $self->assert_str_equals($oldState, $state);
    my $newState = $res->[0][1]->{newState};
    $self->assert_not_null($newState);
    $self->assert_str_equals('Contact/set', $res->[1][0]);
    $self->assert_str_equals($cardId, $res->[1][1]{destroyed}[0]);
    $self->assert_str_equals('foo', $res->[2][1]{list}[0]{firstName});

    # Is the blobId downloadable?
    my $blob = $jmap->Download({ accept => 'text/vcard' },
                               'other',
                               $res->[0][1]{created}{"1"}{blobId});
    $self->assert_str_equals('text/vcard; version=3.0',
                             $blob->{headers}->{'content-type'});
    $self->assert_num_not_equals(0, $blob->{headers}->{'content-length'});
    $self->assert_matches(qr/\r\nFN:foo bar\r\n/, $blob->{content});
}
