<?php

namespace MintyPHP\Tests\Architecture;

use PHPUnit\Framework\TestCase;

class ListTemplateContractTest extends TestCase
{
    use ListContractFiles;
    use ProjectFileAssertionSupport;

    public function testListTemplatesUseCentralToolbarRendererAndStandardListWrapper(): void
    {
        foreach ($this->listIndexTemplates() as $file) {
            $content = $this->readProjectFile($file);
            $moduleContent = $this->readTemplatePageModule($file);
            $usesPartial = $this->usesSharedListFiltersPartial($content);
            $this->assertTrue(
                $usesPartial || str_contains($content, 'renderGridFilterToolbar('),
                $file . ' does not render toolbar directly and does not include shared list-filters partial.'
            );
            $this->assertTrue(
                str_contains($moduleContent, 'createListPageModule('),
                $file . ' does not initialize list module via createListPageModule.'
            );
            $this->assertStringNotContainsString('initStandardListPage(', $moduleContent, $file);
            $this->assertStringNotContainsString('gridFiltersFromSchema(', $content, $file);
            $this->assertSame(0, preg_match('/<select id=\"[^\"]*-filter\"/', $content), $file);
            $this->assertSame(0, preg_match('/<input[^>]*id=\"[^\"]*-filter\"/', $content), $file);
            $this->assertSame(0, preg_match('/filters:\s*\[/', $content), $file);
        }
    }

    public function testDrawerListTemplatesUseDrawerAndActiveFilterChips(): void
    {
        foreach ($this->drawerListTemplates() as $file) {
            $content = $this->readProjectFile($file);
            $moduleContent = $this->readTemplatePageModule($file);
            $usesPartial = $this->usesSharedListFiltersPartial($content);
            $this->assertStringContainsString("mode: 'drawer'", $moduleContent, $file);
            if (!$usesPartial) {
                $this->assertStringContainsString('data-filter-drawer-open', $content, $file);
                $this->assertStringContainsString('data-filter-drawer-apply', $content, $file);
                $this->assertStringContainsString('data-active-filter-chips', $content, $file);
            }
            $this->assertStringNotContainsString('data-filter-drawer-reset', $content, $file);
        }
    }

    public function testSearchOnlyTemplatesDoNotUseDrawerControls(): void
    {
        foreach ($this->searchOnlyTemplates() as $file) {
            $content = $this->readProjectFile($file);
            $moduleContent = $this->readTemplatePageModule($file);
            $this->assertStringContainsString("mode: 'search-only'", $moduleContent, $file);
            $this->assertStringNotContainsString('data-filter-drawer-open', $content, $file);
            $this->assertStringNotContainsString('data-filter-drawer-apply', $content, $file);
            $this->assertStringNotContainsString('data-active-filter-chips', $content, $file);
        }
    }
}
