<?php

namespace App\Models;

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

class Comment extends Model
{
    use HasFactory;

    protected $fillable = [
        'ticket_id',
        'user_id',
        'content',
        'is_internal',
        'is_resolution',
        'read_by_requester',
        'read_by_assigned',
    ];

    protected $casts = [
        'is_internal' => 'boolean',
        'is_resolution' => 'boolean',
        'read_by_requester' => 'boolean',
        'read_by_assigned' => 'boolean',
    ];

    /**
     * Get the ticket this comment belongs to
     */
    public function ticket()
    {
        return $this->belongsTo(Ticket::class);
    }

    /**
     * Get the user who wrote this comment
     */
    public function user()
    {
        return $this->belongsTo(User::class);
    }

    /**
     * Get the attachments associated with this comment
     */
    public function attachments()
    {
        return $this->hasMany(Attachment::class);
    }

    /**
     * Scope for public comments
     */
    public function scopePublic($query)
    {
        return $query->where('is_internal', false);
    }

    /**
     * Scope for internal comments
     */
    public function scopeInternal($query)
    {
        return $query->where('is_internal', true);
    }

    /**
     * Scope for resolution comments
     */
    public function scopeResolution($query)
    {
        return $query->where('is_resolution', true);
    }
}