<?php

namespace Modules\Product\Entities;

use App\Models\BaseModel;

class SectionItem extends BaseModel
{
    protected $guarded = [];

    protected $table = 'product_section_item';

    public function Section()
    {
        return $this->belongsTo(ModelSection::class, 'section_id');
    }

    public function calcPrice(): string
    {
        $price = $this->price;
        
        $product = $this->Section->Product ?? null;
        
        if($price > 0){
            if ($product->hasDiscount()) {
                if ($product->discountType() === 'percentage') {
                    $price -= ($price * $product->discountValue() / 100);
                } else {
                    $price -= $product->discountValue();
                }
            }
        }

        return number_format(
            $price * Country()->currancy_value,
            Country()->decimals,
            '.',
            ''
        );
    }


    public function price(): string
    {
        return number_format(
            $this->price * Country()->currancy_value,
            Country()->decimals,
            '.',
            ''
        );
    }
}
