<?php

namespace Tests\Tenant;

use Mtc\MercuryDataModels\Page;
use Tests\TenantTestCase;
use Tests\UserForTenant;

class PageBulkUpdateValidationTest extends TenantTestCase
{
    use UserForTenant;

    protected bool $needsMasterDatabase = true;

    private function submit(array $payload)
    {
        return $this->actingAs($this->getUser())
            ->withHeader('X-Tenant', tenant('id'))
            ->postJson(route('tenant.content-pages.bulk-update.bulk-update'), $payload);
    }

    public function testSameModeRejectsOverlongSeoTitle(): void
    {
        $page = Page::factory()->create();

        $this->submit([
            'mode' => 'same',
            'page_ids' => [$page->id],
            'field' => 'seo.title',
            'value' => str_repeat('a', 251),
        ])->assertStatus(422)->assertJsonValidationErrors(['value']);
    }

    public function testSameModeRejectsInvalidJsonSchema(): void
    {
        $page = Page::factory()->create();

        $this->submit([
            'mode' => 'same',
            'page_ids' => [$page->id],
            'field' => 'seo.jsonSchema',
            'value' => '{not valid json',
        ])->assertStatus(422)->assertJsonValidationErrors(['value']);
    }

    public function testSameModeRejectsInvalidPublishedAt(): void
    {
        $page = Page::factory()->create();

        $this->submit([
            'mode' => 'same',
            'page_ids' => [$page->id],
            'field' => 'published_at',
            'value' => 'not-a-date',
        ])->assertStatus(422)->assertJsonValidationErrors(['value']);
    }

    public function testSameModeRejectsInvalidStatus(): void
    {
        $page = Page::factory()->create();

        $this->submit([
            'mode' => 'same',
            'page_ids' => [$page->id],
            'field' => 'status',
            'value' => 'archived-nonsense',
        ])->assertStatus(422)->assertJsonValidationErrors(['value']);
    }

    public function testSameModeAcceptsValidValues(): void
    {
        $page = Page::factory()->create();

        $this->submit([
            'mode' => 'same',
            'page_ids' => [$page->id],
            'field' => 'published_at',
            'value' => '2026-12-25T10:30:00',
        ])->assertStatus(200);
    }

    public function testDifferentModeRejectsOverlongSeoTitlePerRow(): void
    {
        $page = Page::factory()->create();

        $this->submit([
            'mode' => 'different',
            'page_ids' => [$page->id],
            'updates' => [
                $page->id => ['seo.title' => str_repeat('a', 251)],
            ],
        ])->assertStatus(422)->assertJsonValidationErrors(["updates.{$page->id}.seo.title"]);
    }

    public function testDifferentModeAcceptsValidValues(): void
    {
        $page = Page::factory()->create();

        $this->submit([
            'mode' => 'different',
            'page_ids' => [$page->id],
            'updates' => [
                $page->id => ['seo.title' => 'A perfectly reasonable SEO title'],
            ],
        ])->assertStatus(200);
    }
}
