<?php

namespace App\Libraries\DBMigration\Process;

use CodeIgniter\Database\BaseConnection;

class LineProcess implements MigrationProcessInterface
{
  private $db;
  public function __construct(BaseConnection $db)
  {
    $this->db = $db;
  }
  public function process(string $site, array $row, int $cnt): void
  {
    $temps = [];
    $temps['code'] = $row['Line_Code'];
    $temps['type'] = $row['Line_case'];
    $temps['role'] = $row['Client_Reseller'] == 30 ? "reseller" : "user";
    $temps['name'] = $row['Client_Name'];
    $temps['phone'] = empty($row['Client_Phone1']) ? "" : $row['Client_Phone1'];
    if (empty($temps['phone'])) {
      $temps['phone'] = empty($row['Client_Phone2']) ? "" : $row['Client_Phone2'];
    }
    $temps['email'] = empty($row['Client_EMail1']) ? "" : $row['Client_EMail1'];
    if (empty($temps['email'])) {
      $temps['email'] = empty($row['Client_EMail2']) ? "" : $row['Client_EMail2'];
    }
    $temps['account_balance'] = intval($row['Client_Money']);
    $temps['coupon_balance'] = 0;
    $temps['point_balance'] = 0;
    $temps['status'] = 'default';
    $temps['updated_at'] = empty($row['Client_Renew_date']) ? NULL : $row['Client_Renew_date'];
    // TODO: $temps에 필요한 데이터 매핑 추가
    if (!$this->db->table('lineinfo')->insert($temps)) {
      throw new \Exception($this->db->error()['message']);
    }
    echo "{$cnt} -> {$temps['name']} Line완료.\n";
  }
}
