<?php

namespace Modules\Product\Entities;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Modules\Product\Notifications\NotifyQuantityAlert;
use Spatie\MediaLibrary\HasMedia;
use Spatie\MediaLibrary\InteractsWithMedia;
use Spatie\MediaLibrary\MediaCollections\Models\Media;
use App\Models\ProductBatch;

class Product extends Model implements HasMedia
{

    use HasFactory, InteractsWithMedia;

    protected $guarded = [];

    protected $with = ['media'];

    protected $appends = ['product_quantity'];

    public function category() {
        return $this->belongsTo(Category::class, 'category_id', 'id');
    }

    public function batches() {
        return $this->hasMany(ProductBatch::class, 'product_id', 'id');
    }

    public function getProductQuantityAttribute() {
        return $this->batches()->sum('quantity');
    }

    public function registerMediaCollections(): void {
        $this->addMediaCollection('images')
            ->useFallbackUrl('/images/fallback_product_image.png');
    }

    public function registerMediaConversions(Media $media = null): void {
        $this->addMediaConversion('thumb')
            ->width(50)
            ->height(50);
    }

    public function setProductCostAttribute($value) {
        $this->attributes['product_cost'] = ($value * 100);
    }

    public function getProductCostAttribute($value) {
        return ($value / 100);
    }

    public function setProductPriceAttribute($value) {
        $this->attributes['product_price'] = ($value * 100);
    }

    public function getProductPriceAttribute($value) {
        return ($value / 100);
    }

    public function getWholesalePrice($quantity) {
        if ($this->min_quantity_for_wholesale && $quantity >= $this->min_quantity_for_wholesale) {
            $discount = $this->wholesale_discount_percentage / 100;
            return $this->product_price * (1 - $discount);
        }
        return $this->product_price;
    }

    public function isWholesalePrice($quantity) {
        return $this->min_quantity_for_wholesale && $quantity >= $this->min_quantity_for_wholesale;
    }
}
