<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

class ManagementAlert extends Model
{
    public const STATUSES = ['open', 'acknowledged', 'resolved'];
    public const TYPES = [
        'large_loan','large_expense','overdue_threshold','low_account_balance','cash_shortage',
        'pending_approval','write_off_request','collateral_ready_for_release','expiring_documents',
    ];

    protected $fillable = [
        'business_id','branch_id','type','severity','title','message','status','source_type','source_id','alert_key',
        'occurred_at','metadata','acknowledged_by','acknowledged_at','resolved_by','resolved_at','resolution_note',
    ];

    protected function casts(): array
    {
        return ['occurred_at'=>'datetime','metadata'=>'array','acknowledged_at'=>'datetime','resolved_at'=>'datetime'];
    }

    public function business(): BelongsTo { return $this->belongsTo(Business::class); }
    public function branch(): BelongsTo { return $this->belongsTo(Branch::class); }
    public function acknowledger(): BelongsTo { return $this->belongsTo(User::class, 'acknowledged_by'); }
    public function resolver(): BelongsTo { return $this->belongsTo(User::class, 'resolved_by'); }
}