<?php

namespace Tests\Feature;

use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Route;
use Tests\TestCase;

class AdminOnlyMiddlewareTest extends TestCase
{
    use RefreshDatabase;

    protected function setUp(): void
    {
        parent::setUp();
        // Register a dummy route protected by admin.only to exercise middleware
        Route::middleware(['web', 'auth', 'admin.only'])->get('/_admin-only-test', function () {
            return 'ok';
        });
    }

    public function test_guest_is_redirected_to_login(): void
    {
        $this->get('/_admin-only-test')->assertRedirect('/login');
    }

    public function test_customer_is_redirected_to_customer_portal(): void
    {
        $user = \Mockery::mock(User::class)->makePartial();
        $user->shouldReceive('isCustomer')->andReturn(true);
        $this->be($user);
        $this->get('/_admin-only-test')->assertRedirect('/customer-portal');
    }

    public function test_admin_passes_through(): void
    {
        $user = User::factory()->create();
        if (method_exists($user, 'assignRole')) {
            $user->assignRole('admin');
        }
        $this->actingAs($user);
        $this->get('/_admin-only-test')->assertOk();
    }
}
