<?php

namespace Tests\Feature\Modules\Segments\ControllersLogic;

use App\Classes\Modules\Segments\ControllersLogic\ListSegmentLogic;
use App\Classes\Modules\Segments\Services\ListsSegments;
use App\Models\Segment;
use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Bus;
use App\Classes\Jobs\UserRiskAnalysis;
use Illuminate\Support\Collection;
use PHPUnit\Framework\Attributes\Group;
use Tests\TestCase;
use Mockery;

#[Group('feature')]
#[Group('segments')]
#[Group('logic')]
#[Group('ok_to_run')]
class ListSegmentLogicTest extends TestCase
{

    // use DatabaseTransactions;

    /**
     * Test ListSegmentLogic execution.
     *
     * @return void
     */
    public function test_list_segment_logic_execution()
    {
        // A R R A N G E
        Bus::fake([UserRiskAnalysis::class]);

        $email = 'test_list_segment_logic_execution@example.com';
        $user = User::where('email', $email)->first();
        if (!$user) {
            $user = new User();
            $user->name = 'test_list_segment_logic_execution';
            $user->email = $email;
            $user->password = bcrypt('password');
            $user->save();
        }
        $this->actingAs($user);

        $s1 = new Segment();
        $s1->id = 1;
        $s1->name = 'test_list_segment_logic_execution_1';

        $s2 = new Segment();
        $s2->id = 2;
        $s2->name = 'test_list_segment_logic_execution_2';

        $segments = new Collection([$s1, $s2]);

        // Mock ListsSegments
        $listsSegments = Mockery::mock(ListsSegments::class);
        $listsSegments->shouldReceive('deserializeFilters')
            ->once()
            ->with(null)
            ->andReturn([]);
        $listsSegments->shouldReceive('execute')
            ->once()
            ->with([])
            ->andReturn($segments);

        $logic = new ListSegmentLogic($listsSegments);

        $request = Request::create('/api/segments', 'GET');
        $request->headers->set('captcha-token', 'fake-token');

        // A C T
        $response = $logic->execute($request);

        // A S S E R T
        $this->assertEquals(200, $response->getStatusCode());
        $data = $response->getData(true);
        $this->assertEquals('Retrieved Segment Successful', $data['title']);
        $this->assertCount(2, $data['payload']['data']);
    }

    protected function tearDown(): void
    {
        Mockery::close();
        parent::tearDown();
    }
}
