<?php

/**
 * 文件功能：后台用户管理编辑功能测试
 *
 * 覆盖后台用户编辑页对任命职务体系的复用，
 * 确保用户资料弹窗可以直接任命、切换或撤销用户职务。
 */

namespace Tests\Feature\Feature;

use App\Models\Department;
use App\Models\Position;
use App\Models\User;
use App\Models\UserPosition;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;

/**
 * 类功能：验证后台用户管理页面的用户资料编辑行为。
 */
class AdminUserManagerTest extends TestCase
{
    use RefreshDatabase;

    /**
     * 方法功能：验证站长可以在用户编辑页直接任命职务。
     */
    public function test_site_owner_can_assign_position_from_user_editor(): void
    {
        $siteOwner = $this->createSiteOwner();
        $targetUser = User::factory()->create();
        $position = $this->createPosition('招商主管', '🎖️', 66);

        $response = $this->actingAs($siteOwner)
            ->putJson(route('admin.users.update', $targetUser), [
                'exp_num' => $targetUser->exp_num ?? 0,
                'jjb' => $targetUser->jjb ?? 0,
                'meili' => $targetUser->meili ?? 0,
                'sex' => $targetUser->sex ?? 0,
                'qianming' => $targetUser->qianming,
                'position_id' => $position->id,
            ]);

        $response->assertOk()
            ->assertJson([
                'status' => 'success',
            ]);

        $activePosition = $targetUser->fresh()->activePosition;

        $this->assertNotNull($activePosition);
        $this->assertSame($position->id, $activePosition?->position_id);
        $this->assertSame($position->level, $targetUser->fresh()->user_level);
    }

    /**
     * 方法功能：验证站长可以在用户编辑页撤销现有职务。
     */
    public function test_site_owner_can_revoke_position_from_user_editor(): void
    {
        $siteOwner = $this->createSiteOwner();
        $targetUser = User::factory()->create([
            'exp_num' => 0,
            'user_level' => 66,
        ]);
        $position = $this->createPosition('值班主持', '🎤', 66);

        UserPosition::create([
            'user_id' => $targetUser->id,
            'position_id' => $position->id,
            'appointed_by_user_id' => $siteOwner->id,
            'appointed_at' => now(),
            'is_active' => true,
        ]);

        $response = $this->actingAs($siteOwner)
            ->putJson(route('admin.users.update', $targetUser), [
                'exp_num' => $targetUser->exp_num ?? 0,
                'jjb' => $targetUser->jjb ?? 0,
                'meili' => $targetUser->meili ?? 0,
                'sex' => $targetUser->sex ?? 0,
                'qianming' => $targetUser->qianming,
                'position_id' => null,
            ]);

        $response->assertOk()
            ->assertJson([
                'status' => 'success',
            ]);

        $targetUser->refresh();

        $this->assertNull($targetUser->activePosition);
        $this->assertSame(1, $targetUser->user_level);
    }

    /**
     * 方法功能：创建测试用站长账号。
     */
    private function createSiteOwner(): User
    {
        return User::factory()->create([
            'id' => 1,
            'user_level' => 100,
        ]);
    }

    /**
     * 方法功能：创建测试用部门与职务。
     */
    private function createPosition(string $name, string $icon, int $level): Position
    {
        $department = Department::create([
            'name' => '测试部门-'.$name,
            'rank' => 90,
            'color' => '#334155',
            'sort_order' => 1,
            'description' => '后台用户管理测试部门',
        ]);

        return Position::create([
            'department_id' => $department->id,
            'name' => $name,
            'icon' => $icon,
            'rank' => $level,
            'level' => $level,
            'max_persons' => 5,
            'sort_order' => 1,
            'permissions' => [],
        ]);
    }
}
