<?php

declare(strict_types=1);

namespace App\Policies;

use App\Contracts\OwnedByUser;
use App\Enums\UserRole;
use App\Models\SellerProfile;
use App\Models\User;
use Illuminate\Auth\Access\Response;

/**
 * @extends OwnershipPolicy
 */
final class SellerProfilePolicy extends OwnershipPolicy
{
    /**
     * The public seller page is readable by anyone, including visitors, so the
     * public view is not gated here at all -- it is served by a resource that
     * exposes only trust signals. This ability covers the seller's *own* view,
     * which includes contact details and suspension state.
     */
    public function view(User $user, OwnedByUser $resource): Response
    {
        return parent::view($user, $resource);
    }

    /**
     * Only the owner may change their public identity, and only while the
     * account can transact. A suspended seller editing their display name is
     * how a bad actor sheds a reputation.
     */
    public function update(User $user, OwnedByUser $resource): Response
    {
        if (! $this->owns($user, $resource)) {
            return $this->notFound();
        }

        return $user->canTransact()
            ? Response::allow()
            : Response::deny('This account cannot currently make changes.');
    }

    /**
     * Listing suspension is a staff action, held at admin level rather than
     * support, because it stops a seller trading.
     */
    public function suspendListings(User $user, SellerProfile $profile): Response
    {
        return $user->hasAtLeastRole(UserRole::Admin)
            ? Response::allow()
            : Response::deny('You do not have permission to suspend a seller.');
    }

    /**
     * Risk state is never visible to the seller it concerns, so this is a
     * staff-only ability rather than an owner-or-staff one.
     */
    public function viewRiskSignals(User $user, SellerProfile $profile): Response
    {
        return $user->hasAtLeastRole(UserRole::Support)
            ? Response::allow()
            : $this->notFound();
    }
}
