<?php

declare(strict_types=1);

use Builder\Seo\Models\Redirect;
use Illuminate\Support\Facades\Schema;

it('passes through when no redirect matches', function (): void {
    $this->get('/some-page-that-does-not-exist')
        ->assertStatus(404);
});

it('performs a 301 redirect for permanent type', function (): void {
    Redirect::create(['from' => '/old-page', 'to' => '/new-page', 'type' => 'permanent']);

    $this->get('/old-page')
        ->assertRedirect('/new-page')
        ->assertStatus(301);
});

it('performs a 302 redirect for temporary type', function (): void {
    Redirect::create(['from' => '/temp-page', 'to' => '/elsewhere', 'type' => 'temporary']);

    $this->get('/temp-page')
        ->assertRedirect('/elsewhere')
        ->assertStatus(302);
});

it('only matches exact from path', function (): void {
    Redirect::create(['from' => '/old', 'to' => '/new', 'type' => 'permanent']);

    $this->get('/old-different')
        ->assertStatus(404);
});

it('passes through gracefully when redirects table does not exist', function (): void {
    Schema::drop('redirects');

    $this->get('/some-page')
        ->assertStatus(404);

    Schema::create('redirects', function ($table): void {
        $table->id();
        $table->string('from')->unique();
        $table->string('to');
        $table->string('type')->default('permanent');
        $table->timestamps();
    });
});
