<?php

namespace App\Models;

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

class TrackingExport extends Model
{
    use HasFactory;

    protected $fillable = [
        'device_id',
        'user_agent',
        'ip_address',
        'platform',
        'meme_id',
        'meme_media_id',
        'background_media_id',
        'caption_texts',
        'export_status',
        'error_message',
        'action_at',
        'completed_at',
    ];

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

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

    /**
     * Get the associated meme
     */
    public function meme(): BelongsTo
    {
        return $this->belongsTo(Meme::class);
    }

    /**
     * Get the associated meme media
     */
    public function memeMedia(): BelongsTo
    {
        return $this->belongsTo(MemeMedia::class, 'meme_media_id');
    }

    /**
     * Get the associated background media
     */
    public function backgroundMedia(): BelongsTo
    {
        return $this->belongsTo(BackgroundMedia::class, 'background_media_id');
    }

    /**
     * Scope for completed exports
     */
    public function scopeCompleted($query)
    {
        return $query->where('export_status', 'completed');
    }

    /**
     * Scope for failed exports
     */
    public function scopeFailed($query)
    {
        return $query->where('export_status', 'failed');
    }

    /**
     * Scope for processing exports
     */
    public function scopeProcessing($query)
    {
        return $query->where('export_status', 'processing');
    }
}
