<?php
/* 
 * Proprietary Software © May 11, 2026
 * Developer: AhsanHabib (WhatsApp: +8801907738499)
 * All rights reserved.
 */

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Product extends Model
{
    use HasFactory;

    protected $guarded = [];
    public function getRouteKeyName()
    {
        return 'slug';
    }
    public function image()
    {
        return $this->hasOne(Productimage::class, 'product_id')->select('id', 'image', 'product_id');
    }
    public function images()
    {
        return $this->hasMany(Productimage::class, 'product_id')->select('id', 'image', 'product_id');
    }
    public function reviews()
    {
        return $this->hasMany(Review::class, 'product_id')->select('id');
    }
    public function category()
    {
        return $this->hasOne(Category::class, 'id', 'category_id')->select('id', 'name', 'slug');
    }
    public function subcategory()
    {
        return $this->hasOne(Subcategory::class, 'id', 'subcategory_id')->select('id', 'subcategoryName', 'slug');
    }
    public function childcategory()
    {
        return $this->hasOne(Childcategory::class, 'id', 'childcategory_id')->select('id', 'childcategoryName', 'slug');
    }
    public function brand()
    {
        return $this->hasOne(Brand::class, 'id', 'brand_id')->select('id', 'name', 'slug');
    }
    public function sizes()
    {
        return $this->belongsToMany('App\Models\Size', 'productsizes')->withTimestamps();
    }
    public function colors()
    {
        return $this->belongsToMany('App\Models\Color', 'productcolors')->withTimestamps();
    }

    public function prosizes()
    {
        return $this->hasMany('App\Models\Productsize');
    }
    public function procolors()
    {
        return $this->hasMany('App\Models\Productcolor');
    }

    public function prosize()
    {
        return $this->hasOne(Productsize::class, 'product_id');
    }
    public function procolor()
    {
        return $this->hasOne(Productcolor::class, 'product_id');
    }

    public function getNewPriceAttribute($value)
    {
        if (\Auth::guard('customer')->check() && \Auth::guard('customer')->user()->is_seller == 1) {
            $seller_discount = \Illuminate\Support\Facades\Cache::remember('seller_discount_percentage', 3600, function() {
                $setting = \App\Models\GeneralSetting::select('seller_discount_percentage')->where('status', 1)->first();
                return $setting ? (float) $setting->seller_discount_percentage : 0;
            });
            
            if ($seller_discount > 0) {
                $discount = $value * ($seller_discount / 100);
                return round($value - $discount);
            }
        }
        return $value;
    }

    public function getOldPriceAttribute($value)
    {
        if (\Auth::guard('customer')->check() && \Auth::guard('customer')->user()->is_seller == 1) {
            // Overwrite the normal 'old_price' to show the retail 'new_price' as crossed out
            if (isset($this->attributes['new_price'])) {
                return $this->attributes['new_price'];
            }
        }
        return $value;
    }
}
