<?php

namespace App\Models;

use App\Enums\PaymentMethodType;
use App\Enums\PaymentStatusType;
use App\Enums\PaymentType;
use App\Models\Traits\Filterable;
use App\Models\Traits\SelectOptions;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

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

  protected $fillable = [
    'company_id',
    'department_id',
    'budget_id',
    'user_review_id',
    'user_bank_id',
    'company_bank_id',
    'transaction_id',
    'code',
    'type',
    'method',
    'amount',
    'payment_date',
    'due_date',
    'cheque_number',
    'description',
    'status',
    'status_message',
    'review_at',
    'max_amount',
    'payment_percentage',
  ];

  protected $casts = [
    'type' => PaymentType::class,
    'method' => PaymentMethodType::class,
    'status' => PaymentStatusType::class,
    'payment_date' => 'date',
    'due_date' => 'date',
    'review_at' => 'datetime',
  ];

  public function company()
  {
    return $this->belongsTo(Company::class, 'company_id');
  }

  public function department()
  {
    return $this->belongsTo(Department::class, 'department_id');
  }

  public function budget()
  {
    return $this->belongsTo(Budget::class, 'budget_id');
  }

  public function transaction()
  {
    return $this->belongsTo(Transaction::class, 'transaction_id');
  }

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

  public function userBank()
  {
    return $this->belongsTo(UserBank::class, 'user_bank_id');
  }

  public function companyBank()
  {
    return $this->belongsTo(UserBank::class, 'company_bank_id');
  }

  public function approvalLogs()
  {
    return $this->hasMany(ApprovalLog::class, 'payment_id');
  }

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

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