<?php

namespace App\Models\Tenancy;

use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Cache;

class Warehouse extends Model
{
    use HasFactory;

    protected $table = 'warehouse';

    protected $appends = [
        'price_old',
    ];

    protected $hidden = [
        'unit_id',
        'creator_id',
        'min_quantity',
        'action_date',
        'step',
        'category_id',
        'is_realization',
        'brand_name',
        'doctor_percentage',
        'administrative_percentage',
    ];

    public $timestamps = false;

    protected function priceOld(): Attribute
    {
        return Attribute::make(
            get: function (mixed $value, array $attributes) {
                $ccyRates = Cache::get(config('currencies.ccy_cache'));

                return $attributes['currency'] !== 'gel' ?
                    $attributes['realization_price'] * $ccyRates[strtoupper($attributes['currency'])]
                    :
                    $attributes['realization_price'];
            }
        );
    }

    public function scopeByProductCodes(Builder $query, array $productCode): Builder
    {
        return $query->whereIn('product_code', $productCode);
    }
}
