<?php

declare(strict_types=1);

namespace Apexl\Tests\Core\FeatureTests\Relations;

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

final class HasOneTest extends AbstractRelationTest
{
    #[Test]
    public function fetch(): void
    {
        $company1 = Company::factory()->create();

        $salesOpportunity = SalesOpportunity::factory()->create([
            'company_id' => $company1->id,
        ]);

        $found = SalesOpportunity::find(
            id: $salesOpportunity->id,
            fields: [
                'company.name',
            ],
        );

        $this->assertInstanceOf(Company::class, $found->company);
        $this->assertEquals($company1->name, $found->company->name);
    }

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

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

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

        $salesOpportunity = SalesOpportunity::factory()->create();

        $salesOpportunity->relation('company')->set($company1);

        $this->assertInstanceOf(Company::class, $salesOpportunity->company);
        $this->assertEquals($company1->id, $salesOpportunity->company->id);

        $salesOpportunity->relation('company')->set($company2);

        $this->assertInstanceOf(Company::class, $salesOpportunity->company);
        $this->assertEquals($company2->id, $salesOpportunity->company->id);

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

        $this->assertInstanceOf(Company::class, $found->company);
        $this->assertEquals($company2->id, $found->company->id);
    }

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

        $salesOpportunity = SalesOpportunity::factory()->create();

        $this->assertNull($salesOpportunity->company);

        $salesOpportunity->relation('company')->set($company1);

        $this->assertInstanceOf(Company::class, $salesOpportunity->company);
        $this->assertEquals($company1->id, $salesOpportunity->company->id);


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

        $this->assertInstanceOf(Company::class, $found->company);
        $this->assertEquals($company1->id, $found->company->id);
    }

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

        $salesOpportunity = SalesOpportunity::factory()->create();

        $this->assertNull($salesOpportunity->company);

        $salesOpportunity->relation('company')->set($company1->id);

        $this->assertInstanceOf(Company::class, $salesOpportunity->company);
        $this->assertEquals($company1->id, $salesOpportunity->company->id);


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

        $this->assertInstanceOf(Company::class, $found->company);
        $this->assertEquals($company1->id, $found->company->id);
    }

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

        $salesOpportunity = SalesOpportunity::factory()->create([
            'company_id' => $company1->id,
        ]);

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

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

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

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