<?php

namespace App\Models;

use App\Enums\OriginStatusType;
use App\Enums\PeriodType;
use App\Models\Traits\Filterable;
use App\Models\Traits\SelectOptions;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class Category extends Model
{
  use HasFactory, SoftDeletes, Filterable, SelectOptions;

  protected $fillable = [
    'name',
    'period_type',
    'status',
    'parent_id',
    'level',
  ];

  protected $casts = [
    'period_type' => PeriodType::class,
    'status' => OriginStatusType::class,
  ];

  public function userCategories()
  {
    return $this->hasMany(UserCategory::class, 'category_id');
  }

  public function users()
  {
    return $this->belongsToMany(User::class, 'user_categories', 'category_id', 'user_id');
  }

  public function parent()
  {
    return $this->belongsTo(Category::class, 'parent_id');
  }

  public function children()
  {
    return $this->hasMany(Category::class, 'parent_id');
  }
  public function budgetCategories()
  {
    return $this->hasMany(BudgetCategory::class);
  }
}
