<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

/**
 * Polymorphic-style target attached to a Post.
 * For v1: target_type = 'artwork', target_id = artworks.id
 */
class PostTarget extends Model
{
    protected $table = 'post_targets';

    public $timestamps = false;

    protected $fillable = [
        'post_id',
        'target_type',
        'target_id',
    ];

    protected $casts = [
        'created_at' => 'datetime',
    ];

    const CREATED_AT = 'created_at';
    const UPDATED_AT = null;

    public function post(): BelongsTo
    {
        return $this->belongsTo(Post::class);
    }

    /** Resolved Artwork when target_type = 'artwork' */
    public function artwork(): BelongsTo
    {
        return $this->belongsTo(Artwork::class, 'target_id');
    }
}
