<?php

namespace App\Models;

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

class InnerOptionItem extends Model
{
    use HasFactory;

    protected $fillable = [
        'inner_option_set_id',
        'name',
        'price',
        'is_active',
        'sort_order',
    ];

    protected $casts = [
        'price' => 'decimal:2',
        'is_active' => 'boolean',
        'sort_order' => 'integer',
    ];

    protected $appends = ['price_modifier', 'is_default'];

    // Map price to price_modifier for frontend consistency
    public function getPriceModifierAttribute(): string
    {
        return $this->price ?? '0.00';
    }

    public function setPriceModifierAttribute($value): void
    {
        $this->attributes['price'] = $value;
    }

    // Default to false since column doesn't exist
    public function getIsDefaultAttribute(): bool
    {
        return false;
    }

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

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

    public function scopeOrdered($query)
    {
        return $query->orderBy('sort_order');
    }
}
