<?php

namespace Tests\Unit\Modules\Segments\Services;

use App\Classes\Modules\Segments\DataTransferObjects\SeasonalSegmentObject;
use App\Classes\Modules\Segments\Services\CreatesSeasonalSegment;
use App\Models\Company;
use App\Models\Segment;
use App\Models\SeasonalSegment;
use PHPUnit\Framework\Attributes\Group;
use Tests\TestCase;
use Carbon\Carbon;

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

    /**
     * Test it can create a seasonal segment.
     *
     * @return void
     * @throws \App\Classes\Exceptions\MalformedRequestException
     */
    public function test_it_can_create_a_seasonal_segment()
    {
        // A R R A N G E
        $company = new Company();
        $company->name = 'test_it_can_create_a_seasonal_segment_COMP';
        $company->reference = 'TEST-CSST';
        $company->business_type = 1;
        $company->save();

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

        $startingOn = Carbon::now()->addDay()->format('Y-m-d H:i:s');
        $endingOn = Carbon::now()->addMonth()->format('Y-m-d H:i:s');

        $object = new SeasonalSegmentObject(
            $company->id,
            $segment->id,
            $startingOn,
            $endingOn
        );

        $service = new CreatesSeasonalSegment();

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

        // A S S E R T
        $this->assertInstanceOf(SeasonalSegment::class, $result);
        $this->assertEquals($company->id, $result->company_id);
        $this->assertEquals($segment->id, $result->segment_id);
        $this->assertEquals($startingOn, $result->starting_on->format('Y-m-d H:i:s'));
        $this->assertEquals($endingOn, $result->ending_on->format('Y-m-d H:i:s'));

        $this->assertDatabaseHas('seasonal_segment', [
            'id' => $result->id,
            'company_id' => $company->id,
            'segment_id' => $segment->id,
        ]);
    }
}
