<?php

namespace Tests\Feature\Auth;

use App\Models\Tenant;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Auth;
use Laravel\Socialite\Facades\Socialite;
use Laravel\Socialite\Two\User as SocialiteUser;
use Mockery;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Tests\TestCase;

class TenantAdminGoogleControllerTest extends TestCase
{
    use RefreshDatabase;

    protected function tearDown(): void
    {
        Mockery::close();
        parent::tearDown();
    }

    public function test_redirect_stores_return_to_and_issues_google_redirect(): void
    {
        $driver = Mockery::mock();
        Socialite::shouldReceive('driver')->once()->with('google')->andReturn($driver);
        $driver->shouldReceive('scopes')->once()->with(['openid', 'profile', 'email'])->andReturnSelf();
        $driver->shouldReceive('with')->once()->with(['prompt' => 'select_account'])->andReturnSelf();
        $driver->shouldReceive('redirect')->once()->andReturn(new RedirectResponse('https://accounts.google.com'));

        $encodedReturn = rtrim(strtr(base64_encode(url('/test')), '+/', '-_'), '=');

        $response = $this->get('/event-admin/auth/google?return_to='.$encodedReturn);

        $response->assertRedirect('https://accounts.google.com');
        $this->assertSame($encodedReturn, session('tenant_oauth_return_to'));
    }

    public function test_callback_logs_in_tenant_admin_and_redirects_to_encoded_target(): void
    {
        $tenant = Tenant::factory()->create();
        $user = User::factory()->create([
            'tenant_id' => $tenant->id,
            'role' => 'tenant_admin',
        ]);

        $socialiteUser = tap(new SocialiteUser)->map([
            'id' => 'google-id-123',
            'name' => 'Google Tenant Admin',
            'email' => $user->email,
        ]);

        $driver = Mockery::mock();
        Socialite::shouldReceive('driver')->once()->with('google')->andReturn($driver);
        $driver->shouldReceive('user')->once()->andReturn($socialiteUser);

        $targetUrl = url('/event-admin/dashboard?foo=bar');
        $encodedReturn = rtrim(strtr(base64_encode($targetUrl), '+/', '-_'), '=');

        $this->withSession([
            'tenant_oauth_return_to' => $encodedReturn,
        ]);

        $response = $this->get('/event-admin/auth/google/callback');

        $response->assertRedirect($targetUrl);
        $this->assertAuthenticatedAs($user);
    }

    public function test_callback_redirects_back_when_user_not_found(): void
    {
        $socialiteUser = tap(new SocialiteUser)->map([
            'id' => 'missing-user',
            'name' => 'Unknown User',
            'email' => 'unknown@example.com',
        ]);

        $driver = Mockery::mock();
        Socialite::shouldReceive('driver')->once()->with('google')->andReturn($driver);
        $driver->shouldReceive('user')->once()->andReturn($socialiteUser);

        $response = $this->get('/event-admin/auth/google/callback');

        $response->assertRedirect();
        $this->assertStringContainsString('error=google_no_match', $response->headers->get('Location'));
        $this->assertFalse(Auth::check());
    }

    public function test_callback_handles_socialite_failure(): void
    {
        $driver = Mockery::mock();
        Socialite::shouldReceive('driver')->once()->with('google')->andReturn($driver);
        $driver->shouldReceive('user')->once()->andThrow(new \RuntimeException('boom'));

        $response = $this->get('/event-admin/auth/google/callback');

        $response->assertRedirect();
        $this->assertStringContainsString('error=google_failed', $response->headers->get('Location'));
    }
}
