<?php

namespace App\Models\Ecommerce\Product\Product\Concerns;

use Illuminate\Database\Eloquent\Casts\Attribute;
use Spatie\Sluggable\SlugOptions;

trait ProductTransformers
{
    /**
     * Get the options for generating the slug.
     */
    public function getSlugOptions(): SlugOptions
    {
        return SlugOptions::create()
            ->generateSlugsFrom('name')
            ->saveSlugsTo('slug');
    }

    /**
     * Get discount difference
     *
     * @return int|float
     */
    public function discountDifference(): Attribute
    {
        return Attribute::make(get: fn () => $this->caluculateDiscountDifference());
    }

    /**
     * Discount percentage
     */
    public function discountPercentage()
    {
        return number_format(($this->caluculateDiscountDifference() * 100) / $this->old_price_amount);
    }

    /**
     * Caluculate Discount Difference
     */
    public function caluculateDiscountDifference(int|float|null $price_amount = null): float
    {
        return $this->old_price_amount ? $this->old_price_amount - ($price_amount ?: $this->price_amount) : 0;
    }

    /**
     * Full name
     */
    public function fullName(): Attribute
    {
        return Attribute::make(
            get: function () {
                $brand = $this->brand ? "({$this->brand->name})" : '';

                return $this->name.$brand.$this->colour.$this->size;
            }
        );
    }

    /**
     * @param bool backorder
     */
    public function backOrder(): Attribute
    {
        return Attribute::make(
            get: fn () => $this->options->get('back_order')
        );
    }

    /**
     * @param string weight_value
     */
    public function weightValue(): Attribute
    {
        return Attribute::make(
            get: fn () => $this->options->get('weight_value')
        );
    }

    /**
     * @param string weight_unit
     */
    public function weightUnit(): Attribute
    {
        return Attribute::make(
            get: fn () => $this->options->get('weight_unit')
        );
    }

    /**
     * @param string height_value
     */
    public function heightValue(): Attribute
    {
        return Attribute::make(
            get: fn () => $this->options->get('height_value')
        );
    }

    /**
     * @param string height_unit
     */
    public function heightUnit(): Attribute
    {
        return Attribute::make(
            get: fn () => $this->options->get('height_unit')
        );
    }

    /**
     * @param string width_value
     */
    public function widthValue(): Attribute
    {
        return Attribute::make(
            get: fn () => $this->options->get('width_value')
        );
    }

    /**
     * @param string width_unit
     */
    public function widthUnit(): Attribute
    {
        return Attribute::make(
            get: fn () => $this->options->get('width_unit')
        );
    }

    /**
     * @param string depth_value
     */
    public function depthValue(): Attribute
    {
        return Attribute::make(
            get: fn () => $this->options->get('depth_value')
        );
    }

    /**
     * @param string depth_unit
     */
    public function depthUnit(): Attribute
    {
        return Attribute::make(
            get: fn () => $this->options->get('depth_unit')
        );
    }

    /**
     * @param string volume_value
     */
    public function volumeValue(): Attribute
    {
        return Attribute::make(
            get: fn () => $this->options->get('volume_value')
        );
    }

    /**
     * @param string volume_unit
     */
    public function volumeUnit(): Attribute
    {
        return Attribute::make(
            get: fn () => $this->options->get('volume_unit')
        );
    }

    /**
     * Flexible Content
     */
    // public function flexibleContent(): Attribute
    // {
    //     return Attribute::make(
    //         get: fn () => $this->flexible('flexible-content')
    //     );
    // }
}
