<?php

namespace App\Libraries\DBMigration\Process;

use CodeIgniter\Database\BaseConnection;

class PartCodeProcess implements MigrationProcessInterface
{
  private $db;
  public function __construct(BaseConnection $db)
  {
    $this->db = $db;
  }
  public function process(array $row, int $cnt): void
  {
    try {
      if (count($row) < 2) {
        throw new \Exception("{$cnt} {$row[0]}:{$row[1]} -> SKIP PARTCODE\n");
      }

      $temps = [];
      $temps['title']  = trim($row[1]);
      $temps['price']  = trim($row[2]);
      $temps['stock']  = 100;
      $temps['status'] = "available";
      switch (trim($row[0])) {
        case 'CPU':
          $table = 'cpuinfo';
          break;
        case 'RAM':
          $table = 'raminfo';
          break;
        case 'DISK':
          $table = 'diskinfo';
          break;
        case 'OS':
          $table = 'osinfo';
          break;
        case 'SOFTWARE':
          $table = 'softwareinfo';
          break;
      }
      $result = $this->db->table($table)->insert($temps);
      if (!$result) {
        throw new \Exception($this->db->error()['message']);
      }
      echo "{$cnt} -> {$temps['title']} 완료.\n";
    } catch (\Exception $e) {
      echo $e->getMessage();
    }
  }
}
