<?php

namespace App\Helpers;

use App\Models\StatusAkun;
use App\Models\UserModel;
use App\Models\UserRole;
use App\Interfaces\UserRequest;

class UserMapper
{


  public function toUser(array $row): UserModel
  {

    $user = new UserModel(
      id: $row["id"],
      nama: $row["nama"],
      username: $row["username"],
      password: $row["password"],
      email: $row["email"],
      noTelepon: $row["noTelepon"],
      role: UserRole::from($row["role"]),
      divisiId: $row["divisiId"],
      namaDivisi: $row["namaDivisi"],
      statusAkun: StatusAkun::from($row["statusAkun"]),
      createdAt: $row["createdAt"],
      updatedAt: $row["updatedAt"],
      spesialisasi: $row["spesialisasi"]
    );

    return $user;
  }

  public function toArray(UserRequest $request, bool $includePassword = true): array
  {
    $params = [
      'nama' => $request->getNama(),
      'username' => $request->getUsername(),
      'email' => $request->getEmail(),
      'no_telepon' => $request->getNoTelepon(),
      'role' => $request->getRole()->value,
      'id_divisi' => $request->getDivisiId(),
      'status_akun' => $request->getStatusAkun()->value
    ];

    if ($request->getId() !== null) {
      $params['id_user'] = $request->getId();
    }

    if ($includePassword) {
      $params['password'] = $request->getPassword();
    }

    return $params;
  }

  public static function excludePassword(UserModel $user): array
  {
    return [
      'id' => $user->getId(),
      'nama' => $user->getNama(),
      'username' => $user->getUsername(),
      'email' => $user->getEmail(),
      'noTelepon' => $user->getNoTelepon(),
      'role' => $user->getRole()->value,
      'divisiId' => $user->getDivisiId(),
      'namaDivisi' => $user->getNamaDivisi(),
      'statusAkun' => $user->getStatusAkun()->value,
      'createdAt' => $user->getCreatedAt(),
      'updatedAt' => $user->getUpdatedAt(),
      'spesialisasi' => $user->getSpesialisasi(),
    ];
  }
}
