<?php

namespace App\Models\Transaction;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use App\Traits\TenantScoped;
use App\Models\Machine\Machine;
use App\Models\Member\Member;

class Order extends Model
{
    use HasFactory, SoftDeletes, TenantScoped;

    protected $fillable = [
        'company_id',
        'flow_id',
        'order_no',
        'machine_id',
        'member_id',
        'total_amount',
        'discount_amount',
        'pay_amount',
        'payment_type',
        'payment_status',
        'payment_at',
        'status',
        'metadata',
    ];

    protected $casts = [
        'total_amount' => 'decimal:2',
        'discount_amount' => 'decimal:2',
        'pay_amount' => 'decimal:2',
        'payment_at' => 'datetime',
        'metadata' => 'array',
    ];

    public function machine()
    {
        return $this->belongsTo(Machine::class);
    }

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

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

    public function invoice()
    {
        return $this->hasOne(Invoice::class);
    }

    public function dispenseRecords()
    {
        return $this->hasMany(DispenseRecord::class);
    }
}
