<?php

namespace Tests\Feature;

use App\Models\Purchase;
use App\Models\User;
use App\Notifications\PurchaseStatusNotification;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Spatie\Permission\Models\Role;
use Tests\TestCase;

class PurchaseStatusNotificationTest extends TestCase
{
    use RefreshDatabase;

    private function makePurchase(int $id, string $poNumber): Purchase
    {
        $purchase = new Purchase(['po_number' => $poNumber]);
        $purchase->id = $id;

        return $purchase;
    }

    public function test_to_array_url_points_to_portal_supplier_for_supplier_recipient(): void
    {
        Role::firstOrCreate(['name' => 'supplier', 'guard_name' => 'web']);
        $supplierUser = User::factory()->create();
        $supplierUser->assignRole('supplier');

        $purchase = $this->makePurchase(15, 'PO-0015');

        $data = (new PurchaseStatusNotification($purchase, 'sent', 'PO baru diterima', 'Budi'))
            ->toArray($supplierUser);

        $this->assertSame('/portal-supplier?id=15', $data['url']);
    }

    public function test_to_array_url_points_to_pembelian_for_non_supplier_recipient(): void
    {
        Role::firstOrCreate(['name' => 'manager', 'guard_name' => 'web']);
        $manager = User::factory()->create();
        $manager->assignRole('manager');

        $purchase = $this->makePurchase(15, 'PO-0015');

        $data = (new PurchaseStatusNotification($purchase, 'confirmed', 'PO dikonfirmasi', 'Sari'))
            ->toArray($manager);

        $this->assertSame('/pembelian?id=15', $data['url']);
    }
}
