<?php

namespace App\Policies;

use App\Models\ConversationParticipant;
use App\Models\Message;
use App\Models\User;

class MessagePolicy
{
    public function view(User $user, Message $message): bool
    {
        return ConversationParticipant::query()
            ->where('conversation_id', $message->conversation_id)
            ->where('user_id', $user->id)
            ->whereNull('left_at')
            ->exists();
    }

    public function update(User $user, Message $message): bool
    {
        return $message->sender_id === $user->id && $message->deleted_at === null;
    }

    public function delete(User $user, Message $message): bool
    {
        return $message->sender_id === $user->id || $user->isAdmin();
    }
}
