<?php

namespace MintyPHP\Tests\Architecture;

use PHPUnit\Framework\TestCase;

class DetailPageContractTest extends TestCase
{
    use ProjectFileAssertionSupport;

    /**
     * @return list<string>
     */
    private function standardDetailFormFiles(): array
    {
        return [
            'pages/admin/users/_form.phtml',
            'pages/admin/tenants/_form.phtml',
            'pages/admin/departments/_form.phtml',
            'pages/admin/roles/_form.phtml',
            'pages/admin/permissions/_form.phtml',
            'pages/admin/scheduled-jobs/edit(default).phtml',
            'pages/admin/settings/index(default).phtml',
        ];
    }

    public function testScopedDetailFormsUseStandardDetailOptInAttribute(): void
    {
        foreach ($this->standardDetailFormFiles() as $file) {
            $content = $this->readProjectFile($file);
            $this->assertStringContainsString('data-standard-detail-form="1"', $content, $file);
        }
    }

    public function testAppInitIncludesStandardDetailPageAutoInitComponent(): void
    {
        $content = $this->readProjectFile('web/js/app-init.js');
        $this->assertStringContainsString("import './components/app-standard-detail-page.js';", $content);
    }

    public function testDetailsTitlebarExposesUnsavedMessageAndPrimarySaveMarkers(): void
    {
        $content = $this->readProjectFile('templates/partials/app-details-titlebar.phtml');
        $this->assertStringContainsString('data-detail-unsaved-message=', $content);
        $this->assertStringContainsString('data-detail-action-policy="1"', $content);
        $this->assertStringContainsString('data-detail-action-kind=', $content);
        $this->assertStringContainsString('data-detail-save-primary="1"', $content);
    }

    public function testImportsWizardFormsUseStandardDetailOptIn(): void
    {
        $content = $this->readProjectFile('pages/admin/imports/index(default).phtml');
        $this->assertStringContainsString('id="imports-upload-form"', $content);
        $this->assertStringContainsString('id="imports-mapping-form"', $content);
        $this->assertStringContainsString('id="imports-commit-form"', $content);
        $this->assertSame(3, substr_count($content, 'data-standard-detail-form="1"'));
    }
}
