<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Spatie\Activitylog\Traits\LogsActivity;
use Spatie\Activitylog\LogOptions;

class Transaction extends Model
{
    use HasFactory, SoftDeletes;
    use LogsActivity;

    public function getActivitylogOptions(): LogOptions
    {
        return LogOptions::defaults();
   }

    protected $fillable = [
        'number',
        'user_id',
        'customer_id',
        'address_id',
        'location_id',
        'time',
        'note',
        'amount',
        'shipping_price',
        'courier_company',
        'courier_type',
        'subtotal',
        'discount',
        'point',
        'status',
        'waybill_number',
    ];

    public function user()
    {
        return $this->belongsTo(User::class, 'user_id', 'id');
    }

    public function customer()
    {
        return $this->belongsTo(Customer::class, 'customer_id', 'id');
    }

    public function address()
    {
        return $this->belongsTo(Address::class, 'address_id', 'id');
    }

    public function vouchers()
    {
        return $this->morphMany(Voucher::class,'reference_used');
    }

    public function shipping()
    {
        return $this->hasOne(TransactionShipping::class);
    }

    public function location()
    {
        return $this->belongsTo(Location::class, 'location_id', 'id');
    }

    public function detailTransaction()
    {
        return $this->hasMany(TransactionDetail::class, 'transaction_id', 'id');
    }

    public function details()
    {
        return $this->hasMany(TransactionDetail::class, 'transaction_id', 'id');
    }

    public function xendits()
    {
        return $this->hasMany(TransactionPayment::class, 'transaction_id', 'id')
            ->where("method_type",XenditLink::class);
    }

    public function payments()
    {
        return $this->hasMany(TransactionPayment::class, 'transaction_id', 'id');
    }

    public function statuses()
    {
        return $this->hasMany(TransactionStatus::class, 'transaction_id', 'id');
    }

    public function invoice()
    {
        return $this->belongsTo(PosInvoice::class, 'invoice_id', 'id');
    }

}
