<?php

namespace App\Modules\HR\Recruitment\DTOs;

final readonly class HiringWizardData
{
    /**
     * @param  array<string, mixed>  $employee
     * @param  list<int>  $roleIds
     */
    public function __construct(
        public array $employee,
        public bool $createUserAccount,
        public ?string $password,
        public array $roleIds,
        public bool $createEmploymentContract,
        public ?string $notes,
    ) {}

    /**
     * @param  array<string, mixed>  $data
     */
    public static function fromValidated(array $data): self
    {
        $createUser = (bool) ($data['create_user_account'] ?? false);
        $meta = [
            'create_user_account', 'create_employment_contract', 'notes',
            'password', 'password_confirmation', 'role_ids',
        ];
        $employee = array_diff_key($data, array_flip($meta));

        return new self(
            employee: $employee,
            createUserAccount: $createUser,
            password: $createUser ? (string) ($data['password'] ?? '') : null,
            roleIds: $createUser
                ? array_map('intval', (array) ($data['role_ids'] ?? []))
                : [],
            createEmploymentContract: (bool) ($data['create_employment_contract'] ?? true),
            notes: isset($data['notes']) ? (string) $data['notes'] : null,
        );
    }
}
