<?php

namespace Tests\Unit\Modules\Segments\Services;

use App\Classes\Modules\Segments\Services\ListsConstants;
use App\Models\Segment;
use App\Models\SegmentConstant;
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 ListsConstantsTest extends TestCase
{
    // use DatabaseTransactions;

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

        $segment = new Segment();
        $segment->name = 'test_it_can_list_segment_constants_SEG';
        $segment->save();

        $c1 = new SegmentConstant();
        $c1->segment_id = $segment->id;
        $c1->name = 'test_it_can_list_segment_constants_A';
        $c1->reference = 'test_it_can_list_segment_constants_REF_A';
        $c1->detail = (object) ['foo' => 'bar'];
        $c1->save();

        $c2 = new SegmentConstant();
        $c2->segment_id = $segment->id;
        $c2->name = 'test_it_can_list_segment_constants_B';
        $c2->reference = 'test_it_can_list_segment_constants_REF_B';
        $c2->detail = (object) ['foo' => 'bar'];
        $c2->save();

        $service = new ListsConstants(new SegmentConstant());

        // 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_segment_constants_A'));
        $this->assertTrue($result->contains('name', 'test_it_can_list_segment_constants_B'));
    }

    /**
     * Test it can list segment constants with filters.
     *
     * @return void
     */
    public function test_it_can_filter_segment_constants_by_segment_id()
    {
        // A R R A N G E
        SegmentConstant::query()->delete();

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

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

        $c1 = new SegmentConstant();
        $c1->segment_id = $segment1->id;
        $c1->name = 'test_it_can_filter_segment_constants_by_segment_id_C1';
        $c1->reference = 'test_it_can_filter_segment_constants_by_segment_id_REF1';
        $c1->detail = (object) ['foo' => 'bar'];
        $c1->save();

        $c2 = new SegmentConstant();
        $c2->segment_id = $segment2->id;
        $c2->name = 'test_it_can_filter_segment_constants_by_segment_id_C2';
        $c2->reference = 'test_it_can_filter_segment_constants_by_segment_id_REF2';
        $c2->detail = (object) ['foo' => 'bar'];
        $c2->save();

        $service = new ListsConstants(new SegmentConstant());

        // A C T
        $result = $service->execute(['segment_id' => $segment1->id]);

        // A S S E R T
        $this->assertCount(1, $result);
        $this->assertEquals('test_it_can_filter_segment_constants_by_segment_id_REF1', $result->first()->reference);
    }
}
