<?php

declare(strict_types=1);

namespace Kylin\Retail\Models;

use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Kylin\Core\Models\BaseModel;

/**
 * 订单模型
 *
 * 依据《消费者权益保护法》第二十五条：
 * 七日无理由退货。
 *
 * @property int    $id
 * @property string $order_no       订单编号
 * @property int    $member_id      会员 ID
 * @property int    $total_amount   总金额（分）
 * @property string $pay_method     支付方式
 * @property string $paid_at        支付时间
 * @property string $status         状态
 */
class Order extends BaseModel
{
    protected $table = 'kylin_retail_orders';

    protected $fillable = [
        'order_no', 'member_id', 'total_amount',
        'pay_method', 'paid_at', 'status',
    ];

    protected $casts = [
        'total_amount' => 'integer',
        'paid_at' => 'datetime',
    ];

    public function items(): HasMany
    {
        return $this->hasMany(OrderItem::class, 'order_id');
    }

    public function member(): BelongsTo
    {
        return $this->belongsTo(Member::class, 'member_id');
    }
}