<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class ProductVariant extends Model
{
    protected $fillable = [
        'product_id', 'sku', 'price',
        'discount_price', 'stock', 'attributes', 'is_active',
    ];

    protected $casts = [
        'price'          => 'integer',
        'discount_price' => 'integer',
        'attributes'     => 'array',
        'is_active'      => 'boolean',
    ];

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

    public function getFinalPriceAttribute(): float
    {
        return $this->discount_price ?? $this->price;
    }
}
