<?php

namespace MintyPHP\Tests\Architecture;

use PHPUnit\Framework\TestCase;

class DetailActionPolicyContractTest extends TestCase
{
    use ProjectFileAssertionSupport;

    /**
     * @return list<string>
     */
    private function migratedConfirmFiles(): array
    {
        return [
            'pages/admin/users/edit(default).phtml',
            'pages/admin/users/_form.phtml',
            'pages/admin/tenants/edit(default).phtml',
            'pages/admin/tenants/_form.phtml',
            'pages/admin/departments/edit(default).phtml',
            'pages/admin/roles/edit(default).phtml',
            'pages/admin/permissions/edit(default).phtml',
            'pages/admin/settings/index(default).phtml',
            'templates/partials/app-details-titlebar.phtml',
            'templates/partials/app-details-aside-actions.phtml',
        ];
    }

    public function testDetailPageFactoryUsesActionPolicyModule(): void
    {
        $content = $this->readProjectFile('web/js/core/app-detail-page-factory.js');
        $this->assertStringContainsString("import { initDetailActionPolicy } from './app-details-action-policy.js';", $content);
        $this->assertStringContainsString('const actionPolicy = initDetailActionPolicy(', $content);
        $this->assertStringContainsString('actionPolicy.isSubmitting()', $content);
    }

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

        $asideActions = $this->readProjectFile('templates/partials/app-details-aside-actions.phtml');
        $this->assertStringContainsString('data-detail-confirm-message=', $asideActions);

        $danger = $this->readProjectFile('templates/partials/app-danger-zone-delete-field.phtml');
        $this->assertStringContainsString('data-detail-action-kind="delete"', $danger);
    }

    public function testMigratedFilesNoLongerUseInlineConfirmHandlers(): void
    {
        foreach ($this->migratedConfirmFiles() as $file) {
            $content = $this->readProjectFile($file);
            $this->assertStringNotContainsString('onsubmit="return confirm(', $content, $file);
            $this->assertStringNotContainsString('onclick="return confirm(', $content, $file);
        }
    }

    public function testMigratedFilesExposeDataDetailConfirmContract(): void
    {
        foreach ($this->migratedConfirmFiles() as $file) {
            $content = $this->readProjectFile($file);
            $this->assertStringContainsString('data-detail-confirm-message', $content, $file);
        }
    }
}
