<?php

namespace App\Libraries\DBMigration\Process;

use App\Entities\Equipment\SwitchEntity;
use CodeIgniter\Database\BaseConnection;

class SwitchCodeProcess implements MigrationProcessInterface
{
  private $db;
  public function __construct(BaseConnection $db)
  {
    $this->db = $db;
  }
  public function process(array $row, int $cnt): void
  {
    $pattern = env("Switch.Prefix.code.pattern", false);
    if (!$pattern) {
      throw new \Exception(__METHOD__ . "에서 code의 prefix[Switch.Prefix.code.pattern]가 정의되지 않았습니다.");
    }
    if (!preg_match($pattern, $row['service_sw'])) {
      echo "{$cnt} {$row['service_sw']}-> SKIP SWITCHCODE\n";
      return;
    }
    $temps = [];
    $temps['code'] = trim($row['service_sw']);
    $temps['status'] = SwitchEntity::DEFAULT_STATUS; // Assuming SwitchEntity has a STATUS_DEFAULT constant
    if (!$this->db->table('switchinfo')->insert($temps)) {
      throw new \Exception($this->db->error()['message']);
    }
    echo "{$cnt} -> {$temps['code']} SWITCHCODE 완료.\n";
  }
}
