<?php

declare(strict_types=1);

namespace App\Filament\Resources\BaselineSnapshotResource\Pages;

use App\Filament\Resources\BaselineSnapshotResource;
use App\Models\User;
use App\Models\Workspace;
use App\Services\Auth\WorkspaceCapabilityResolver;
use App\Support\Auth\Capabilities;
use Filament\Resources\Pages\ListRecords;

class ListBaselineSnapshots extends ListRecords
{
    protected static string $resource = BaselineSnapshotResource::class;

    protected function authorizeAccess(): void
    {
        $user = auth()->user();
        $workspace = BaselineSnapshotResource::resolveWorkspace();

        if (! $user instanceof User || ! $workspace instanceof Workspace) {
            abort(404);
        }

        /** @var WorkspaceCapabilityResolver $resolver */
        $resolver = app(WorkspaceCapabilityResolver::class);

        if (! $resolver->isMember($user, $workspace)) {
            abort(404);
        }

        if (! $resolver->can($user, $workspace, Capabilities::WORKSPACE_BASELINES_VIEW)) {
            abort(403);
        }
    }

    protected function getHeaderActions(): array
    {
        return [];
    }
}
