#!perl
use Cassandane::Tiny;

sub test_expunge_messages
{
    my ($self) = @_;

    xlog $self, "testing expunge of messages with message scope";
    xlog $self, "annotations [IRIS-1553]";

    my $entry = '/comment';
    my $attrib = 'value.priv';

    $self->{store}->set_fetch_attributes('uid', "annotation ($entry $attrib)");
    my $talk = $self->{store}->get_client();
    $talk->uid(1);

    my @data_by_uid = (
        undef,
        # data thanks to hipsteripsum.me
        "polaroid seitan",
        "bicycle rights",
        "bushwick gastropub"
    );

    xlog $self, "Append some messages and store annotations";
    my %exp;
    my $uid = 1;
    while (defined $data_by_uid[$uid])
    {
        my $data = $data_by_uid[$uid];
        my $msg = $self->make_message("Message $uid");
        $msg->set_annotation($entry, $attrib, $data);
        $exp{$uid} = $msg;
        $self->set_msg_annotation(undef, $uid, $entry, $attrib, $data);
        $uid++;
    }

    xlog $self, "Check the annotations are there";
    $self->check_messages(\%exp, keyed_on => 'uid');

    xlog $self, "Check the annotations are in the DB too";
    my $r = $self->list_annotations(scope => 'message');
    $self->assert_deep_equals([
        {
            mboxname => 'user.cassandane',
            uid => 1,
            entry => $entry,
            userid => 'cassandane',
            data => $data_by_uid[1]
        },
        {
            mboxname => 'user.cassandane',
            uid => 2,
            entry => $entry,
            userid => 'cassandane',
            data => $data_by_uid[2]
        },
        {
            mboxname => 'user.cassandane',
            uid => 3,
            entry => $entry,
            userid => 'cassandane',
            data => $data_by_uid[3]
        }
    ], $r);

    $uid = 1;
    while (defined $data_by_uid[$uid])
    {
        xlog $self, "Delete message $uid";
        $talk->store($uid, '+flags', '(\\Deleted)');
        $talk->expunge();

        xlog $self, "Check the annotation is gone";
        delete $exp{$uid};
        $self->check_messages(\%exp);
        $uid++;
    }

    xlog $self, "Check the annotations are still in the DB";
    $r = $self->list_annotations(scope => 'message');
    $self->assert_deep_equals([
        {
            mboxname => 'user.cassandane',
            uid => 1,
            entry => $entry,
            userid => 'cassandane',
            data => $data_by_uid[1]
        },
        {
            mboxname => 'user.cassandane',
            uid => 2,
            entry => $entry,
            userid => 'cassandane',
            data => $data_by_uid[2]
        },
        {
            mboxname => 'user.cassandane',
            uid => 3,
            entry => $entry,
            userid => 'cassandane',
            data => $data_by_uid[3]
        }
    ], $r);

    $self->run_delayed_expunge();

    xlog $self, "Check the annotations are gone from the DB";
    $r = $self->list_annotations(scope => 'message');
    $self->assert_deep_equals([], $r);
}
