<?php

namespace App\Models\Equipment;

use App\Entities\Equipment\LineEntity;

class LineModel extends EquipmentModel
{
    const TABLE = "lineinfo";
    const PK = "uid";
    const TITLE = "title";
    protected $table            = self::TABLE;
    protected $primaryKey       = self::PK;
    protected $returnType       = LineEntity::class;
    protected $allowedFields = [
        "clientinfo_uid",
        "type",
        "title",
        "bandwith",
        "status",
        "start_at",
        "updated_at"
    ];
    public function __construct()
    {
        parent::__construct();
    }
    public function getFieldRule(string $action, string $field): string
    {
        if (is_array($field)) {
            throw new \Exception(__FUNCTION__ . "=> field가 array 입니다.\n" . var_export($field, true));
        }
        switch ($field) {
            case "clientinfo_uid":
                $rule  = "if_exist|permit_empty|numeric";
                break;
            case "title":
            case "bandwith":
            case "type":
                $rule = "required|trim|string";
                break;
            default:
                $rule = parent::getFieldRule($action, $field);
                break;
        }
        return $rule;
    }
    protected function convertEntityData(string $field, array $formDatas, mixed $entity): mixed
    {
        switch ($field) {
            case 'clientinfo_uid':
                if (array_key_exists($field, $formDatas) && $formDatas[$field] !== '') {
                    $entity->$field = $formDatas[$field];
                }
                break;
            default:
                $entity = parent::convertEntityData($field, $formDatas, $entity);
                break;
        }
        return $entity;
    }
}
