<?php

use App\Models\Branch;
use App\Models\Region;
use App\Models\User;

test('any authenticated user can list regions with branches_count', function () {
    $region = Region::factory()->create();
    Branch::factory()->count(2)->create(['region_id' => $region->id]);
    $viewer = User::factory()->branchViewer()->create();

    $response = $this->actingAs($viewer)->getJson('/api/v1/regions');

    $response->assertOk();
    $found = collect($response->json('data'))->firstWhere('id', $region->id);
    expect($found['branches_count'])->toBe(2);
});

test('super_admin can create, update, and delete a region', function () {
    $superAdmin = User::factory()->superAdmin()->create();

    $create = $this->actingAs($superAdmin)->postJson('/api/v1/regions', [
        'name' => 'Eastern Region',
        'code' => 'ER',
    ]);
    $create->assertCreated();
    $regionId = $create->json('data.id');

    $update = $this->actingAs($superAdmin)->putJson("/api/v1/regions/{$regionId}", [
        'name' => 'Eastern Region Updated',
        'code' => 'ER',
    ]);
    $update->assertOk()->assertJsonPath('data.name', 'Eastern Region Updated');

    $delete = $this->actingAs($superAdmin)->deleteJson("/api/v1/regions/{$regionId}");
    $delete->assertStatus(204);
});

test('region_admin cannot create a region', function () {
    $regionAdmin = User::factory()->regionAdmin()->create();

    $this->actingAs($regionAdmin)->postJson('/api/v1/regions', [
        'name' => 'New Region',
        'code' => 'NR',
    ])->assertStatus(403);
});

test('region_admin can update only their own region', function () {
    $ownRegion = Region::factory()->create();
    $otherRegion = Region::factory()->create();
    $regionAdmin = User::factory()->regionAdmin()->create(['region_id' => $ownRegion->id]);

    $this->actingAs($regionAdmin)->putJson("/api/v1/regions/{$ownRegion->id}", [
        'name' => 'My Region Renamed',
        'code' => $ownRegion->code,
    ])->assertOk();

    $this->actingAs($regionAdmin)->putJson("/api/v1/regions/{$otherRegion->id}", [
        'name' => 'Hijacked',
        'code' => $otherRegion->code,
    ])->assertStatus(403);
});

test('deleting a region with branches is blocked with 409', function () {
    $superAdmin = User::factory()->superAdmin()->create();
    $region = Region::factory()->create();
    Branch::factory()->create(['region_id' => $region->id]);

    $this->actingAs($superAdmin)->deleteJson("/api/v1/regions/{$region->id}")->assertStatus(409);
});

test('creating a region with a duplicate code fails validation', function () {
    $superAdmin = User::factory()->superAdmin()->create();
    Region::factory()->create(['code' => 'DUP']);

    $this->actingAs($superAdmin)->postJson('/api/v1/regions', [
        'name' => 'Another Region',
        'code' => 'DUP',
    ])->assertStatus(422)->assertJsonValidationErrors('code');
});

test('unauthenticated request to list regions is rejected', function () {
    $this->getJson('/api/v1/regions')->assertStatus(401);
});
