<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Support\Str;

class FundCollection extends Model
{
    protected $fillable = [
        'title',
        'description',
        'target_amount',
        'raised_amount',
        'cover_image',
        'gallery',
        'status',
        'slug',
        'bank_details',
        'benefits',
        'how_donations_help',
    ];

    protected $casts = [
        'target_amount' => 'decimal:2',
        'raised_amount' => 'decimal:2',
        'gallery' => 'array',
    ];

    public function donations(): HasMany
    {
        return $this->hasMany(Donation::class);
    }

    protected static function boot()
    {
        parent::boot();

        static::creating(function ($fundCollection) {
            if (empty($fundCollection->slug)) {
                $fundCollection->slug = Str::slug($fundCollection->title) . '-' . uniqid();
            }
        });
    }
}
