﻿<?php

namespace App\Models;

use App\Traits\ProductRelationship;
use App\Traits\WarehouseRelationship;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use App\Scopes\OrganisationScope;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Spatie\Activitylog\Traits\LogsActivity;
use Spatie\Activitylog\LogOptions;

/**
 * ProductStock
 */
class ProductStock extends Model
{
    use HasFactory, ProductRelationship, WarehouseRelationship;

    protected $fillable = [
        'organisation_id',
        'quantity',
        'product_id',
        'warehouse_id',
        'attribute_id',
        'attribute_item_id',
        'price',
        'customer_buying_price',
        'supplier_id',
        'shelf_id'
    ];

    use LogsActivity;

    public function getActivitylogOptions(): LogOptions
    {
        return LogOptions::defaults()
            ->logAll()
            ->useLogName('Product Stock')
            ->logOnlyDirty()
            ->setDescriptionForEvent(fn(string $eventName) => "Product stock ID #{$this->id} has been {$eventName}")
            ->dontSubmitEmptyLogs();
    }

    protected static function booted()
    {
        static::addGlobalScope(new OrganisationScope);

        // Automatically set the organisation_id before creating the model
        static::creating(function ($model) {
            if (auth()->check()) {
                $model->organisation_id = auth()->user()->organisation_id;
            }
        });
    }


    protected $appends = ['price_for_sale'];

    /**
     * attribute
     *
     * @return BelongsTo
     */
    public function attribute(): BelongsTo
    {
        return $this->belongsTo(Attribute::class);
    }

    /**
     * attributeItem
     *
     * @return BelongsTo
     */
    public function attributeItem(): BelongsTo
    {
        return $this->belongsTo(AttributeItem::class, 'attribute_item_id', 'id');
    }
    public function shelf(): BelongsTo
    {
        return $this->belongsTo(Shelf::class, 'shelf_id', 'id');
    }
    public function getPriceForSaleAttribute()
    {

        return ($this->customer_buying_price > 0 || $this->customer_buying_price != null) ?
            $this->customer_buying_price : (($this->price > 0 || $this->price != null) ? $this->price : (($this->product->customer_buying_price > 0 || $this->product->customer_buying_price != null) ? $this->product->customer_buying_price : $this->product->price));

        // if (auth()->guard('customer')->check()) {
        //     return ($this->customer_buying_price > 0 || $this->customer_buying_price != null) ?
        //         $this->customer_buying_price : (($this->price > 0 || $this->price != null) ? $this->price : (($this->product->customer_buying_price > 0 || $this->product->customer_buying_price != null) ? $this->product->customer_buying_price : $this->product->price));
        // } else {
        //     return ($this->price > 0 || $this->price != null) ? $this->price : (($this->product->price > 0 || $this->product->price != null) ? $this->product->price : 0);
        // }
    }
    public function warehouse()
    {
        return $this->belongsTo(Warehouse::class, 'warehouse_id');
    }