<?php

declare(strict_types=1);

namespace App\Http\Api;

use App\Models\Like;
use App\Models\Notification;
use Illuminate\Support\Str;

class WatchApi
{
    public static function change(array $resId, string $from, string $message)
    {
        // 发送站内信
        $watches = Like::where('type', 'watch')
            ->whereIn('target_id', $resId)
            ->get();
        $notifications = [];
        foreach ($watches as $watch) {
            $notifications[] = [
                'id' => Str::uuid(),
                'from' => $from,
                'to' => $watch->user_id,
                'url' => '',
                'content' => $message,
                'res_type' => $watch->target_type,
                'res_id' => $watch->target_id,
                'channel' => Str::uuid(),
                'created_at' => now(),
                'updated_at' => now(),
            ];
        }
        $new = Notification::insert($notifications);

        return $new;
    }
}
