<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class TransactionItem extends Model
{
    protected $fillable = [
        'transaction_id', 'product_id', 'product_name', 'product_sku',
        'cost_price', 'unit_price', 'quantity', 'unit',
        'discount_percent', 'discount_amount', 'subtotal', 'notes',
    ];

    protected $casts = [
        'cost_price'       => 'decimal:2',
        'unit_price'       => 'decimal:2',
        'discount_percent' => 'decimal:2',
        'discount_amount'  => 'decimal:2',
        'subtotal'         => 'decimal:2',
    ];

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

    public function getProfitAttribute(): float {
        return ($this->unit_price - $this->cost_price) * $this->quantity;
    }
}
