<?php

namespace App\Models;

use App\Entities\PaymentEntity;

class PaymentModel extends CommonModel
{
    const TABLE = "payment";
    const PK = "uid";
    const TITLE = "title";
    protected $table            = self::TABLE;
    protected $primaryKey       = self::PK;
    protected $returnType       = PaymentEntity::class;
    protected $allowedFields = [
        "uid",
        "user_uid",
        "clientinfo_uid",
        "serviceinfo_uid",
        "serverpartinfo_uid",
        "title",
        "content",
        "amount",
        "billing",
        "billing_at",
        "pay",
        "status",
        "updated_at"
    ];
    public function __construct()
    {
        parent::__construct();
    }
    public function getFormRule(string $action, string $field): string
    {
        if (is_array($field)) {
            throw new \Exception(__FUNCTION__ . "=> field가 array 입니다.\n" . var_export($field, true));
        }
        switch ($field) {
            case "user_uid":
            case "clientinfo_uid":
            case "serviceinfo_uid":
            case "amount":
                $rule = "required|numeric";
                break;
            case "serverpartinfo_uid":
                $rule = "permit_empty|numeric";
                break;
            case "title":
            case "billing":
            case "pay":
            case "status":
                $rule = "required|trim|string";
                break;
            case "billing_at":
                $rule = "required|valid_date";
                break;
            case "content":
                $rule = "permit_empty|trim|string";
                break;
            default:
                $rule = parent::getFormRule($action, $field);
                break;
        }
        return $rule;
    }

    final public function create(array $formDatas): PaymentEntity
    {
        // 관리자 UID는 현재 인증된 사용자로 설정
        $formDatas['user_uid'] = $this->getMyAuth()->getUIDByAuthInfo();
        return parent::create($formDatas);
    }
}
