<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class Donor extends Model
{
    use HasFactory, SoftDeletes;

    protected $fillable = [
        'user_id', // optional link to registered user
        'first_name',
        'last_name',
        'email',
        'phone',
        'region_id',
        'school_id',
        'assigned_intern_id',
        'status', // new, contacted, pledged, donated, lapsed
        'notes',
        'custom_fields', // JSON for extra data
        'last_contacted_at',
        'total_donated',
        'source', // import, auction_winner, website, etc.
    ];

    protected $casts = [
        'custom_fields' => 'array',
        'last_contacted_at' => 'datetime',
        'total_donated' => 'decimal:2',
    ];

    public function region()
    {
        return $this->belongsTo(Region::class);
    }

    public function school()
    {
        return $this->belongsTo(School::class);
    }

    public function assignedIntern()
    {
        return $this->belongsTo(User::class, 'assigned_intern_id');
    }

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

    public function fullName(): string
    {
        return trim("{$this->first_name} {$this->last_name}");
    }
}
