<?php

declare(strict_types=1);

namespace App\Services;

use App\Models\Artwork;
use App\Models\ArtworkAward;
use App\Models\ArtworkAwardStat;
use App\Models\User;

class ArtworkAwardService
{
    public function __construct(private readonly ArtworkMedalService $medals)
    {
    }

    /**
     * Award an artwork with the given medal.
     * Throws ValidationException if the user already awarded this artwork.
     */
    public function award(Artwork $artwork, User $user, string $medal): ArtworkAward
    {
        return $this->medals->award($artwork, $user, $medal);
    }

    /**
     * Change an existing award medal for a user/artwork pair.
     */
    public function changeAward(Artwork $artwork, User $user, string $medal): ArtworkAward
    {
        return $this->medals->changeMedal($artwork, $user, $medal);
    }

    /**
     * Remove an award for a user/artwork pair.
     * Uses model-level delete so the ArtworkAwardObserver fires.
     */
    public function removeAward(Artwork $artwork, User $user): void
    {
        $this->medals->removeMedal($artwork, $user);
    }

    /**
     * Recalculate and persist stats for the given artwork.
     */
    public function recalcStats(int $artworkId): ArtworkAwardStat
    {
        return $this->medals->recalculateStats($artworkId);
    }

    /**
     * Queue a non-blocking reindex for the artwork after award stats change.
     */
    public function syncToSearch(Artwork $artwork): void
    {
        $this->medals->syncArtworkToSearch((int) $artwork->id);
    }
}
