<?php

namespace Tests\Feature\BranchIsolation;

use App\Models\Branch;
use App\Models\Business;
use App\Models\Customer;
use App\Models\ManagementAlert;
use App\Models\Permission;
use App\Models\Role;
use App\Models\User;
use Database\Seeders\DatabaseSeeder;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;

class Phase8ManagementWorkScopeTest extends TestCase
{
    use RefreshDatabase;

    protected function setUp(): void
    {
        parent::setUp();
        $this->seed(DatabaseSeeder::class);
    }

    public function test_primary_only_actor_and_staff_keep_calendar_and_alert_scope_without_pivot_rows(): void
    {
        $business = Business::firstOrFail();
        $branchA = Branch::create(['business_id'=>$business->id,'name'=>'Phase 8 Management A','status'=>'active']);
        $branchB = Branch::create(['business_id'=>$business->id,'name'=>'Phase 8 Management B','status'=>'active']);

        $role = Role::create(['name'=>'phase8-management-scope','display_name'=>'Phase 8 Management Scope']);
        $role->permissions()->attach(Permission::whereIn('name',[
            'calendar.view','appointments.create','management-alerts.view',
        ])->pluck('id'));

        $actor = User::factory()->create([
            'name'=>'Primary Only Manager','username'=>'phase8_primary_manager','status'=>'active','primary_branch_id'=>$branchA->id,
        ]);
        $actor->roles()->attach($role->id);

        $staff = User::factory()->create([
            'name'=>'Primary Only Calendar Staff','username'=>'phase8_primary_calendar_staff','status'=>'active','primary_branch_id'=>$branchA->id,
        ]);
        $customer = Customer::factory()->create([
            'branch_id'=>$branchA->id,'customer_number'=>'CUS-PHASE8-MGMT',
        ]);

        ManagementAlert::create([
            'business_id'=>$business->id,'branch_id'=>$branchA->id,'type'=>'large_loan','severity'=>'warning',
            'title'=>'Primary branch alert','message'=>'Visible to the primary-only manager.','status'=>'open',
            'alert_key'=>'phase8-management-a','occurred_at'=>now(),
        ]);
        ManagementAlert::create([
            'business_id'=>$business->id,'branch_id'=>$branchB->id,'type'=>'large_loan','severity'=>'warning',
            'title'=>'Other branch alert','message'=>'Must stay hidden.','status'=>'open',
            'alert_key'=>'phase8-management-b','occurred_at'=>now(),
        ]);

        $this->assertSame([], $actor->branches()->pluck('branches.id')->all());
        $this->assertSame([], $staff->branches()->pluck('branches.id')->all());

        $this->actingAs($actor)->get(route('management-alerts.index'))
            ->assertOk()
            ->assertSee('Primary branch alert')
            ->assertDontSee('Other branch alert');

        $this->actingAs($actor)->get(route('calendar.index'))
            ->assertOk()
            ->assertSee($branchA->name)
            ->assertSee($staff->name)
            ->assertDontSee($branchB->name);

        $this->actingAs($actor)->post(route('calendar.appointments.store'),[
            'branch_id'=>$branchA->id,
            'customer_id'=>$customer->id,
            'staff_id'=>$staff->id,
            'title'=>'Primary-only staff appointment',
            'purpose'=>'Branch assignment regression',
            'scheduled_at'=>now()->addDay()->format('Y-m-d H:i:s'),
        ])->assertRedirect();

        $this->assertDatabaseHas('customer_appointments',[
            'branch_id'=>$branchA->id,'customer_id'=>$customer->id,'staff_id'=>$staff->id,
            'title'=>'Primary-only staff appointment','status'=>'scheduled',
        ]);
    }
}
