<?php

namespace App\Models;

use App\Enums\TransactionType;
use App\Models\Traits\Filterable;
use App\Enums\TransactionStatusType;
use App\Models\Traits\SelectOptions;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Factories\HasFactory;

class Transaction extends Model
{
  use HasFactory, Filterable, SelectOptions;

  protected $fillable = [
    'budget_detail_id',
    'user_id',
    'vendor_id',
    'balance_id',
    'code',
    'name',
    'type',
    'description',
    'price',
    'price_total',
    'status',
    'status_message',
  ];

  protected $casts = [
    'type' => TransactionType::class,
    'status' => TransactionStatusType::class,
  ];

  public function quantityUnits()
  {
    return $this->hasMany(QuantityUnit::class);
  }

  public function unitType()
  {
    return $this->belongsTo(UnitType::class);
  }

  public function budgetDetail()
  {
    return $this->belongsTo(BudgetDetail::class, 'budget_detail_id');
  }

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

  public function vendor()
  {
    return $this->belongsTo(Vendor::class, 'vendor_id');
  }

  public function notifications()
  {
    return $this->hasMany(Notification::class, 'transaction_id');
  }

  public function attachments()
  {
    return $this->hasMany(Attachment::class, 'transaction_id');
  }

  public function payment()
  {
    return $this->hasOne(Payment::class, 'transaction_id');
  }
}
