<?php

declare(strict_types=1);

namespace Apexl\Io\Dtos\Relationship\Decorators;

use Apexl\Io\Interfaces\RelationOrderBy;
use Apexl\Io\Interfaces\RelationshipInterface;
use Apexl\Vault\Enums\SortDirection;

/**
 * Returns an order-by clause keyed off the relationship's aliased remote table.
 */
final readonly class OrderByRemoteField implements RelationOrderBy
{
    public function __construct(
        public string $field,
        public SortDirection $direction = SortDirection::ASC,
    ) {
    }

    public function clauses(RelationshipInterface $relationship): array
    {
        return [[sprintf('%s.%s', $relationship->getAliasedRemoteTable(), $this->field), $this->direction]];
    }
}
