<?php

namespace Tests\Feature\Auth;

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

class RoleBasedLoginTest extends TestCase
{
    use RefreshDatabase;

    public function test_tenant_admin_redirects_to_event_admin_dashboard()
    {
        $user = User::factory()->create([
            'email' => 'tenant@example.com',
            'role' => 'tenant_admin',
            'password' => bcrypt('password'),
            'email_verified_at' => now(),
        ]);

        $response = $this->post(route('login.store'), [
            'login' => 'tenant@example.com',
            'password' => 'password',
        ]);

        $this->assertAuthenticated();
        $response->assertRedirect('/event-admin/dashboard');
        $this->assertEquals('tenant@example.com', Auth::user()->email);
    }

    public function test_super_admin_redirects_to_super_admin_panel()
    {
        $user = User::factory()->create([
            'email' => 'super@example.com',
            'role' => 'super_admin',
            'password' => bcrypt('password'),
            'email_verified_at' => now(),
        ]);

        $response = $this->post(route('login.store'), [
            'login' => 'super@example.com',
            'password' => 'password',
        ]);

        $this->assertAuthenticated();
        $response->assertRedirect('/super-admin');
        $this->assertEquals('super@example.com', Auth::user()->email);
    }

    public function test_regular_user_redirects_to_packages()
    {
        $user = User::factory()->create([
            'email' => 'regular@example.com',
            'role' => 'user', // Regular user with 'user' role
            'password' => bcrypt('password'),
            'email_verified_at' => now(),
        ]);

        $response = $this->post(route('login.store'), [
            'login' => 'regular@example.com',
            'password' => 'password',
        ]);

        $this->assertAuthenticated();
        $response->assertRedirect('/packages');
        $this->assertEquals('regular@example.com', Auth::user()->email);
    }
}
