<?php

namespace Tests\Unit\Modules\Segments\Services;

use App\Classes\Modules\Segments\Services\FetchesSegment;
use App\Models\Segment;
use App\Classes\Exceptions\ResourceNotFoundException;
use PHPUnit\Framework\Attributes\Group;
use Tests\TestCase;

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

    /**
     * Test it can fetch a segment by ID.
     *
     * @return void
     */
    public function test_it_can_fetch_a_segment_by_id()
    {
        // A R R A N G E
        $segment = new Segment();
        $segment->name = 'test_it_can_fetch_a_segment_by_id';
        $segment->save();

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

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

        // A S S E R T
        $this->assertInstanceOf(Segment::class, $result);
        $this->assertEquals($segment->id, $result->id);
        $this->assertEquals('test_it_can_fetch_a_segment_by_id', $result->name);
    }

    /**
     * Test it can fetch a segment by name.
     *
     * @return void
     */
    public function test_it_can_fetch_a_segment_by_name()
    {
        // A R R A N G E
        $segment = new Segment();
        $segment->name = 'test_it_can_fetch_a_segment_by_name';
        $segment->save();

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

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

        // A S S E R T
        $this->assertInstanceOf(Segment::class, $result);
        $this->assertEquals($segment->id, $result->id);
    }

    /**
     * Test it throws exception if segment not found.
     *
     * @return void
     */
    public function test_it_throws_exception_if_segment_not_found()
    {
        // A R R A N G E
        $service = new FetchesSegment(new Segment());

        // A S S E R T
        $this->expectException(ResourceNotFoundException::class);

        // A C T
        $service->execute(['id' => 99999]);
    }
}
