<?php

namespace lib\Services;

use lib\Entities\ServerEntity as Entity;
use lib\Models\ServerModel as Model;

class ServerService extends CommonService
{
  public function __construct()
  {
    parent::__construct();
  }
  final public function getClassName(): string
  {
    return "Server";
  }
  final public function getClassPath(): string
  {
    return $this->getClassName();
  }
  public function getModelClass(): string
  {
    return Model::class;
  }
  public function getEntityClass(): string
  {
    return Entity::class;
  }
  public function getCountByMode(string $mode, string $cpu, ?string $spec = null): int
  {
    switch ($mode) {
      case 'all':
        $this->getModel()->like(["server_cpuname" => "%$cpu%", "server_spec" => "%$spec%"]);
        break;
      case 'use':
        $this->getModel()->where("server_use_status", "n");
        $this->getModel()->like(["server_cpuname" => "%$cpu%", "server_spec" => "%$spec%"]);
        break;
      case 'empty':
        $this->getModel()->where("server_use_status", "y");
        $this->getModel()->like(["server_cpuname" => "%$cpu%", "server_spec" => "%$spec%"]);
        break;
      case 'format':
        $this->getModel()->where("server_use_status", "y");
        $this->getModel()->where("server_fomat_date !='NULL'");
        $this->getModel()->like(["server_cpuname" => "%$cpu%"]);
        break;
      default:
        throw new \InvalidArgumentException("Invalid mode: $mode");
    }
    return $this->getCount();
  }
}
