<?php

namespace App\Helpers;
use App\Models\Notifications;
use InvalidArgumentException;

class NotificationHelper
{
    public static function newExpense($expense_type, $expense_id, $user_id, $expense_number, $receiver_ids = [])
	{
		if (!is_array($receiver_ids) || empty($receiver_ids)) {
			throw new InvalidArgumentException('Receiver IDs must be a non-empty array.');
		}

		// Buat data untuk batch insert
		$notifications = [];
		foreach ($receiver_ids as $receiver_id) {
			$notifications[] = [
				'user_id' => $user_id,
				'receiver_id' => $receiver_id,
				'title' => 'New Expense',
				'content' => 'You have new expense waiting for approval (' . $expense_number . ')',
				'is_read' => 0,
				'type' => 'new_expense',
				'expense_type' => $expense_type,
				'expense_id' => $expense_id,
				'created_at' => now(),
				'updated_at' => now(),
			];
		}

		// Lakukan batch insert
		Notifications::insert($notifications);
	}

	public static function approveExpense($expense_type, $expense_id, $user_id, $expense_number, $receiver_ids = [])
    {
		if (!is_array($receiver_ids) || empty($receiver_ids)) {
			throw new InvalidArgumentException('Receiver IDs must be a non-empty array.');
		}

		// Buat data untuk batch insert
		$notifications = [];
		foreach ($receiver_ids as $receiver_id) {
			$notifications[] = [
				'user_id' => $user_id,
				'receiver_id' => $receiver_id,
				'title' => 'New Expense',
				'content' => 'You have new expense waiting for second approval (' . $expense_number . ')',
				'is_read' => 0,
				'type' => 'approve_expense',
				'expense_type' => $expense_type,
				'expense_id' => $expense_id,
				'created_at' => now(),
				'updated_at' => now(),
			];
		}

		// Lakukan batch insert
		Notifications::insert($notifications);
    }

	public static function approve2Expense($expense_type, $expense_id, $user_id, $expense_number, $receiver_ids = [])
    {
		if (!is_array($receiver_ids) || empty($receiver_ids)) {
			throw new InvalidArgumentException('Receiver IDs must be a non-empty array.');
		}

		// Buat data untuk batch insert
		$notifications = [];
		foreach ($receiver_ids as $receiver_id) {
			$notifications[] = [
				'user_id' => $user_id,
				'receiver_id' => $receiver_id,
				'title' => 'New Expense',
				'content' => 'You have new expense waiting for final approval (' . $expense_number . ')',
				'is_read' => 0,
				'type' => 'approve2_expense',
				'expense_type' => $expense_type,
				'expense_id' => $expense_id,
				'created_at' => now(),
				'updated_at' => now(),
			];
		}

		// Lakukan batch insert
		Notifications::insert($notifications);
    }
}
