<?php

namespace App\Filament\Resources\Workspaces\Pages;

use App\Filament\Resources\Workspaces\WorkspaceResource;
use App\Models\User;
use App\Services\Audit\WorkspaceAuditLogger;
use Filament\Resources\Pages\EditRecord;

class EditWorkspace extends EditRecord
{
    protected static string $resource = WorkspaceResource::class;

    protected function afterSave(): void
    {
        $user = auth()->user();

        app(WorkspaceAuditLogger::class)->log(
            workspace: $this->record,
            action: 'workspace.updated',
            actor: $user instanceof User ? $user : null,
            resourceType: 'workspace',
            resourceId: (string) $this->record->getKey(),
            context: [
                'metadata' => [
                    'workspace_id' => (int) $this->record->getKey(),
                    'slug' => (string) $this->record->slug,
                ],
            ],
        );
    }
}
