<?php

declare(strict_types=1);

namespace App\DTOs\Import;

readonly class BonusRowDto
{
    public function __construct(
        public string $employeeNumber,
        public ?string $bonusDate,
        public ?string $bonusType,
        public ?float $amount,
        public ?string $reason,
    ) {}

    /**
     * @param  array<string, mixed>  $row
     */
    public static function fromMappedRow(array $row): self
    {
        return new self(
            employeeNumber: (string) ($row['employee_number'] ?? ''),
            bonusDate: self::nullableString($row['bonus_date'] ?? null),
            bonusType: self::nullableString($row['bonus_type'] ?? null),
            amount: isset($row['amount']) && $row['amount'] !== '' ? (float) $row['amount'] : null,
            reason: self::nullableString($row['reason'] ?? null),
        );
    }

    private static function nullableString(mixed $value): ?string
    {
        if ($value === null || $value === '') {
            return null;
        }

        return (string) $value;
    }
}
