<?php

namespace MintyPHP\Tests\Architecture;

use PHPUnit\Framework\TestCase;

class AuthzAdminMasterDataContractTest extends TestCase
{
    use ProjectFileAssertionSupport;

    public function testAdminDepartmentsEditAndDeleteUseCentralPolicies(): void
    {
        $indexContent = $this->readProjectFile('pages/admin/departments/index().php');
        $this->assertStringContainsString('AuthorizationService::class', $indexContent);
        $this->assertStringContainsString('DepartmentAuthorizationPolicy::ABILITY_ADMIN_DEPARTMENTS_VIEW', $indexContent);

        $dataContent = $this->readProjectFile('pages/admin/departments/data().php');
        $this->assertStringContainsString('Guard::requireAbilityDecisionOrForbidden(DepartmentAuthorizationPolicy::ABILITY_ADMIN_DEPARTMENTS_VIEW);', $dataContent);
        $this->assertStringContainsString('DepartmentAuthorizationPolicy::ABILITY_ADMIN_DEPARTMENTS_VIEW', $dataContent);

        $createContent = $this->readProjectFile('pages/admin/departments/create().php');
        $this->assertStringContainsString('actionCreateContext(', $createContent);
        $this->assertStringContainsString('DepartmentAuthorizationPolicy::ABILITY_ADMIN_DEPARTMENTS_CREATE', $createContent);

        $editContent = $this->readProjectFile('pages/admin/departments/edit($id).php');
        $this->assertStringContainsString('AuthorizationService::class', $editContent);
        $this->assertStringContainsString('DepartmentAuthorizationPolicy::ABILITY_ADMIN_DEPARTMENTS_EDIT_CONTEXT', $editContent);
        $this->assertStringContainsString('DepartmentAuthorizationPolicy::ABILITY_ADMIN_DEPARTMENTS_EDIT_SUBMIT', $editContent);

        $deleteContent = $this->readProjectFile('pages/admin/departments/delete($id).php');
        $this->assertStringContainsString('AuthorizationService::class', $deleteContent);
        $this->assertStringContainsString('DepartmentAuthorizationPolicy::ABILITY_ADMIN_DEPARTMENTS_DELETE', $deleteContent);
    }

    public function testAdminRolesActionsUseCentralPolicies(): void
    {
        $indexContent = $this->readProjectFile('pages/admin/roles/index().php');
        $this->assertStringContainsString('AuthorizationService::class', $indexContent);
        $this->assertStringContainsString('RoleAuthorizationPolicy::ABILITY_ADMIN_ROLES_VIEW', $indexContent);

        $dataContent = $this->readProjectFile('pages/admin/roles/data().php');
        $this->assertStringContainsString('Guard::requireAbilityOrForbidden(RoleAuthorizationPolicy::ABILITY_ADMIN_ROLES_VIEW);', $dataContent);
        $this->assertStringContainsString('RoleAuthorizationPolicy::ABILITY_ADMIN_ROLES_VIEW', $dataContent);

        $createContent = $this->readProjectFile('pages/admin/roles/create().php');
        $this->assertStringContainsString('actionCreateContext(', $createContent);
        $this->assertStringContainsString('RoleAuthorizationPolicy::ABILITY_ADMIN_ROLES_CREATE', $createContent);

        $editContent = $this->readProjectFile('pages/admin/roles/edit($id).php');
        $this->assertStringContainsString('AuthorizationService::class', $editContent);
        $this->assertStringContainsString('RoleAuthorizationPolicy::ABILITY_ADMIN_ROLES_EDIT_CONTEXT', $editContent);
        $this->assertStringContainsString('RoleAuthorizationPolicy::ABILITY_ADMIN_ROLES_EDIT_SUBMIT', $editContent);

        $deleteContent = $this->readProjectFile('pages/admin/roles/delete($id).php');
        $this->assertStringContainsString('AuthorizationService::class', $deleteContent);
        $this->assertStringContainsString('RoleAuthorizationPolicy::ABILITY_ADMIN_ROLES_DELETE', $deleteContent);
    }

    public function testAdminPermissionsActionsUseCentralPolicies(): void
    {
        $indexContent = $this->readProjectFile('pages/admin/permissions/index().php');
        $this->assertStringContainsString('AuthorizationService::class', $indexContent);
        $this->assertStringContainsString('PermissionAuthorizationPolicy::ABILITY_ADMIN_PERMISSIONS_VIEW', $indexContent);

        $dataContent = $this->readProjectFile('pages/admin/permissions/data().php');
        $this->assertStringContainsString('Guard::requireAbilityOrForbidden(PermissionAuthorizationPolicy::ABILITY_ADMIN_PERMISSIONS_VIEW);', $dataContent);
        $this->assertStringContainsString('PermissionAuthorizationPolicy::ABILITY_ADMIN_PERMISSIONS_VIEW', $dataContent);

        $createContent = $this->readProjectFile('pages/admin/permissions/create().php');
        $this->assertStringContainsString('actionCreateContext(', $createContent);
        $this->assertStringContainsString('PermissionAuthorizationPolicy::ABILITY_ADMIN_PERMISSIONS_CREATE', $createContent);

        $editContent = $this->readProjectFile('pages/admin/permissions/edit($id).php');
        $this->assertStringContainsString('AuthorizationService::class', $editContent);
        $this->assertStringContainsString('PermissionAuthorizationPolicy::ABILITY_ADMIN_PERMISSIONS_EDIT_CONTEXT', $editContent);
        $this->assertStringContainsString('PermissionAuthorizationPolicy::ABILITY_ADMIN_PERMISSIONS_EDIT_SUBMIT', $editContent);

        $deleteContent = $this->readProjectFile('pages/admin/permissions/delete($id).php');
        $this->assertStringContainsString('AuthorizationService::class', $deleteContent);
        $this->assertStringContainsString('PermissionAuthorizationPolicy::ABILITY_ADMIN_PERMISSIONS_DELETE', $deleteContent);
    }
}
