<?php

namespace App\Livewire\Studio\Catalog\Perfume;

use App\Enums\AuditStockStatus;
use App\Models\Outlet;
use App\Models\Perfume;
use Illuminate\Contracts\View\View;
use Illuminate\Database\Eloquent\Collection;
use Livewire\Attributes\Title;
use Livewire\Component;
use Spatie\Activitylog\Models\Activity;

#[Title('Audit')]
class Audit extends Component
{
    public string $label;

    public Collection $activityLogs;

    public function mount(Outlet $outlet, Perfume $perfume): void
    {
        $this->label = $perfume->name.' di '.$outlet->name;

        $this->activityLogs = Activity::where('subject_type', get_class($perfume))
            ->where('subject_id', $perfume->id)
            ->where('properties->outlet_id', $outlet->id)
            ->latest()
            ->get()
            ->map(function (Activity $activity) {
                $activity->event = [
                    'label' => $activity->event,
                    'color' => AuditStockStatus::from($activity->event)->color(),
                ];

                return $activity;
            });
    }

    public function render(): View
    {
        return view('components.sections.ui.audit', [
            'pageTitle' => 'Audit '.$this->label,
            'module' => 'perfume',
        ]);
    }
}
