<?php

namespace App\Support;

use App\Models\InventoryItem;
use App\Models\RealtimeNotification;
use App\Models\ReplacementReport;
use App\Models\Room;
use App\Models\User;

class AdminRealtimeNotifier
{
    public static function scanRoom(Room $room, User $user): RealtimeNotification
    {
        return self::scanRoomByOfficer($room, $user->nama, ['officer_id' => $user->id]);
    }

    public static function scanRoomByOfficer(Room $room, string $officer, array $payload = []): RealtimeNotification
    {
        return self::create(
            'room_scan',
            'Scan QR Ruangan',
            $officer . ' memindai QR ' . $room->nama_ruangan . '.',
            route('reports.index', [], false),
            array_merge([
                'id_ruangan' => $room->id,
            ], $payload),
        );
    }

    public static function itemScan(InventoryItem $item, string $officer): RealtimeNotification
    {
        return self::create(
            'item_scan',
            'Scan Barang',
            $item->nama_barang . ' dipindai oleh ' . $officer . '.',
            route('reports.index', [], false),
            [
                'id_barang' => $item->id,
                'id_ruangan' => $item->id_ruangan,
            ],
        );
    }

    public static function replacementReport(ReplacementReport $report): RealtimeNotification
    {
        $report->loadMissing(['inventoryItem.room', 'user']);
        $item = $report->inventoryItem;
        $user = $report->user;
        $isReplacement = $report->jenis_laporan === 'penggantian';

        return self::create(
            $isReplacement ? 'replacement_request' : 'damage_report',
            $isReplacement ? 'Pengajuan Penggantian Barang' : 'Laporan Kerusakan Barang',
            ($user?->nama ?? 'Petugas') . ' mengirim ' . ($isReplacement ? 'pengajuan penggantian' : 'laporan kerusakan') . ' untuk ' . $item->nama_barang . ' di ' . $item->room->nama_ruangan . '.',
            route('replacements.index', [], false),
            [
                'id_laporan' => $report->id,
                'id_barang' => $item->id,
                'id_ruangan' => $item->id_ruangan,
                'jenis_laporan' => $report->jenis_laporan,
            ],
        );
    }

    public static function replacementRequest(InventoryItem $item, string $officer = 'Petugas Mobile'): RealtimeNotification
    {
        $item->loadMissing('room');

        return self::create(
            'replacement_request',
            'Pengajuan Penggantian Barang',
            $officer . ' mengajukan penggantian untuk ' . $item->nama_barang . ' di ' . $item->room->nama_ruangan . '.',
            route('replacements.index', [], false),
            [
                'id_barang' => $item->id,
                'id_ruangan' => $item->id_ruangan,
            ],
        );
    }

    public static function create(string $type, string $title, string $message, ?string $url = null, array $payload = []): RealtimeNotification
    {
        return RealtimeNotification::create([
            'jenis' => $type,
            'judul' => $title,
            'pesan' => $message,
            'url' => $url,
            'data' => $payload ?: null,
        ]);
    }
}
