<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Attributes\Fillable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\MorphTo;

/**
 * Raw bytes: an upload, a generated image, something crawled.
 *
 * Deliberately not a creative. An upload only becomes advertising once it is
 * paired with copy, and the same asset can back several ads across several
 * campaigns.
 */
#[Fillable(['owner_type', 'owner_id', 'kind', 'source', 'path', 'original_name', 'mime', 'bytes', 'checksum'])]
class Asset extends Model
{
    public function owner(): MorphTo
    {
        return $this->morphTo();
    }

    public function platformRefs(): HasMany
    {
        return $this->hasMany(AssetPlatformRef::class);
    }

    /** Whether this asset is already on the given platform account. */
    public function refFor(string $platform, string $adAccountId): ?AssetPlatformRef
    {
        return $this->platformRefs()
            ->where('platform', $platform)
            ->where('ad_account_id', $adAccountId)
            ->first();
    }
}
