<?php

namespace Tricks\CampaignMonitor\Test\Http;

use Illuminate\Foundation\Testing\WithFaker;
use Illuminate\Support\Facades\Route;
use Tricks\CampaignMonitor\Test\TestCase;

class CampaignMonitorControllerTest extends TestCase
{
    use WithFaker;

    /**
     * @test
     */
    public function formsErrorWhenRequiredFieldsAreMissing()
    {
        $this->getAllRouteNames()
            ->each(function ($routeName) {
                $response = $this->postJson(route($routeName));
                $this->assertTrue($response->isClientError());
            });
    }

    private function getAllRouteNames()
    {
        return collect(Route::getRoutes())
            ->map(function (\Illuminate\Routing\Route $route) {
                return $route->getName();
            })
            ->filter(function ($routeName) {
                return strstr($routeName, 'api.campaignmonitor.transactional');
            });
    }

    /**
     * @test
     */
    // TODO: not sure if this really tests Sanctum/auth at the moment
    public function formRoutesAreProtectedViaSanctum()
    {
        $this->getAllRouteNames()
            ->each(function ($routeName) {
                $response = $this->post(route($routeName));

                $response->assertStatus(302);
            });
    }

    /**
     * @test
     */
    public function can_send_dummy_form()
    {
        $response = $this
            ->post($this->getRoute('dummy'), [
                'test1' => 'foo',
                'test2' => 'bar',
            ]);

        // We're ok with the CM API responding with 401 here, as we just want to check if we reach the CM API
        // (and don't want to send real mails).
        $response->assertStatus(401);
        $response->assertJsonFragment([
            'campaignmonitor_error' => [
                "Code" => 50,
                "Message" => "Must supply a valid HTTP Basic Authorization header"
            ]
        ]);
    }
    
    /**
     * @test
     */
    public function can_send_dummy_form_with_dynamic_recipient()
    {
        $response = $this
            ->post($this->getRoute('dummy_with_dynamic_recipient'), [
                'recipient' => 'foo',
            ]);

        // We're ok with the CM API responding with 401 here, as we just want to check if we reach the CM API
        // (and don't want to send real mails).
        $response->assertStatus(401);
        $response->assertJsonFragment([
            'campaignmonitor_error' => [
                "Code" => 50,
                "Message" => "Must supply a valid HTTP Basic Authorization header"
            ]
        ]);
    }

    /**
     * @test
     */
    public function can_send_dummy_form_to_multiple_recipients()
    {
        $response = $this
            ->post($this->getRoute('dummy_with_multiple_recipients'), [
                'test1' => 'foo',
                'test2' => 'bar',
            ]);

        // We're ok with the CM API responding with 401 here, as we just want to check if we reach the CM API
        // (and don't want to send real mails).
        $response->assertStatus(401);
        $response->assertJsonFragment([
            'campaignmonitor_error' => [
                "Code" => 50,
                "Message" => "Must supply a valid HTTP Basic Authorization header"
            ]
        ]);
    }

    /**
     * @test
     */
    public function can_send_dummy_form_with_attachment()
    {
        $fileName = __DIR__ . '/./../files/dummy.pdf';
        $pdfAsBinary = fread(fopen($fileName, "r"), filesize($fileName));

        $attachments = [
            [
                'Name' => 'foo',
                'Type' => 'application/pdf',
                'Content' => base64_encode($pdfAsBinary)
            ]
        ];

        $response = $this
            ->post($this->getRoute('dummy_with_attachment'), [
                'test1' => 'foo',
                'test2' => 'bar',
                'attachments' => $attachments
            ]);

        // We're ok with the CM API responding with 401 here, as we just want to check if we reach the CM API
        // (and don't want to send real mails).
        $response->assertStatus(401);
        $response->assertJsonFragment([
            'campaignmonitor_error' => [
                "Code" => 50,
                "Message" => "Must supply a valid HTTP Basic Authorization header"
            ]
        ]);
    }

    private function getRoute($formType)
    {
        return route('api.campaignmonitor.transactional.' . $formType);
    }
}
