#!perl
use Cassandane::Tiny;

sub test_shared_invite_as_secretary
    :VirtDomains :min_version_3_1 :NoAltNameSpace
{
    my ($self) = @_;

    my $admintalk = $self->{adminstore}->get_client();

    $admintalk->create("user.test");
    $admintalk->setacl("user.test", "test" => "lrswipkxtecda");

    my $service = $self->{instance}->get_service("http");
    my $testtalk = Net::CalDAVTalk->new(
        user => "test",
        password => 'pass',
        host => $service->host(),
        port => $service->port(),
        scheme => 'http',
        url => '/',
        expandurl => 1,
    );

    my $xml = <<EOF;
<?xml version="1.0" encoding="UTF-8"?>
<D:propertyupdate xmlns:D="DAV:" xmlns:C="urn:ietf:params:xml:ns:caldav">
  <D:set>
    <D:prop>
      <C:calendar-user-address-set>
        <D:href>mailto:test\@example.com</D:href>
      </C:calendar-user-address-set>
    </D:prop>
  </D:set>
</D:propertyupdate>
EOF

    $testtalk->Request('PROPPATCH', "/dav/principals/user/test", $xml,
                       'Content-Type' => 'text/xml');

    xlog $self, "create calendar";
    my $CalendarId = $testtalk->NewCalendar({name => 'Team Calendar'});
    $self->assert_not_null($CalendarId);

    xlog $self, "set calendar-user-address-set for all sharees";
    $testtalk->Request('PROPPATCH', "/dav/calendars/user/test/$CalendarId", $xml,
                       'Content-Type' => 'text/xml');

    xlog $self, "share to user";
    $admintalk->setacl("user.test.#calendars.$CalendarId",
                       "cassandane" => 'lrswipcdn');

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

    xlog $self, "subscribe to shared calendar";
    my $imapstore = $self->{instance}->get_service('imap')->create_store(
                        username => "cassandane");
    my $imaptalk = $imapstore->get_client();
    $imaptalk->subscribe("user.test.#calendars.$CalendarId");

    xlog $self, "get calendars as cassandane";
    my $CasCal = $CalDAV->GetCalendars();
    my $sharedCalendarId = $CasCal->[1]{href};

    $xml = <<EOF;
<?xml version="1.0" encoding="UTF-8"?>
<D:propertyupdate xmlns:D="DAV:" xmlns:C="urn:ietf:params:xml:ns:caldav">
  <D:set>
    <D:prop>
      <C:calendar-user-address-set>
        <D:href>mailto:test\@example.com</D:href>
      </C:calendar-user-address-set>
    </D:prop>
  </D:set>
</D:propertyupdate>
EOF

    my $uuid = "6de280c9-edff-4019-8ebd-cfebc73f8201";
    my $href = "$sharedCalendarId/$uuid.ics";
    my $card = <<EOF;
BEGIN:VCALENDAR
VERSION:2.0
PRODID:-//Apple Inc.//Mac OS X 10.10.4//EN
BEGIN:VEVENT
CREATED:20150806T234327Z
UID:$uuid
DTEND;TZID=Australia/Melbourne:20160831T183000
TRANSP:OPAQUE
SUMMARY:An Event
DTSTART;TZID=Australia/Melbourne:20160831T153000
DTSTAMP:20150806T234327Z
SEQUENCE:0
ATTENDEE;CN=Test User;PARTSTAT=ACCEPTED;RSVP=TRUE:MAILTO:friend\@example.com
ATTENDEE;PARTSTAT=ACCEPTED:MAILTO:test\@example.com
ORGANIZER;CN=Test User:MAILTO:friend\@example.com
END:VEVENT
END:VCALENDAR
EOF

    $CalDAV->Request('PUT', $href, $card, 'Content-Type' => 'text/calendar');

    $self->assert_caldav_notified(
        { recipient => "friend\@example.com", is_update => JSON::false, method => 'REPLY' },
    );
}
