<?php

namespace App\Models;

use App\Traits\ModelCounterTrait;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class Task extends Model
{
    use HasFactory, SoftDeletes, ModelCounterTrait;

    protected $fillable = [
        'code',
        'bucket_id',
        'user_id',
        'title',
        'description',
        'progress',
        'priority',
        'start_date',
        'due_date',
        'completed_date',
    ];

    // Relationship with Bucket
    public function bucket()
    {
        return $this->belongsTo(Bucket::class);
    }

    // relationship with User
    public function user()
    {
        return $this->belongsTo(User::class);
    }

    public static function booted(): void {
        static::creating(function(Task $task) {
            $task->code = $task->codeGenerator([
                "prefix" => "TCH",
                "model" => "Task",
                "length" => 5,
            ]);
        });
    }
}
