<?php

namespace App\Models;

use App\Helpers\Price;
use App\Models\Scopes\YearScope;
use Illuminate\Database\Eloquent\Attributes\ScopedBy;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\SoftDeletes;


#[ScopedBy([YearScope::class])]
class Product extends Model
{
    use SoftDeletes;

    const DEFAULT_SORT_BY = 'article';

    protected $fillable = [
        'year',
        'article',
        'name_tz',
        'type_tz',
        'nomenclature_number',
        'sizes',
        'manufacturer',
        'unit',
        'type',
        'product_price',
        'installation_price',
        'total_price',
        'manufacturer_name',
        'note',
        'certificate_id',
    ];

    // set year attribute to current selected year
    protected static function boot(): void
    {
        parent::boot();
        static::creating(function($attributes) {
            if(!isset($attributes->year)) {
                $attributes->year = year();
            }
        });
    }

    protected $appends = [
        'product_price',
        'installation_price',
        'total_price',
        'product_price_txt',
        'installation_price_txt',
        'total_price_txt',
        'image'
    ];

    protected function productPrice(): Attribute
    {
        return Attribute::make(
            get: fn(int $value) => (float) $value / 100,
            set: fn (float $value) => (int) ($value * 100),
        );
    }

    protected function installationPrice(): Attribute
    {
        return Attribute::make(
            get: fn(int $value) => (float) $value / 100,
            set: fn (float $value) => (int) ($value * 100),
        );
    }


    protected function totalPrice(): Attribute
    {
        return Attribute::make(
            get: fn(int $value) => (float) $value / 100,
            set: fn (float $value) => (int) ($value * 100),
        );
    }

    public function productPriceTxt(): Attribute
    {
        return Attribute::make(
            get: fn($value) => Price::format($this->product_price),
        );
    }

    public function installationPriceTxt(): Attribute
    {
        return Attribute::make(
            get: fn($value) => Price::format($this->installation_price),
        );
    }


    protected function totalPriceTxt(): Attribute
    {
        return Attribute::make(
            get: fn($value) => Price::format($this->total_price),
        );
    }

    public function commonName(): Attribute
    {
        return Attribute::make(
            get: fn($value) =>  $this->article . ', №' . $this->nomenclature_number,
        );
    }


    /**
     * @return BelongsToMany
     */
    public function orders(): BelongsToMany
    {
        return $this->belongsToMany(Order::class, 'products_sku', 'order_id', 'product_id');
    }

    public function certificate(): BelongsTo
    {
        return $this->belongsTo(File::class, 'certificate_id', 'id');
    }

    public function maf_orders(): HasMany
    {
        return $this->hasMany(MafOrder::class, 'product_id', 'id');
    }

    public function hasRelations(): bool
    {
        if($this->maf_orders && ($this->maf_orders->count() > 0)) {
            return true;
        }
        
        if($this->orders && ($this->orders->count() > 0)) {
            return true;
        }

        return false;
    }

    public function image(): Attribute
    {
        if(file_exists(public_path() . '/images/main/' . $this->article . '.0000.0000.jpg')) {

            $path =  url('/images/main/' . $this->article . '.0000.0000.jpg');
        } else {
            $path =  '';
        }
        return Attribute::make(
            get: fn() =>  $path,
        );
    }


}
