<?php

namespace Tests\Feature;

use App\Exports\EbayMotorsExport;
use App\Facades\Settings;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Mtc\MercuryDataModels\Vehicle;
use Tests\TestCase;
use Tests\UserForTenant;

class EbayMotorsExportTest extends TestCase
{
    use RefreshDatabase;
    use UserForTenant;

    protected $tenancy = true;

    public function testData()
    {
        Settings::make([
            'tab' => 'foo-baz',
            'section' => 'lorem',
            'group' => 'ipsum',
            'name' => 'ebay enabled',
            'config_key' => 'sales-channels-ebay-motors-enabled',
            'value' => true,
            'type' => 'boolean',
        ]);

        Vehicle::factory()->create([
            'is_published' => true,
            'description' => 'foo',
            'type' => 'car',
        ]);

        $vehicles = (new EbayMotorsExport())->collection();

        $this->assertCount(1, $vehicles);

        $vehicle = $vehicles->first();

        $this->assertEquals('foo', $vehicle['Description']);
        $this->assertEquals(0, $vehicle['PricePlusVat']);
    }

    public function testSanitisedDescription()
    {
        Settings::make([
            'tab' => 'foo-baz',
            'section' => 'lorem',
            'group' => 'ipsum',
            'name' => 'ebay enabled',
            'config_key' => 'sales-channels-ebay-motors-enabled',
            'value' => true,
            'type' => 'boolean',
        ]);

        // set expected string, testing full range of allowable characters
        $expected_string = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789.,?!/()\\[]"\'-_+=@£€$%^&*';

        // add characters that we want to exclude from feed
        $description = '<p>â ðŸ“§ ðŸ“± ðŸŽ¶</p>' . $expected_string;

        Vehicle::factory()->create([
            'is_published' => true,
            'description' => $description,
            'type' => 'car',
        ]);

        $vehicles = (new EbayMotorsExport())->collection();

        $this->assertCount(1, $vehicles);

        $vehicle = $vehicles->first();

        // assert that we have the vehicle with the expected description (including additional characters) in the db
        $this->assertCount(1, Vehicle::query()->where('description', $description)->get());

        // assert that the feed contains the sanitised description
        $this->assertEquals($expected_string, $vehicle['Description']);
    }
}
