<?php

namespace Tests\Unit\Modules\Segments\Services;

use App\Classes\Modules\Segments\DataTransferObjects\ConstantObject;
use App\Classes\Modules\Segments\Services\UpdatesConstant;
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 UpdatesConstantTest extends TestCase
{
    // use DatabaseTransactions;

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

        $constant = new SegmentConstant();
        $constant->segment_id = $segment->id;
        $constant->name = 'test_it_can_update_a_segment_constant_OLD';
        $constant->reference = 'old-reference';
        $constant->detail = (object) ['old' => 'detail'];
        $constant->save();

        $newDetail = ['new' => 'detail'];
        $object = new ConstantObject('test_it_can_update_a_segment_constant_NEW', 'test_it_can_update_a_segment_constant_REF', $newDetail);
        $service = new UpdatesConstant();

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

        // A S S E R T
        $this->assertInstanceOf(SegmentConstant::class, $result);
        $this->assertEquals('test_it_can_update_a_segment_constant_NEW', $result->name);
        $this->assertEquals('test_it_can_update_a_segment_constant_REF', $result->reference);
        $this->assertEquals((object) $newDetail, $result->detail);
        $this->assertEquals($constant->id, $result->id);

        $this->assertDatabaseHas('segment_constants', [
            'id' => $constant->id,
            'name' => 'test_it_can_update_a_segment_constant_NEW',
            'reference' => 'test_it_can_update_a_segment_constant_REF',
        ]);
    }
}
