<?php

namespace App\Models;

class ProductModel extends BaseCompanyModel
{
    protected $table = 'products';
    protected $primaryKey = 'id';
    protected $useTimestamps = true;
    protected $allowedFields = ['company_id', 'name', 'description', 'cost_price', 'price', 'currency', 'is_active'];

    /** هامش الربح لكل وحدة = سعر البيع - سعر الشراء */
    public function margin(array $product): float
    {
        return (float) $product['price'] - (float) ($product['cost_price'] ?? 0);
    }

    public function marginPercent(array $product): float
    {
        $price = (float) $product['price'];
        if ($price <= 0) return 0;
        return round(($this->margin($product) / $price) * 100, 1);
    }
}
