<?php

namespace Tests\Unit\Modules\Segments\Services;

use App\Classes\Modules\Segments\DataTransferObjects\ConstantObject;
use App\Classes\Modules\Segments\Services\CreatesConstant;
use App\Models\Segment;
use App\Models\SegmentConstant;
use PHPUnit\Framework\Attributes\Group;
use Tests\TestCase;

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

    /**
     * Test it can create a segment constant.
     *
     * @return void
     * @throws \App\Classes\Exceptions\MalformedRequestException
     */
    public function test_it_can_create_a_segment_constant()
    {
        // A R R A N G E
        $segment = new Segment();
        $segment->name = 'test_it_can_create_a_segment_constant_SEG';
        $segment->save();

        $detail = ['key' => 'value'];
        $object = new ConstantObject('test_it_can_create_a_segment_constant_CON', 'test_it_can_create_a_segment_constant_REF', $detail);
        $service = new CreatesConstant();

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

        // A S S E R T
        $this->assertInstanceOf(SegmentConstant::class, $result);
        $this->assertEquals('test_it_can_create_a_segment_constant_CON', $result->name);
        $this->assertEquals('test_it_can_create_a_segment_constant_REF', $result->reference);
        $this->assertEquals((object) $detail, $result->detail);
        $this->assertEquals($segment->id, $result->segment_id);

        $this->assertDatabaseHas('segment_constants', [
            'id' => $result->id,
            'name' => 'test_it_can_create_a_segment_constant_CON',
            'reference' => 'test_it_can_create_a_segment_constant_REF',
            'segment_id' => $segment->id,
        ]);
    }
}
