<?php

namespace App\Models;

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

class PriceAdjustmentDetail extends Model
{
    protected $fillable = [
        'price_adjustment_log_id',
        'product_id',
        'old_cost_price',
        'new_cost_price',
        'old_selling_price',
        'new_selling_price',
        'old_price_2',
        'new_price_2',
        'old_price_3',
        'new_price_3',
    ];

    protected $casts = [
        'old_cost_price' => 'decimal:2',
        'new_cost_price' => 'decimal:2',
        'old_selling_price' => 'decimal:2',
        'new_selling_price' => 'decimal:2',
        'old_price_2' => 'decimal:2',
        'new_price_2' => 'decimal:2',
        'old_price_3' => 'decimal:2',
        'new_price_3' => 'decimal:2',
    ];

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

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

    public function getCostChangeAttribute(): float
    {
        return $this->new_cost_price - $this->old_cost_price;
    }

    public function getSellingChangeAttribute(): float
    {
        return $this->new_selling_price - $this->old_selling_price;
    }

    public function getCostChangePercentAttribute(): float
    {
        if ($this->old_cost_price == 0) {
            return 0;
        }

        return (($this->new_cost_price - $this->old_cost_price) / $this->old_cost_price) * 100;
    }

    public function getSellingChangePercentAttribute(): float
    {
        if ($this->old_selling_price == 0) {
            return 0;
        }

        return (($this->new_selling_price - $this->old_selling_price) / $this->old_selling_price) * 100;
    }
}
