<?php

namespace App\Models;

use App\Entities\OrderBillingEntity;

class OrderBillingModel extends BaseModel
{
    protected $table = "tw_orderbilling";
    protected $returnType = OrderBillingEntity::class;
    public function __construct()
    {
        parent::__construct('Billing');
        $this->allowedFields = [
            ...$this->allowedFields,  "order_uid", "billing_uid"
        ];
        $this->validationRules = [...$this->validationRules, ...$this->getFieldRules($this->allowedFields),];
    }
    final public function getTitleField(): string
    {
        return 'billing_uid';
    }
    public function getFieldRule(string $field, array $rules, string $action = ""): array
    {
        switch ($field) {
            case "order_uid":
                $rules[$field] = $this->getUUIDFieldRule('required');
                break;
            case 'billing_uid':
                $rules[$field] = "required|numeric";
                break;
            default:
                $rules = parent::getFieldRule($field, $rules, $action);
                break;
        }
        return $rules;
    }
    public function getEntity($conditions): OrderBillingEntity
    {
        return $this->where($conditions)->first() ?: throw new \Exception(__FUNCTION__ . "에서 {$this->getClassName()}의 해당 데이터가 없습니다.");
    }
    public function create(array $formDatas): OrderBillingEntity
    {
        return $this->create_process(new OrderBillingEntity(), $formDatas);
    }
    public function modify(OrderBillingEntity $entity, array $formDatas): OrderBillingEntity
    {
        return $this->modify_process($entity, $formDatas);
    }
}
