<?php

declare(strict_types=1);

namespace App\Repositories\Uploads;

use Illuminate\Support\Facades\DB;

final class ArtworkFileRepository
{
    private const SCREENSHOT_VARIANT_PREFIX = 'shot';

    public function upsert(int $artworkId, string $variant, string $path, string $mime, int $size): void
    {
        DB::table('artwork_files')->updateOrInsert(
            ['artwork_id' => $artworkId, 'variant' => $variant],
            ['path' => $path, 'mime' => $mime, 'size' => $size]
        );
    }

    public function deleteVariant(int $artworkId, string $variant): void
    {
        DB::table('artwork_files')
            ->where('artwork_id', $artworkId)
            ->where('variant', $variant)
            ->delete();
    }

    public function deleteScreenshotVariants(int $artworkId): void
    {
        DB::table('artwork_files')
            ->where('artwork_id', $artworkId)
            ->where('variant', 'like', self::SCREENSHOT_VARIANT_PREFIX . '%')
            ->delete();
    }
}
