<?php

namespace PeytzMail\Tests;

use PeytzMail\Entity\Feed;
use PeytzMail\Entity\Mailinglist;
use PeytzMail\Entity\Newsletter;
use PeytzMail\PeytzMail;

class NewsletterTest extends TestCase
{
    public function testGetNewsletter()
    {
        $json = <<<JSON
{
  "newsletter": {
    "id": "weekly-newsletter-weeknumber-20",
    "title": "Weekly newsletter weeknumber 20",
    "token": "nwmnxk",
    "subject": "SUBJECT",
    "from_email": "info@storebaelt.dk",
    "from_name": "Storebælt",
    "reply_to": null,
    "template": "storebaelt",
    "theme_id": "bekraeftelsesmail",
    "layout_id": "bromelding-da",
    "domain_key": "nov09-domainkey-storebaelt-dkim-peytz-dk",
    "state": "archived",
    "is_delayed_sending": false,
    "delayed_sending_at": null,
    "skip_duplicate_check": false,
    "sent_at": "2014-12-18T10:54:01+01:00",
    "created_at": "2014-12-18T10:53:43+01:00",
    "sent": 1,
    "skipped": 0,
    "failed": 0,
    "complaint": 0,
    "bounced": 0,
    "opened": 1,
    "clicks": 0,
    "clickers": 0,
    "links": [
      {
        "title": "Storebælt",
        "url": "http://www.storebaelt.dk/oplevdanmark",
        "tags": {},
        "clicks": 0,
        "clickers": 0
      },
      {
        "title": "Arena Fyn Apassionata - Den Gyldne Hemmelighed",
        "url": "http://www.storebaelt.dk/node/126160",
        "tags": {},
        "clicks": 0,
        "clickers": 0
      }
    ]
  }
}
JSON;
        $peytzMail = new PeytzMail($this->getMockClientReturns($json));

        $mailinglist = new Mailinglist;
        $mailinglist->id = 'foo-bar-mailinglist';

        $newsletter = $peytzMail->getNewsletter($mailinglist, 'weekly-newsletter-weeknumber-20');
        $this->assertInstanceOf('PeytzMail\Entity\Newsletter', $newsletter);
        $this->assertSame('weekly-newsletter-weeknumber-20', $newsletter->id);
        $this->assertSame('Weekly newsletter weeknumber 20', $newsletter->title);
        $this->assertSame("nwmnxk", $newsletter->token);
        $this->assertSame("SUBJECT", $newsletter->subject);
        $this->assertSame("info@storebaelt.dk", $newsletter->fromEmail);
        $this->assertSame("Storebælt", $newsletter->fromName);
        $this->assertNull($newsletter->replyTo);
        $this->assertSame("storebaelt", $newsletter->template);
        $this->assertSame("bekraeftelsesmail", $newsletter->themeId);
        $this->assertSame("bromelding-da", $newsletter->layoutId);
        $this->assertSame("nov09-domainkey-storebaelt-dkim-peytz-dk", $newsletter->domainKey);
        $this->assertSame("archived", $newsletter->state);
        $this->assertFalse($newsletter->isDelayedSending);
        $this->assertNull($newsletter->delayedSendingAt);
        $this->assertFalse($newsletter->skipDuplicateCheck);
        $this->assertSame('2014-12-18T10:54:01+01:00', $newsletter->sentAt);
        $this->assertSame("2014-12-18T10:53:43+01:00", $newsletter->createdAt);
        $this->assertSame(1, $newsletter->sent);
        $this->assertSame(0, $newsletter->skipped);
        $this->assertSame(0, $newsletter->failed);
        $this->assertSame(0, $newsletter->complaint);
        $this->assertSame(0, $newsletter->bounced);
        $this->assertSame(1, $newsletter->opened);
        $this->assertSame(0, $newsletter->clicks);
        $this->assertSame(0, $newsletter->clickers);
        $this->assertCount(2, $newsletter->links);
    }

    public function testCreateAndSendNewsletter()
    {
        $mailinglist = new Mailinglist;
        $mailinglist->id = 'foo-bar-mailinglist';

        $newsletter = new Newsletter();
        $newsletter->title     = 'title';
        $newsletter->preheader = '';
        $newsletter->layout_id = 'bromelding-da';
        $newsletter->subject   = 'subject';

        $feed = new Feed('bromelding');
        $feed->setData(['key' => 'value']);
        $newsletter->setFeeds([$feed]);

        $json = <<<JSON
{
  "newsletter": {
    "id": "weekly-newsletter-weeknumber-20",
    "title": "Weekly newsletter weeknumber 20",
    "token": "nwmnxk",
    "subject": "SUBJECT",
    "from_email": "info@storebaelt.dk",
    "from_name": "Storebælt",
    "reply_to": null,
    "template": "storebaelt",
    "theme_id": "bekraeftelsesmail",
    "layout_id": "bromelding-da",
    "domain_key": "nov09-domainkey-storebaelt-dkim-peytz-dk",
    "state": "draft",
    "is_delayed_sending": false,
    "delayed_sending_at": null,
    "skip_duplicate_check": false,
    "sent_at": null,
    "created_at": "2014-12-18T10:53:43+01:00",
    "sent": 0,
    "skipped": 0,
    "failed": 0,
    "complaint": 0,
    "bounced": 0,
    "opened": 0,
    "clicks": 0,
    "clickers": 0,
    "links": []
  }
}
JSON;

        $peytzMail = new PeytzMail($this->getMockClientReturns($json));
        $this->assertTrue($peytzMail->createAndSendNewsletter($newsletter, $mailinglist));

        $this->assertInstanceOf('PeytzMail\Entity\Newsletter', $newsletter);
        $this->assertSame('draft', $newsletter->state);
        $this->assertNull($newsletter->sentAt);
        $this->assertSame("2014-12-18T10:53:43+01:00", $newsletter->createdAt);
        $this->assertSame(0, $newsletter->sent);
        $this->assertSame(0, $newsletter->opened);
        $this->assertSame([['name' => 'bromelding', 'data' => ['key' => 'value']]], $newsletter->feeds);
        $this->assertEmpty($newsletter->links);
    }
}
