<?php

namespace Tests\Feature;

use App\Models\User;
use App\Models\Administration;
use App\Services\User\Contracts\UserServiceInterface;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;

class SendThankCardTest extends TestCase
{
    use RefreshDatabase;

    private UserServiceInterface $userService;
    private User $sender;
    private User $receiver;

    protected function setUp(): void
    {
        parent::setUp();
        \Illuminate\Support\Facades\Http::fake();
        $this->userService = $this->app->make(UserServiceInterface::class);

        // Create sender and receiver
        $this->sender = User::create([
            'msnv'        => 5001,
            'name'        => 'Sender User',
            'mail'        => 'sender@example.com',
            'pass'        => md5('password'),
            'departments' => config('constants.DEPARTMENTS')[0],
            'role'        => config('constants.ROLE_MEMBER'),
            'status'      => config('constants.STATUS_ACTIVE'),
            'card'        => 10,
            'flag_send'   => config('constants.FLAG_SEND_ENABLED'),
            'first_login' => config('constants.FIRST_LOGIN_FALSE'),
        ]);

        $this->receiver = User::create([
            'msnv'        => 5002,
            'name'        => 'Receiver User',
            'mail'        => 'receiver@example.com',
            'pass'        => md5('password'),
            'departments' => config('constants.DEPARTMENTS')[0],
            'role'        => config('constants.ROLE_MEMBER'),
            'status'      => config('constants.STATUS_ACTIVE'),
            'card'        => 0,
            'flag_send'   => config('constants.FLAG_SEND_ENABLED'),
            'first_login' => config('constants.FIRST_LOGIN_FALSE'),
        ]);
    }

    public function test_send_thankcards_reduces_card_balance_and_creates_records(): void
    {
        $this->userService->sendThankcards($this->sender, '5002', 3);

        $this->sender->refresh();
        $this->assertEquals(7, $this->sender->card);

        // Check administration records
        $this->assertDatabaseHas('administration', [
            'msnv'     => 5002,
            'received' => 3,
            'sender'   => 5001,
            'sent'     => 0,
            'receiver' => null,
        ]);

        $this->assertDatabaseHas('administration', [
            'msnv'     => 5001,
            'received' => 0,
            'sender'   => null,
            'sent'     => 3,
            'receiver' => 5002,
        ]);
    }

    public function test_controller_validates_and_stores_thankcard(): void
    {
        $this->actingAs($this->sender);

        $response = $this->postJson(route('user.store_send'), [
            'receiver' => '5002',
            'amount' => 2,
        ]);

        $response->assertStatus(200);
        $response->assertJson([
            'success' => true,
            'receiver_email' => 'receiver@example.com',
        ]);

        $this->sender->refresh();
        $this->assertEquals(8, $this->sender->card);
    }

    public function test_controller_validation_fails_returns_correct_json_format(): void
    {
        $this->actingAs($this->sender);

        $response = $this->postJson(route('user.store_send'), [
            'receiver' => '',
            'amount' => 999,
        ]);

        $response->assertStatus(422);
        $response->assertJson([
            'success' => false,
        ]);
        $response->assertJsonStructure([
            'success',
            'errors' => [
                'receiver',
                'amount',
            ]
        ]);
    }

    public function test_get_recipient_stats_returns_correct_sent_count(): void
    {
        $this->actingAs($this->sender);

        // Before sending, stats should return 0 sent cards
        $response = $this->getJson(route('user.recipient_stats', ['receiver' => '5002']));
        $response->assertStatus(200);
        $response->assertJson([
            'success' => true,
            'sent_count' => 0,
        ]);

        // Send a card to the receiver
        $this->userService->sendThankcards($this->sender, '5002', 3);

        // Now it should return 3
        $response = $this->getJson(route('user.recipient_stats', ['receiver' => '5002']));
        $response->assertStatus(200);
        $response->assertJson([
            'success' => true,
            'sent_count' => 3,
        ]);
    }
}

