<?php

namespace App\Services\Equipment;

use App\Entities\Equipment\ServerEntity;
use App\Entities\Equipment\ServerPartEntity;
use App\Models\Equipment\ServerPartModel;
use App\Services\Equipment\EquipmentService;

class ServerPartService extends EquipmentService
{
    private ?PartService $_partService = null;
    public function __construct()
    {
        parent::__construct(new ServerPartModel());
        $this->addClassName('Server');
    }
    public function getFormFields(): array
    {
        return [
            'fields' => [
                "partinfo_uid",
                "serverinfo_uid",
                "serviceinfo_uid",
                "type",
                "billing",
                "amount",
                "cnt",
                "extra",
            ],
            'filters' => [
                "partinfo_uid",
                "serverinfo_uid",
                "serviceinfo_uid",
                "type"
            ],
        ];
    }
    public function getIndexFields(): array
    {
        return $this->getFormFields();
    }
    public function getBatchjobFields(): array
    {
        return ['partinfo_uid', "serverinfo_uid", "serviceinfo_uid",];
    }
    final public function getPartService(): PartService
    {
        if (!$this->_partService) {
            $this->_partService = new PartService();
        }
        return $this->_partService;
    }
    //partEntity 정보 추가
    protected function getEntity_process(mixed $entity): ServerPartEntity
    {
        $entity->setPartEntity($this->getPartService()->getEntity($entity->getPartInfoUID()));
        return $entity;
    }
    //기본 기능부분
    // FieldForm관련용
    public function getFormFieldOption(string $field, array $options = []): array
    {
        switch ($field) {
            case 'partinfo_uid_CPU':
                $options = $this->getPartService()->getEntities(['type' => 'CPU']);
                break;
            case 'partinfo_uid_RAM':
                $options = $this->getPartService()->getEntities(['type' => 'RAM']);
                break;
            case 'partinfo_uid_DISK':
                $options = $this->getPartService()->getEntities(['type' => 'DISK']);
                break;
            case 'partinfo_uid_OS':
                $options = $this->getPartService()->getEntities(['type' => 'OS']);
                break;
            case 'partinfo_uid_DB':
                $options = $this->getPartService()->getEntities(['type' => 'DB']);
                break;
            case 'partinfo_uid_SOFTWARE':
                $options = $this->getPartService()->getEntities(['type' => 'SOFTWARE']);
                break;
            case 'partinfo_uid': //수정때문에 전체가 필요
                $options = $this->getPartService()->getEntities();
                break;
            default:
                $options = parent::getFormFieldOption($field, $options);
                break;
        }
        return $options;
    }

    private function getFormDatasByServer(ServerEntity $serverEntity, string $partType, array $formDatas): array
    {
        $temps = [
            "partinfo_uid" => $formDatas["partinfo_uid_{$partType}"],
            "serverinfo_uid" => $serverEntity->getPK(),
            "serviceinfo_uid" => $serverEntity->getServiceInfoUID(),
            "type" => $partType,
            "billing" =>  $formDatas['billing'] ?? ServerPartENtity::DEFAULT_BILLING,
            "amount" => $formDatas["amount"] ?? 0,
            "cnt" => $formDatas["serverinfopartinfo_uid_{$partType}_cnt"] ?? 1,
            "extra" => $formDatas["serverinfopartinfo_uid_{$partType}_extra"] ?? ""
        ];
        return $temps;
    }
    //생성
    public function createByServer(ServerEntity $serverEntity, array $formDatas): array
    {
        $entities = [];
        foreach (SERVER['PARTTYPES'] as $partType) {
            $entities[] = parent::create(
                $this->getFormDatasByServer(
                    $serverEntity,
                    $partType,
                    $formDatas
                )
            );
        }
        return $entities;
    }
}
