<?php

namespace Tests\Feature;

use Mtc\MercuryDataModels\Page;
use Mtc\MercuryDataModels\SeoRedirect;
use Tests\DatabaseTestCase;

class SeoRedirectMiddlewareTest extends DatabaseTestCase
{
    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'));
    }

    /**
     * A start-anchored regex 'from' must only match at the start of the path: it redirects the
     * removed-franchise URL (/stark/...) but leaves a live SRP URL (/new-motorcycles/stark) alone.
     */
    public function testStartAnchoredRegexRedirectOnlyMatchesAtPathStart()
    {
        Page::factory()->create(['slug' => 'new-motorcycles', 'status' => 'published']);

        SeoRedirect::factory()->create([
            'from' => '^/stark(.*)$',
            'to' => '/',
            'code' => 301,
            'has_regex' => 1,
            'from_domain' => '',
            'to_domain' => '',
        ]);

        // Anchored match at the start of the path -> redirect.
        $response = $this->asTenant(tenant())
            ->withHeader('x-path', '/stark/offers')
            ->getJson(route('pages.show', 'new-motorcycles'));
        $response->assertStatus(404);
        $this->assertEquals('redirect', $response->json('type'));
        $this->assertEquals(301, $response->json('code'));
        $this->assertEquals('/', $response->json('to'));

        // 'stark' appearing mid-path is a live SRP URL and must NOT be redirected.
        $response = $this->asTenant(tenant())
            ->withHeader('x-path', '/new-motorcycles/stark')
            ->getJson(route('pages.show', 'new-motorcycles'));
        $response->assertStatus(200);
    }

    public function testSeoGoneReturnsHttp410()
    {
        Page::factory()->create(['slug' => 'old-product', 'status' => 'published']);

        SeoRedirect::factory()->create([
            'from' => '/old-product',
            'to' => null,
            'code' => 410,
            'from_domain' => '',
            'to_domain' => '',
        ]);

        $response = $this->asTenant(tenant())
            ->withHeader('x-path', '/old-product')
            ->getJson(route('pages.show', 'old-product'));

        $response->assertStatus(410);
        $this->assertEquals(410, $response->json('code'));
        $this->assertEquals('gone', $response->json('type'));
        $this->assertNull($response->json('to'));
    }
}
