<?php

namespace App\Models;

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

class TrackingContentSelection extends Model
{
    use HasFactory;

    protected $fillable = [
        'device_id',
        'user_agent',
        'ip_address',
        'platform',
        'content_type',
        'content_id',
        'content_name',
        'search_query',
        'selection_method',
        'action_at',
    ];

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

    protected $attributes = [
        'platform' => 'web',
    ];

    /**
     * Get the related content (polymorphic relationship)
     */
    public function content()
    {
        if ($this->content_type === 'meme') {
            return $this->belongsTo(MemeMedia::class, 'content_id');
        }
        
        if ($this->content_type === 'background') {
            return $this->belongsTo(BackgroundMedia::class, 'content_id');
        }
        
        return null;
    }
}