<?php

declare(strict_types=1);

namespace Apexl\Tests\Core\Infrastructure;

use apexl\entityCore\collections\EntityCollection;
use apexl\Io\dto\Relationship\BelongsTo;
use apexl\Io\dto\Relationship\BelongsToMany;
use apexl\Io\dto\Relationship\HasMany;
use apexl\Io\dto\Relationship\HasOne;
use apexl\Io\includes\Entity;

/**
 * @property int $id
 * @property string $name
 *
 * @property-read string $name_twice
 * @property-read TestRelatedEntity $belongs_to
 * @property-read TestRelatedEntity $has_one
 * @property-read EntityCollection $belongs_to_many
 * @property-read EntityCollection $has_many
 *
 * @method BelongsTo belongs_to()
 * @method HasOne has_one()
 * @method BelongsToMany belongs_to_many()
 * @method HasMany has_many()
 *
 * @method static TestEntityFactory factory()
 */
final class TestEntity extends Entity
{
    static public ?string $tableName;
    static public ?string $hasManyPivotTableName;

    public function __construct()
    {
        parent::__construct(TestEntity::$tableName);
    }

    public static function entityFactoryClass(): string
    {
        return TestEntityFactory::class;
    }

    protected function definedRelations(): array
    {
        return [
            $this->hasOne('has_one', TestRelatedEntity::class, 'related_test_entity_id'),
            $this->belongsTo('belongs_to', TestRelatedEntity::class),
            $this->belongsToMany('belongs_to_many', TestRelatedEntity::class),
            $this->hasMany(
                name: 'has_many',
                remoteEntity: TestRelatedEntity::class,
                pivotTable: TestEntity::$hasManyPivotTableName,
                pivotLocal: 'local_id',
                pivotRemote: 'remote_id',
            ),
        ];
    }

    protected function definedVirtualFields(): array
    {
        return [
            'name_twice' => 'concat(name,name)'
        ];
    }
}