<?php



namespace App\Models;



use App\Models\Concerns\BelongsToCompany;

use Illuminate\Database\Eloquent\Model;

use Illuminate\Database\Eloquent\Relations\BelongsTo;



class Expense extends Model

{

    use BelongsToCompany;



    protected $table = 'expenses';



    protected $fillable = [

        'company_id',

        'user_id',

        'submitted_by',

        'account_id',

        'vendor_id',

        'category',

        'amount',

        'date',

        'description',

        'status',

        'deduct_from_payroll',

        'payroll_cycle_id',

        'reviewed_by',

        'review_remarks',

        'reviewed_at',

    ];



    protected $casts = [

        'amount' => 'decimal:2',

        'date' => 'date',

        'deduct_from_payroll' => 'boolean',

        'reviewed_at' => 'datetime',

    ];



    public function account(): BelongsTo

    {

        return $this->belongsTo(Account::class);

    }



    public function vendor(): BelongsTo

    {

        return $this->belongsTo(Vendor::class);

    }



    public function claimant(): BelongsTo

    {

        return $this->belongsTo(User::class, 'user_id');

    }



    public function submitter(): BelongsTo

    {

        return $this->belongsTo(User::class, 'submitted_by');

    }



    public function reviewer(): BelongsTo

    {

        return $this->belongsTo(User::class, 'reviewed_by');

    }



    public function statusLabel(): string

    {

        return str_replace('_', ' ', ucfirst((string) ($this->status ?? '')));

    }



    public function statusBadgeVariant(): string
    {
        return \App\Support\StatusBadge::variant($this->status);
    }



    public function isLocked(): bool
    {
        return in_array($this->status, ['paid', 'rejected'], true);
    }

    public function isEditableByClaimant(): bool

    {

        return $this->status === 'pending';

    }

}


