<?php

namespace MintyPHP\Tests\Architecture;

trait ListContractFiles
{
    private function readTemplatePageModule(string $templateFile): string
    {
        $template = $this->readProjectFile($templateFile);
        $matched = preg_match(
            "/assetVersion\\('((?:modules\\/[^\\/]+\\/)?js\\/pages\\/[^']+\\.js)'\\)/",
            $template,
            $captures
        );
        $this->assertSame(1, $matched, $templateFile . ' must reference a page module via assetVersion().');

        $assetPath = ltrim($captures[1], '/');
        $webPath = 'web/' . $assetPath;
        $root = $this->projectRootPath();

        if (is_file($root . '/' . $webPath)) {
            return $this->readProjectFile($webPath);
        }

        if (str_starts_with($assetPath, 'modules/')) {
            $sourcePath = preg_replace('#^modules/([^/]+)/#', 'modules/$1/web/', $assetPath, 1);
            $this->assertIsString($sourcePath, 'Could not resolve module asset path for ' . $templateFile);

            return $this->readProjectFile($sourcePath);
        }

        return $this->readProjectFile($webPath);
    }

    private function usesSharedListFiltersPartial(string $content): bool
    {
        return str_contains($content, "require templatePath('partials/app-list-filters.phtml');");
    }

    /**
     * @return list<string>
     */
    private function hardCutGridEndpointFiles(): array
    {
        return [
            'modules/addressbook/pages/address-book/data().php',
            'modules/helpdesk/pages/helpdesk/search-data().php',
            'pages/search/data().php',
            'pages/admin/users/data().php',
            'pages/admin/tenants/data().php',
            'pages/admin/departments/data().php',
            'pages/admin/roles/data().php',
            'pages/admin/permissions/data().php',
            'pages/admin/mail-log/data().php',
            'pages/admin/scheduled-jobs/data().php',
            'pages/admin/scheduled-jobs/runs-data($id).php',
            'modules/audit/pages/audit/api-audit/data().php',
            'modules/audit/pages/audit/import-audit/data().php',
            'modules/audit/pages/audit/settings-user-lifecycle/audit-data().php',
            'modules/audit/pages/audit/system-audit/data().php',
        ];
    }

    /**
     * @return array<string,string>
     */
    private function hardCutGridEndpointSchemaFiles(): array
    {
        return [
            'modules/addressbook/pages/address-book/data().php' => "gridParseFiltersFromSchemaFile(__DIR__ . '/filter-schema.php'",
            'modules/helpdesk/pages/helpdesk/search-data().php' => "gridParseFiltersFromSchemaFile(__DIR__ . '/filter-schema.php'",
            'pages/search/data().php' => "gridParseFiltersFromSchemaFile(__DIR__ . '/filter-schema.php'",
            'pages/admin/users/data().php' => "gridParseFiltersFromSchemaFile(__DIR__ . '/filter-schema.php'",
            'pages/admin/tenants/data().php' => "gridParseFiltersFromSchemaFile(__DIR__ . '/filter-schema.php'",
            'pages/admin/departments/data().php' => "gridParseFiltersFromSchemaFile(__DIR__ . '/filter-schema.php'",
            'pages/admin/roles/data().php' => "gridParseFiltersFromSchemaFile(__DIR__ . '/filter-schema.php'",
            'pages/admin/permissions/data().php' => "gridParseFiltersFromSchemaFile(__DIR__ . '/filter-schema.php'",
            'pages/admin/mail-log/data().php' => "gridParseFiltersFromSchemaFile(__DIR__ . '/filter-schema.php'",
            'pages/admin/scheduled-jobs/data().php' => "gridParseFiltersFromSchemaFile(__DIR__ . '/filter-schema.php'",
            'pages/admin/scheduled-jobs/runs-data($id).php' => "gridParseFiltersFromSchemaFile(__DIR__ . '/runs-filter-schema.php'",
            'modules/audit/pages/audit/api-audit/data().php' => "gridParseFiltersFromSchemaFile(__DIR__ . '/filter-schema.php'",
            'modules/audit/pages/audit/import-audit/data().php' => "gridParseFiltersFromSchemaFile(__DIR__ . '/filter-schema.php'",
            'modules/audit/pages/audit/settings-user-lifecycle/audit-data().php' => "gridParseFiltersFromSchemaFile(__DIR__ . '/filter-schema.php'",
            'modules/audit/pages/audit/system-audit/data().php' => "gridParseFiltersFromSchemaFile(__DIR__ . '/filter-schema.php'",
        ];
    }

    /**
     * @return list<string>
     */
    private function listIndexTemplates(): array
    {
        return [
            'modules/addressbook/pages/address-book/index(default).phtml',
            'modules/helpdesk/pages/helpdesk/index(default).phtml',
            'pages/search/index(default).phtml',
            'pages/admin/users/index(default).phtml',
            'pages/admin/tenants/index(default).phtml',
            'pages/admin/departments/index(default).phtml',
            'pages/admin/roles/index(default).phtml',
            'pages/admin/permissions/index(default).phtml',
            'pages/admin/mail-log/index(default).phtml',
            'pages/admin/scheduled-jobs/index(default).phtml',
            'modules/audit/pages/audit/api-audit/index(default).phtml',
            'modules/audit/pages/audit/import-audit/index(default).phtml',
            'modules/audit/templates/settings-user-lifecycle-panel.phtml',
            'modules/audit/pages/audit/system-audit/index(default).phtml',
        ];
    }

    /**
     * @return list<string>
     */
    private function listIndexActions(): array
    {
        return [
            'modules/addressbook/pages/address-book/index().php',
            'modules/helpdesk/pages/helpdesk/index().php',
            'pages/search/index().php',
            'pages/admin/users/index().php',
            'pages/admin/tenants/index().php',
            'pages/admin/departments/index().php',
            'pages/admin/roles/index().php',
            'pages/admin/permissions/index().php',
            'pages/admin/mail-log/index().php',
            'pages/admin/scheduled-jobs/index().php',
            'modules/audit/pages/audit/api-audit/index().php',
            'modules/audit/pages/audit/import-audit/index().php',
            'modules/audit/templates/settings-user-lifecycle-panel.phtml',
            'modules/audit/pages/audit/system-audit/index().php',
        ];
    }

    /**
     * @return list<string>
     */
    private function drawerListTemplates(): array
    {
        return [
            'modules/addressbook/pages/address-book/index(default).phtml',
            'modules/helpdesk/pages/helpdesk/index(default).phtml',
            'pages/admin/users/index(default).phtml',
            'pages/admin/tenants/index(default).phtml',
            'pages/admin/departments/index(default).phtml',
            'pages/admin/roles/index(default).phtml',
            'pages/admin/permissions/index(default).phtml',
            'pages/admin/mail-log/index(default).phtml',
            'pages/admin/scheduled-jobs/index(default).phtml',
            'modules/audit/pages/audit/api-audit/index(default).phtml',
            'modules/audit/pages/audit/import-audit/index(default).phtml',
            'modules/audit/templates/settings-user-lifecycle-panel.phtml',
            'modules/audit/pages/audit/system-audit/index(default).phtml',
        ];
    }

    /**
     * @return list<string>
     */
    private function searchOnlyTemplates(): array
    {
        return [
            'pages/search/index(default).phtml',
        ];
    }
}
