<?php

declare(strict_types=1);

namespace app\common\model;

use support\think\Model;

/**
 * 统一订单
 *
 * @property int $id
 * @property string $user_id
 * @property string $type
 * @property string $status
 * @property int $mall_item_id
 * @property int $points_cost
 * @property float $amount
 * @property int $multiplier
 * @property string $external_transaction_id
 * @property string $grant_status
 * @property string|null $fail_reason
 * @property int $retry_count
 * @property string $reject_reason
 * @property string $shipping_company
 * @property string $shipping_no
 * @property string $receiver_name
 * @property string $receiver_phone
 * @property string|null $receiver_address
 * @property int|null $mall_address_id
 */
class MallOrder extends Model
{
    protected string $name = 'mall_order';

    protected bool $autoWriteTimestamp = true;

    public const TYPE_BONUS = 'BONUS';
    public const TYPE_PHYSICAL = 'PHYSICAL';
    public const TYPE_WITHDRAW = 'WITHDRAW';

    public const STATUS_PENDING = 'PENDING';
    public const STATUS_COMPLETED = 'COMPLETED';
    public const STATUS_SHIPPED = 'SHIPPED';
    public const STATUS_REJECTED = 'REJECTED';

    public const GRANT_NOT_SENT = 'NOT_SENT';
    public const GRANT_SENT_PENDING = 'SENT_PENDING';
    public const GRANT_ACCEPTED = 'ACCEPTED';
    public const GRANT_FAILED_RETRYABLE = 'FAILED_RETRYABLE';
    public const GRANT_FAILED_FINAL = 'FAILED_FINAL';

    /** 非红利订单不参与 PlayX/Angpow 推送，固定为该占位值 */
    public const GRANT_NOT_APPLICABLE = '---';

    protected array $type = [
        'create_time' => 'integer',
        'update_time' => 'integer',
        'points_cost' => 'integer',
        'amount'      => 'float',
        'multiplier'  => 'integer',
        'retry_count'       => 'integer',
        'mall_address_id'   => 'integer',
    ];

    public function mallItem(): \think\model\relation\BelongsTo
    {
        return $this->belongsTo(MallItem::class, 'mall_item_id', 'id');
    }

    public function mallAddress(): \think\model\relation\BelongsTo
    {
        return $this->belongsTo(MallAddress::class, 'mall_address_id', 'id');
    }

    /**
     * 订单 user_id 存 playX 侧用户标识字符串，与 mall_user_asset.playx_user_id 对齐。
     */
    public function mallUserAsset(): \think\model\relation\BelongsTo
    {
        return $this->belongsTo(MallUserAsset::class, 'user_id', 'playx_user_id');
    }
}

