<?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 Bucket extends Model
{
    use HasFactory, SoftDeletes, ModelCounterTrait;

    protected $fillable = [
        'code',
        'title',
        'order',
        'board_id',
    ];

    // one to one relationship between Bucket and Board
    public function board() {
        return $this->belongsTo(Board::class);
    }

    // one to many relationship between Bucket and Tasks
    public function tasks() {
        return $this->hasMany(Task::class)->orderBy('order', 'asc');
    }

    // generate code for the bucket
    public static function booted(): void {
        static::creating(function(Bucket $bucket) {
            $bucket->code = $bucket->codeGenerator([
                "prefix" => "LST",
                "model" => "Bucket",
                "length" => 5,
            ]);
        });
    }
}
