<?php

declare(strict_types=1);

use App\Models\User;
use Database\Seeders\RoleAndPermissionSeeder;

beforeEach(function (): void {
    $this->seed(RoleAndPermissionSeeder::class);
    $this->accountUrl = 'http://'.config('app.domains.account');
    $this->adminUrl = 'http://'.config('app.domains.admin');
    $this->validToken = 'test-screenshot-token-abc123';
});

describe('Screenshot Auth Middleware', function (): void {
    it('authenticates as admin on admin subdomain with valid token in local env', function (): void {
        config([
            'app.env' => 'local',
            'app.screenshot_token' => $this->validToken,
        ]);

        $admin = User::factory()->admin()->create();

        $this->get($this->adminUrl.'/dashboard?_screenshot_token='.$this->validToken)
            ->assertOk();
    });

    it('authenticates as customer on account subdomain with valid token in local env', function (): void {
        config([
            'app.env' => 'local',
            'app.screenshot_token' => $this->validToken,
        ]);

        $customer = User::factory()->customer()->create();

        $this->get($this->accountUrl.'/dashboard?_screenshot_token='.$this->validToken)
            ->assertOk();
    });

    it('does not authenticate with invalid token', function (): void {
        config([
            'app.env' => 'local',
            'app.screenshot_token' => $this->validToken,
        ]);

        User::factory()->admin()->create();

        $this->get($this->adminUrl.'/dashboard?_screenshot_token=wrong-token')
            ->assertRedirect();
    });

    it('does not authenticate in production env with screenshot auth disabled', function (): void {
        config([
            'app.env' => 'production',
            'app.screenshot_auth_enabled' => false,
            'app.screenshot_token' => $this->validToken,
        ]);

        User::factory()->admin()->create();

        $this->get($this->adminUrl.'/dashboard?_screenshot_token='.$this->validToken)
            ->assertRedirect();
    });

    it('does not authenticate when no token is in URL', function (): void {
        config([
            'app.env' => 'local',
            'app.screenshot_token' => $this->validToken,
        ]);

        User::factory()->admin()->create();

        $this->get($this->adminUrl.'/dashboard')
            ->assertRedirect();
    });

    it('does not affect already-authenticated user', function (): void {
        config([
            'app.env' => 'local',
            'app.screenshot_token' => $this->validToken,
        ]);

        $admin = User::factory()->admin()->create();
        $customer = User::factory()->customer()->create();

        // Already logged in as admin, token should not change that
        $this->actingAs($admin)
            ->get($this->adminUrl.'/dashboard?_screenshot_token='.$this->validToken)
            ->assertOk();
    });

    it('authenticates in production env when screenshot auth is explicitly enabled', function (): void {
        config([
            'app.env' => 'production',
            'app.screenshot_auth_enabled' => true,
            'app.screenshot_token' => $this->validToken,
        ]);

        $admin = User::factory()->admin()->create();

        $this->get($this->adminUrl.'/dashboard?_screenshot_token='.$this->validToken)
            ->assertOk();
    });
});
