<?php

namespace Tests\Feature;

use App\Models\FriendRequest;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;

class FriendControllerTest extends TestCase
{
    use RefreshDatabase;

    public function test_can_check_friend_status()
    {
        $me = User::factory()->create();
        $target = User::factory()->create();

        // 此时不是好友
        $response = $this->actingAs($me)->getJson(route('friend.status', $target->username));
        $response->assertStatus(200);
        $response->assertJson([
            'is_friend' => false,
            'mutual' => false,
        ]);

        // 我加了对方
        FriendRequest::create(['who' => $me->username, 'towho' => $target->username, 'sub_time' => now()]);

        $response = $this->actingAs($me)->getJson(route('friend.status', $target->username));
        $response->assertStatus(200);
        $response->assertJson([
            'is_friend' => true,
            'mutual' => false,
        ]);

        // 对方也加了我
        FriendRequest::create(['who' => $target->username, 'towho' => $me->username, 'sub_time' => now()]);

        $response = $this->actingAs($me)->getJson(route('friend.status', $target->username));
        $response->assertStatus(200);
        $response->assertJson([
            'is_friend' => true,
            'mutual' => true,
        ]);
    }

    public function test_cannot_add_self_as_friend()
    {
        $me = User::factory()->create();

        $response = $this->actingAs($me)->postJson(route('friend.add', $me->username));

        $response->assertStatus(422);
        $response->assertJsonFragment(['message' => '不能将自己加为好友']);
    }

    public function test_cannot_add_nonexistent_user()
    {
        $me = User::factory()->create();

        $response = $this->actingAs($me)->postJson(route('friend.add', 'nonexistent_foo'));

        $response->assertStatus(404);
        $response->assertJsonFragment(['message' => '用户不存在']);
    }

    public function test_can_add_friend()
    {
        $me = User::factory()->create();
        $target = User::factory()->create();

        $response = $this->actingAs($me)->postJson(route('friend.add', $target->username));

        $response->assertStatus(200);
        $response->assertJsonFragment(['status' => 'success']);

        $this->assertDatabaseHas('friend_requests', [
            'who' => $me->username,
            'towho' => $target->username,
        ]);
    }

    public function test_cannot_add_same_friend_twice()
    {
        $me = User::factory()->create();
        $target = User::factory()->create();

        FriendRequest::create(['who' => $me->username, 'towho' => $target->username, 'sub_time' => now()]);

        $response = $this->actingAs($me)->postJson(route('friend.add', $target->username));

        $response->assertStatus(422);
        $response->assertJsonFragment(['message' => '已是好友，无需重复添加']);

        // 确保只有1条记录
        $this->assertEquals(1, FriendRequest::where('who', $me->username)->where('towho', $target->username)->count());
    }

    public function test_can_remove_friend()
    {
        $me = User::factory()->create();
        $target = User::factory()->create();

        FriendRequest::create(['who' => $me->username, 'towho' => $target->username, 'sub_time' => now()]);

        $response = $this->actingAs($me)->deleteJson(route('friend.remove', $target->username));

        $response->assertStatus(200);
        $response->assertJsonFragment(['status' => 'success']);

        $this->assertDatabaseMissing('friend_requests', [
            'who' => $me->username,
            'towho' => $target->username,
        ]);
    }

    public function test_cannot_remove_non_friend()
    {
        $me = User::factory()->create();
        $target = User::factory()->create();

        $response = $this->actingAs($me)->deleteJson(route('friend.remove', $target->username));

        $response->assertStatus(404);
        $response->assertJsonFragment(['message' => '好友关系不存在']);
    }

    public function test_can_view_friend_list()
    {
        $me = User::factory()->create();

        $myFriend = User::factory()->create();
        $someoneWhoAddedMe = User::factory()->create();
        $mutualFriend = User::factory()->create();

        // 我加了他
        FriendRequest::create(['who' => $me->username, 'towho' => $myFriend->username, 'sub_time' => now()]);

        // 他加了我 (Pending)
        FriendRequest::create(['who' => $someoneWhoAddedMe->username, 'towho' => $me->username, 'sub_time' => now()]);

        // 互相好友
        FriendRequest::create(['who' => $me->username, 'towho' => $mutualFriend->username, 'sub_time' => now()]);
        FriendRequest::create(['who' => $mutualFriend->username, 'towho' => $me->username, 'sub_time' => now()]);

        $response = $this->actingAs($me)->getJson(route('friend.index'));

        $response->assertStatus(200);

        $response->assertJsonStructure([
            'status',
            'friends' => [
                '*' => ['username', 'headface', 'user_level', 'sex', 'mutual', 'sub_time', 'is_online'],
            ],
            'pending' => [
                '*' => ['username', 'headface', 'user_level', 'sex', 'added_at', 'is_online'],
            ],
        ]);

        $responseData = $response->json();

        $this->assertCount(2, $responseData['friends']);
        $this->assertCount(1, $responseData['pending']);

        // 验证 pending 列表
        $this->assertEquals($someoneWhoAddedMe->username, $responseData['pending'][0]['username']);
    }
}
