<?php

declare(strict_types=1);

/*
 * This file is part of the VITICORE2 package.
 *
 * (c) CP Creation <web@cpcreation.fr>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace CpCreation\VitiCore\Tests\Seo;

use CpCreation\VitiCore\Contact\Model\Contact;
use CpCreation\VitiCore\Seo\LocalBusinessSchemaBuilder;
use PHPUnit\Framework\TestCase;

final class LocalBusinessSchemaBuilderTest extends TestCase
{
    public function testBuildsAWinerySchemaFromContactConfiguration(): void
    {
        $contact = $this->createContact();
        $contact->setFacebook('https://www.facebook.com/maison-exemple');
        $contact->setInstagram('https://www.instagram.com/maison-exemple');
        $contact->setLatitude('49.0434000');
        $contact->setLongitude('3.9560000');
        $contact->setOpeningHours([
            [
                'dayOfWeek' => ['Monday', 'Tuesday', 'NotADay'],
                'opens' => '09:00',
                'closes' => '18:00',
            ],
            [
                'dayOfWeek' => ['Sunday'],
                'opens' => 'invalide',
                'closes' => '18:00',
            ],
        ]);

        $schema = new LocalBusinessSchemaBuilder()->build(
            $contact,
            'https://example.com',
            'https://example.com/upload/image/logo.svg',
        );

        self::assertNotNull($schema);
        self::assertSame('https://schema.org', $schema['@context']);
        self::assertSame('Winery', $schema['@type']);
        self::assertSame('https://example.com/#winery', $schema['@id']);
        self::assertSame('Maison Exemple', $schema['name']);
        self::assertSame('+33 3 26 00 00 00', $schema['telephone']);
        self::assertSame([
            '@type' => 'PostalAddress',
            'streetAddress' => '1 rue des Vignes',
            'postalCode' => '51200',
            'addressLocality' => 'Épernay',
            'addressCountry' => 'FR',
        ], $schema['address']);
        self::assertSame([
            '@type' => 'GeoCoordinates',
            'latitude' => 49.0434,
            'longitude' => 3.956,
        ], $schema['geo']);
        self::assertSame('https://example.com/upload/image/logo.svg', $schema['logo']);
        self::assertSame($schema['logo'], $schema['image']);
        self::assertSame([
            'https://www.facebook.com/maison-exemple',
            'https://www.instagram.com/maison-exemple',
        ], $schema['sameAs']);
        self::assertSame([[
            '@type' => 'OpeningHoursSpecification',
            'dayOfWeek' => ['Monday', 'Tuesday'],
            'opens' => '09:00',
            'closes' => '18:00',
        ]], $schema['openingHoursSpecification']);
    }

    public function testReturnsNullWithoutACompanyName(): void
    {
        $contact = $this->createContact();
        $contact->setCompany('   ');

        self::assertNull(new LocalBusinessSchemaBuilder()->build($contact, 'https://example.com'));
    }

    private function createContact(): Contact
    {
        $contact = new Contact();
        $contact->setCompany('Maison Exemple');
        $contact->setAddress('1 rue des Vignes');
        $contact->setPostal('51200');
        $contact->setCity('Épernay');
        $contact->setCountry('FR');
        $contact->setPhone1('+33 3 26 00 00 00');

        return $contact;
    }
}
