<?php

namespace Tests\Unit;

use App\Facades\Settings;
use App\SiteRepository;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Mtc\MercuryDataModels\Page;
use Mtc\MercuryDataModels\NewCar;
use Mtc\MercuryDataModels\Vehicle;
use Mtc\MercuryDataModels\VehicleOffer;
use Tests\TestCase;
use App\Facades\Site;

final class SiteRepositoryTest extends TestCase
{
    use DatabaseMigrations;

    protected $tenancy = true;

    protected $tenantDomain = 'https://test.localhost';

    public function testVehicleUrlWithAbsoluteAndAccess()
    {
        $url = Site::vehicleUrl(Vehicle::factory()->create(), true, true);

        $this->assertStringStartsWith($this->tenantDomain, $url);
        $this->assertStringContainsString('a=', $url);
    }

    public function testVehicleUrlWithoutAbsoluteAndWithAccess()
    {
        $vehicle = Vehicle::factory()->create();

        $url = Site::vehicleUrl($vehicle, true, false);

        $this->assertStringNotContainsString($this->tenantDomain, $url);
        $this->assertStringContainsString($vehicle->urlPath(), $url);
        $this->assertStringContainsString('a=', $url);
    }

    public function testVehicleUrlWithoutAbsoluteAndWithoutAccess()
    {
        $vehicle = Vehicle::factory()->create();

        $url = Site::vehicleUrl($vehicle, false, false);

        $this->assertStringNotContainsString($this->tenantDomain, $url);
        $this->assertStringContainsString($vehicle->urlPath(), $url);
    }

    public function testPageUrlForHomePage()
    {
        $url = Site::pageUrl(Page::factory()->create(['slug' => '']), false);

        $this->assertEquals($this->tenantDomain . '/', $url);
    }

    public function testPageUrlForOtherPages()
    {
        $url = Site::pageUrl(Page::factory()->create(['slug' => 'about']), false);

        $this->assertEquals($this->tenantDomain . '/about', $url);
    }

    public function testOfferUrl()
    {
        $offer = VehicleOffer::factory()->create();

        $url = Site::offerUrl($offer, true);

        $this->assertStringContainsString($offer->urlPath(), $url);
        $this->assertStringContainsString('a=', $url);
    }

    public function testNewCarUrl()
    {
        $newCar = NewCar::factory()->create();

        $url = Site::newCarUrl($newCar, false);

        $this->assertStringContainsString($newCar->urlPath(), $url);
        $this->assertStringStartsWith($this->tenantDomain, $url);
    }

    public function testGenericUrl()
    {
        $this->assertEquals($this->tenantDomain . '/foo/bar', Site::url('foo/bar'));
    }

    public function testUrlWithoutAbsolute()
    {
        $this->assertEquals('/foo/bar', Site::url('foo/bar', false));
    }

    public function testVehicleUrlWithWww()
    {
        Settings::make([
            'tab' => 'Automotive',
            'section' => 'General',
            'group' => 'Vehicles',
            'name' => 'Include www in vehicle page URL path',
            'config_key' => 'vehicles-url-path-include-www',
            'type' => 'boolean',
            'value' => true,
        ]);

        $this->assertStringContainsString('www.', Site::vehicleUrl(Vehicle::factory()->create()));
    }

    public function testVehicleUrlWithoutWww()
    {
        $this->assertStringNotContainsString('www.', Site::vehicleUrl(Vehicle::factory()->create()));
    }
}
