<?php

use App\Models\Branch;
use App\Models\Household;
use App\Models\Member;
use App\Models\MemberNote;
use App\Models\Region;
use App\Models\User;

test('super_admin sees members from every branch', function () {
    $branchA = Branch::factory()->create();
    $branchB = Branch::factory()->create();
    Member::factory()->create(['branch_id' => $branchA->id]);
    Member::factory()->create(['branch_id' => $branchB->id]);

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

    expect(Member::visibleTo($superAdmin)->count())->toBe(2);
});

test('branch_admin of branch A cannot see branch B members via visibleTo', function () {
    $branchA = Branch::factory()->create();
    $branchB = Branch::factory()->create();
    $memberA = Member::factory()->create(['branch_id' => $branchA->id]);
    Member::factory()->create(['branch_id' => $branchB->id]);

    $branchAdminA = User::factory()->branchAdmin()->create(['branch_id' => $branchA->id]);

    $visible = Member::visibleTo($branchAdminA)->get();

    expect($visible)->toHaveCount(1);
    expect($visible->first()->id)->toBe($memberA->id);
});

test('region_admin sees exactly their own region\'s branch members', function () {
    $regionA = Region::factory()->create();
    $regionB = Region::factory()->create();
    $branchA1 = Branch::factory()->create(['region_id' => $regionA->id]);
    $branchA2 = Branch::factory()->create(['region_id' => $regionA->id]);
    $branchB1 = Branch::factory()->create(['region_id' => $regionB->id]);

    Member::factory()->create(['branch_id' => $branchA1->id]);
    Member::factory()->create(['branch_id' => $branchA2->id]);
    Member::factory()->create(['branch_id' => $branchB1->id]);

    $regionAdminA = User::factory()->regionAdmin()->create(['region_id' => $regionA->id]);

    expect(Member::visibleTo($regionAdminA)->count())->toBe(2);
});

test('household visibleTo scope respects branch scoping', function () {
    $branchA = Branch::factory()->create();
    $branchB = Branch::factory()->create();
    Household::factory()->create(['branch_id' => $branchA->id]);
    Household::factory()->create(['branch_id' => $branchB->id]);

    $branchAdminA = User::factory()->branchAdmin()->create(['branch_id' => $branchA->id]);

    expect(Household::visibleTo($branchAdminA)->count())->toBe(1);
});

test('member_note visibleTo scopes through the member relationship', function () {
    $branchA = Branch::factory()->create();
    $branchB = Branch::factory()->create();
    $memberA = Member::factory()->create(['branch_id' => $branchA->id]);
    $memberB = Member::factory()->create(['branch_id' => $branchB->id]);
    MemberNote::factory()->create(['member_id' => $memberA->id]);
    MemberNote::factory()->create(['member_id' => $memberB->id]);

    $branchAdminA = User::factory()->branchAdmin()->create(['branch_id' => $branchA->id]);

    $visible = MemberNote::visibleTo($branchAdminA)->get();

    expect($visible)->toHaveCount(1);
    expect($visible->first()->member_id)->toBe($memberA->id);
});
