<?php

namespace Feature;

use App\Models\FilterIndex;
use Carbon\Carbon;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithFaker;
use Mtc\MercuryDataModels\Dealership;
use Mtc\MercuryDataModels\Page;
use Mtc\MercuryDataModels\SeoRedirect;
use Mtc\MercuryDataModels\VehicleMake;
use Mtc\MercuryDataModels\VehicleOffer;
use Tests\TestCase;

class SeoRedirectMiddlewareTest extends TestCase
{
    use RefreshDatabase;

    protected $tenancy = true;

    public function testSeoRedirects()
    {
        Page::factory()->create(['slug' => 'aboutus', 'status' => 'published']);
        $response = $this->asTenant(tenant())
            ->withHeader('x-path', '/aboutus')
            ->getJson(route('pages.show', 'aboutus'));
        $response->assertStatus(200);

        SeoRedirect::factory()->create([
            'from' => '/aboutus',
            'to' => '/about-us',
            'code' => 301,
            'from_domain' => '',
            'to_domain' => '',
        ]);

        $response = $this->asTenant(tenant())
            ->withHeader('x-path', '/aboutus')
            ->getJson(route('pages.show', 'aboutus'));
        $response->assertStatus(404);
        $this->assertEquals(301, $response->json('code'));
        $this->assertEquals('redirect', $response->json('type'));
        $this->assertEquals('/about-us', $response->json('to'));
    }
}
