<?php

declare(strict_types=1);

namespace Apexl\Tests\Core\FeatureTests\Relations;

use app\module\newtonsTrailers\Entity\BeatingHeart;
use app\module\newtonsTrailers\Entity\Company;
use PHPUnit\Framework\Attributes\Test;

final class BelongsToTest extends AbstractRelationTestBase
{
    #[Test]
    public function notSet(): void
    {
        $company = Company::factory()->create();

        $company->withLazyLoadAllowed(function (Company $company): void {
            $this->assertNull($company->beating_heart);
        });
    }

    #[Test]
    public function fetch(): void
    {
        $company = Company::factory()->create();
        $beatingHeart = BeatingHeart::factory()->create([
            'company_id' => $company->id,
            'department' => $company->beating_heart_default_department,
        ]);

        $found = Company::find(
            id: $company->id,
            fields: [
                'beating_heart.*',
            ],
        );

        $this->assertInstanceOf(BeatingHeart::class, $found->beating_heart);
        $this->assertEquals($beatingHeart->id, $found->beating_heart->id);
    }

    #[Test]
    public function replace()
    {
        $company = Company::factory()->create();

        $beatingHeart1 = BeatingHeart::factory()->create([
            'department' => $company->beating_heart_default_department,
            'company_id' => null,
        ]);
        $beatingHeart2 = BeatingHeart::factory()->create([
            'department' => $company->beating_heart_default_department,
            'company_id' => null,
        ]);

        $company->relation('beating_heart')->set($beatingHeart1->id);

        $this->assertInstanceOf(BeatingHeart::class, $company->beating_heart);
        $this->assertEquals($beatingHeart1->id, $company->beating_heart->id);

        $company->relation('beating_heart')->set($beatingHeart2->id);

        $this->assertInstanceOf(BeatingHeart::class, $company->beating_heart);
        $this->assertEquals($beatingHeart2->id, $company->beating_heart->id);

        $found = Company::find(
            id: $company->id,
            fields: [
                'beating_heart.id',
            ],
        );

        $this->assertInstanceOf(BeatingHeart::class, $found->beating_heart);
        $this->assertEquals($beatingHeart2->id, $found->beating_heart->id);

    }

    #[Test]
    public function setEntity(): void
    {
        $company = Company::factory()->create();

        $beatingHeart = BeatingHeart::factory()->create([
            'department' => $company->beating_heart_default_department,
            'company_id' => null,
        ]);

        $company->relation('beating_heart')->set($beatingHeart);

        $this->assertInstanceOf(BeatingHeart::class, $company->beating_heart);
        $this->assertEquals($beatingHeart->id, $company->beating_heart->id);

        $found = Company::find(
            id: $company->id,
            fields: [
                'beating_heart.id',
            ],
        );

        $this->assertInstanceOf(BeatingHeart::class, $found->beating_heart);
        $this->assertEquals($beatingHeart->id, $found->beating_heart->id);
    }

    #[Test]
    public function setEntityId(): void
    {
        $company = Company::factory()->create();

        $beatingHeart = BeatingHeart::factory()->create([
            'department' => $company->beating_heart_default_department,
            'company_id' => null,
        ]);

        $company->relation('beating_heart')->set($beatingHeart->id);

        $this->assertInstanceOf(BeatingHeart::class, $company->beating_heart);
        $this->assertEquals($beatingHeart->id, $company->beating_heart->id);

        $found = Company::find(
            id: $company->id,
            fields: [
                'beating_heart.id',
            ],
        );

        $this->assertInstanceOf(BeatingHeart::class, $found->beating_heart);
        $this->assertEquals($beatingHeart->id, $found->beating_heart->id);
    }

    #[Test]
    public function clear(): void
    {
        $company = Company::factory()->create();
        $beatingHeart = BeatingHeart::factory()->create([
            'company_id' => $company->id,
            'department' => $company->beating_heart_default_department,
        ]);

        $found = Company::find(
            id: $company->id,
            fields: [
                'beating_heart.id',
            ],
        );

        $found->relation('beating_heart')->clear();

        $found = Company::find(
            id: $company->id,
            fields: [
                'beating_heart.id',
            ],
        );

        $this->assertNull($found->beating_heart);
    }

}