<?php

namespace App\Services\Equipment\Part;

use App\Entities\Equipment\Part\IpEntity;
use App\Entities\Equipment\Part\LineEntity;
use App\Models\Equipment\Part\IpModel;
use App\Services\Equipment\Part\LineService;

class IpService extends PartService
{
    private ?LineService $_lineService = null;
    public function __construct(mixed $request = null)
    {
        parent::__construct($request);
        $this->addClassName('Ip');
    }
    public function getModelClass(): IpModel
    {
        return new IpModel();
    }
    public function getEntityClass(): IpEntity
    {
        return new IpEntity();
    }
    public function getFormFields(): array
    {
        return [
            "lineinfo_uid",
            "ip",
            "status",
        ];
    }
    public function getFilterFields(): array
    {
        return ["lineinfo_uid", 'status'];
    }
    public function getBatchJobFields(): array
    {
        return ['status'];
    }
    public function getIndexFields(): array
    {
        return ['lineinfo_uid', 'ip', 'status'];
    }
    final public function getLineService(): LineService
    {
        if (!$this->_lineService) {
            $this->_lineService = new LineService($this->request);
        }
        return $this->_lineService;
    }
    //기본기능
    //FieldForm관련용
    public function getFormFieldOption(string $field, $options = []): array
    {
        switch ($field) {
            case 'lineinfo_uid':
                $options = $this->getLineService()->getEntities();
                break;
            default:
                $options = parent::getFormFieldOption($field, $options);
                break;
        }
        return $options;
    }
    public function createByLineInfo(LineEntity $entity, string $ip): IpEntity
    {
        $formDatas                 = [];
        $formDatas['lineinfo_uid'] = $entity->getPK();
        $formDatas['ip']           = $ip;
        $formDatas['status']       = DEFAULTS['STATUS'];
        return $this->create($formDatas, new IpEntity());
    }

    //상태변경
    public function setStatus(int $uid, string $status): IpEntity
    {
        //code의 경우 사용가능/사용중으로 설정작업
        $entity = $this->getEntity($uid);
        if (!$entity) {
            throw new \Exception("{$uid}에 대한 IP정보를 찾을수 없습니다.");
        }
        return $this->getModel()->modify($entity, ['status' => $status]);
    }
}
