<?php

namespace App\Models;

use App\Enums\NoteVisibility;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

class ApplicationNote extends Model
{
    protected $fillable = [
        'application_id', 'user_id', 'note', 'visibility', 'requires_response', 'resolved_at',
    ];

    protected function casts(): array
    {
        return [
            'visibility' => NoteVisibility::class,
            'requires_response' => 'boolean',
            'resolved_at' => 'datetime',
        ];
    }

    public function application(): BelongsTo
    {
        return $this->belongsTo(Application::class);
    }

    public function author(): BelongsTo
    {
        return $this->belongsTo(User::class, 'user_id');
    }

    public function scopeVisibleToApplicant(Builder $query): Builder
    {
        return $query->where('visibility', NoteVisibility::Applicant);
    }

    public function scopeInternal(Builder $query): Builder
    {
        return $query->where('visibility', NoteVisibility::Internal);
    }
}
