<?php

namespace Tests\Feature;

use App\Enums\UserRole;
use App\Models\Business;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;

class BusinessWorkspaceSwitcherTest extends TestCase
{
    use RefreshDatabase;

    public function test_business_dropdown_uses_real_accessible_businesses(): void
    {
        [$owner, $business] = $this->ownerWithBusiness('Real Salon');

        $this->actingAs($owner)
            ->withSession(['current_business_id' => $business->id])
            ->get('/dashboard')
            ->assertOk()
            ->assertSee('Real Salon')
            ->assertDontSee('Prime Dental Clinic');
    }

    public function test_owner_can_switch_between_accessible_businesses(): void
    {
        [$owner, $first] = $this->ownerWithBusiness('First Studio');
        $second = Business::factory()->create([
            'owner_id' => $owner->id,
            'name' => 'Second Studio',
            'slug' => 'second-studio',
            'status' => 'active',
        ]);

        $this->actingAs($owner)
            ->withSession(['current_business_id' => $first->id])
            ->post(route('admin.businesses.switch', $second))
            ->assertRedirect();

        $this->assertSame($second->id, session('current_business_id'));
    }

    public function test_owner_can_create_additional_business_workspace(): void
    {
        [$owner, $business] = $this->ownerWithBusiness('Main Studio');

        $this->actingAs($owner)
            ->withSession(['current_business_id' => $business->id])
            ->post('/businesses', [
                'name' => 'New Beauty Branch',
                'email' => 'branch@example.test',
                'phone' => '+15550001111',
            ])
            ->assertRedirect('/dashboard');

        $created = Business::query()->where('name', 'New Beauty Branch')->firstOrFail();

        $this->assertSame($owner->id, $created->owner_id);
        $this->assertSame($created->id, session('current_business_id'));
        $this->assertTrue($owner->fresh()->canAccessBusiness($created));
    }

    public function test_owner_cannot_switch_to_unassigned_business(): void
    {
        [$owner, $business] = $this->ownerWithBusiness('Owner Studio');
        [, $otherBusiness] = $this->ownerWithBusiness('Other Studio', 'other-owner@example.test');

        $this->actingAs($owner)
            ->withSession(['current_business_id' => $business->id])
            ->post(route('admin.businesses.switch', $otherBusiness))
            ->assertForbidden();
    }

    private function ownerWithBusiness(string $businessName, string $email = 'owner@example.test'): array
    {
        $owner = User::factory()->create([
            'email' => $email,
            'role' => UserRole::BusinessOwner->value,
        ]);

        $business = Business::factory()->create([
            'owner_id' => $owner->id,
            'name' => $businessName,
            'slug' => str($businessName)->slug()->toString(),
            'status' => 'active',
        ]);

        return [$owner, $business];
    }
}
