<?php

namespace app\common\model;

use app\common\model\traits\TimestampInteger;
use support\think\Model;

/**
 * MallAddress
 */
class MallAddress extends Model
{
    use TimestampInteger;

    // 表名
    protected $name = 'mall_address';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = true;

    /** 地区字段已废弃，接口与后台不再使用 */
    protected array $hidden = ['region'];

    protected static function onBeforeInsert($model): void
    {
        $model->set('region', '');
    }

    protected static function onBeforeUpdate($model): void
    {
        $model->set('region', '');
    }

    public function getregionAttr($value): string
    {
        return trim(strval($value ?? ''));
    }

    public function setregionAttr($value): string
    {
        return '';
    }

    public function playxUserAsset(): \think\model\relation\BelongsTo
    {
        return $this->belongsTo(\app\common\model\MallUserAsset::class, 'playx_user_asset_id', 'id');
    }

    /**
     * 实物订单收货快照（写入 mall_order.receiver_*，与 mall_address 当前内容一致）
     *
     * @return array{receiver_name: string, receiver_phone: string, receiver_address: string}
     */
    public static function snapshotForPhysicalOrder(self $addr): array
    {
        return [
            'receiver_name'    => trim($addr->receiver_name ?? ''),
            'receiver_phone'   => trim($addr->phone ?? ''),
            'receiver_address' => trim($addr->detail_address ?? ''),
        ];
    }
}
