<?php

namespace Tests\Unit\Modules\Segments\Services;

use App\Classes\Modules\Segments\Services\ListsSegments;
use App\Models\Segment;
use Illuminate\Pagination\LengthAwarePaginator;
use Illuminate\Support\Collection;
use PHPUnit\Framework\Attributes\Group;
use Tests\TestCase;

#[Group('unit')]
#[Group('segments')]
#[Group('services')]
#[Group('ok_to_run')]
class ListsSegmentsTest extends TestCase
{
    // use DatabaseTransactions;

    /**
     * Test it can list segments.
     *
     * @return void
     */
    public function test_it_can_list_segments()
    {
        // A R R A N G E
        Segment::query()->delete(); // Clear existing

        $segment1 = new Segment();
        $segment1->name = 'test_it_can_list_segments_1';
        $segment1->save();

        $segment2 = new Segment();
        $segment2->name = 'test_it_can_list_segments_2';
        $segment2->save();

        $service = new ListsSegments(new Segment());

        // A C T
        $result = $service->execute();

        // A S S E R T
        $this->assertInstanceOf(Collection::class, $result);
        $this->assertCount(2, $result);
        $this->assertTrue($result->contains('name', 'test_it_can_list_segments_1'));
        $this->assertTrue($result->contains('name', 'test_it_can_list_segments_2'));
    }

    /**
     * Test it can list segments with pagination.
     *
     * @return void
     */
    public function test_it_can_paginate_segments()
    {
        // A R R A N G E
        Segment::query()->delete();

        for ($i = 1; $i <= 5; $i++) {
            $s = new Segment();
            $s->name = "test_it_can_paginate_segments_$i";
            $s->save();
        }

        $service = new ListsSegments(new Segment());

        // A C T
        $result = $service->execute(['per_page' => 2]);

        // A S S E R T
        $this->assertInstanceOf(LengthAwarePaginator::class, $result);
        $this->assertEquals(2, $result->perPage());
        $this->assertEquals(5, $result->total());
    }
}
