<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class VirtualTryonResult extends Model
{
    protected $table = 'virtual_tryon_results';

    protected $hidden = ['error', 'segmind_request_id', 'storage_path'];

    /**
     * `code`, `user_id` and `segmind_request_id` are intentionally excluded from
     * mass-assignment — they are authoritative identifiers set only by internal
     * code paths (controller / job), never by request input.
     */
    protected $fillable = [
        'product_id',
        'face_photo_id',
        'source_image',
        'result_image',
        'status',
        'error',
        'is_saved',
        'expires_at',
    ];

    protected $casts = [
        'is_saved' => 'boolean',
        'expires_at' => 'datetime',
    ];

    public function user()
    {
        return $this->belongsTo(User::class);
    }

    public function product()
    {
        return $this->belongsTo(Product::class);
    }

    public function facePhoto()
    {
        return $this->belongsTo(UserFacePhoto::class, 'face_photo_id');
    }

    public function scopeSaved($query)
    {
        return $query->where('is_saved', true);
    }

    public function scopeActive($query)
    {
        return $query->where('expires_at', '>', now());
    }
}
