<?php

namespace Tests\Feature\PermissionAccess;

use Tests\TestCase;
use App\Models\User;
use Spatie\Permission\Models\Role;
use Illuminate\Support\Facades\Hash;
use Spatie\Permission\Models\Permission;
use Illuminate\Foundation\Testing\RefreshDatabase;

class PermissionsTest extends TestCase
{
    use RefreshDatabase;

    protected function setUp(): void
    {
        parent::setUp();

        //Create role
        $this->userRole = Role::create(['name' => 'User']);

        // Create user
        $this->user = User::create([
            'name' => 'User',
            'email' => 'user@example.com',
            'password' => Hash::make('password'),
        ]);
        $this->user->assignRole($this->userRole);

        //Create permissions
        $this->permissionPermission = Permission::create(['name' => 'access_permissions']);
        $this->permissionPermission = Permission::create(['name' => 'create_permissions']);
        $this->permissionPermission = Permission::create(['name' => 'update_permissions']);
        $this->permissionPermission = Permission::create(['name' => 'delete_permissions']);

        $this->userPermission = Permission::create(['name' => 'access_users']);
        $this->rolePermission = Permission::create(['name' => 'access_roles']);
        
    }

    public function test_user_with_permissions_can_view_permissions_page()
    {
        $this->user->givePermissionTo('access_permissions');
        $response = $this->actingAs($this->user)->get('/user-management/permissions');
        $response->assertStatus(200);
    }

    public function test_user_without_permissions_cannot_view_permissions_page()
    {
        $this->user->revokePermissionTo('access_permissions');
        $response = $this->actingAs($this->user)->get('/user-management/permissions');
        $response->assertStatus(403);
    }
    
    public function test_user_with_permissions_can_create_permission()
    {
        $this->user->givePermissionTo('access_permissions');
        $this->user->givePermissionTo('create_permissions');
        $response = $this->actingAs($this->user)->post('/user-management/api/permissions', [
            'name' => 'New Permission',
        ]);
        $response->assertStatus(200);
    }

    public function test_user_without_permissions_cannot_create_permission()
    {
        $this->user->givePermissionTo('access_permissions');
        $this->user->revokePermissionTo('create_permissions');
        $response = $this->actingAs($this->user)->post('/user-management/api/permissions', [
            'name' => 'New Permission',
        ]);
        $response->assertStatus(403);
    }

    public function test_user_with_permissions_can_update_permission()
    {
        $this->user->givePermissionTo('access_permissions');
        $this->user->givePermissionTo('update_permissions');
        $response = $this->actingAs($this->user)->put('/user-management/api/permissions/1', [
            'name' => 'Updated Permission',
        ]);
        $response->assertStatus(200);
    }

    public function test_user_without_permissions_cannot_update_role()
    {
        $this->user->givePermissionTo('access_permissions');
        $this->user->revokePermissionTo('update_permissions');
        $response = $this->actingAs($this->user)->put('/user-management/api/permissions/1', [
            'name' => 'Updated Permission',
        ]);
        $response->assertStatus(403);
    }

    public function test_user_with_permissions_can_delete_role()
    {
        $this->user->givePermissionTo('access_permissions');
        $this->user->givePermissionTo('delete_permissions');
        $response = $this->actingAs($this->user)->delete('/user-management/api/permissions/1');
        $response->assertStatus(200);
    }

    public function test_user_without_permissions_cannot_delete_role()
    {
        $this->user->givePermissionTo('access_permissions');
        $this->user->revokePermissionTo('delete_permissions');
        $response = $this->actingAs($this->user)->delete('/user-management/api/permissions/1');
        $response->assertStatus(403);
    }

    public function test_user_with_permissions_can_bulk_delete_roles()
    {
        $this->user->givePermissionTo('access_permissions');
        $this->user->givePermissionTo('delete_permissions');
        $response = $this->actingAs($this->user)->post('/user-management/api/permissions/bulk-delete', [
            'password' => 'password',
            'ids' => [1]
        ]);
        $response->assertStatus(200);
    }

    public function test_user_with_wrong_password_cannot_bulk_delete_roles()
    {
        $this->user->givePermissionTo('access_permissions');
        $this->user->givePermissionTo('delete_permissions');
        $response = $this->actingAs($this->user)->post('/user-management/api/permissions/bulk-delete', [
            'password' => 'wrongpassword',
            'ids' => [1]
        ]);
        $response->assertSessionHasErrors('password');
        $response->assertStatus(302);
    }

    public function test_user_without_permissions_cannot_bulk_delete_roles()
    {
        $this->user->givePermissionTo('access_permissions');
        $this->user->revokePermissionTo('delete_permissions');
        $response = $this->actingAs($this->user)->post('/user-management/api/permissions/bulk-delete');
        $response->assertStatus(403);
    }
}