<?php

namespace App\Models;

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

class Product extends Model
{
    use HasFactory;
    use SoftDeletes;

    protected $fillable = [
        'name_ar',
        'name_en',
        'details_ar',
        'details_en',
        'cost_price',
        'sale_price',
        'has_color',
        'has_size',
        'has_measurement',
    ];

    protected $casts = [
        'has_color' => 'boolean',
        'has_size' => 'boolean',
        'has_measurement' => 'boolean',
        'cost_price' => 'integer',
        'sale_price' => 'integer',
    ];

    public function getNameAttribute()
    {
        return app()->getLocale() == 'ar' ? $this->name_ar : $this->name_en;
    }

    public function getDetailsAttribute()
    {
        return app()->getLocale() == 'ar' ? $this->details_ar : $this->details_en;
    }

    public function images()
    {
        return $this->hasMany(ProductImage::class);
    }

    public function colors()
    {
        return $this->belongsToMany(ProductColor::class, 'product_color_stocks', 'product_id', 'color_id')
            ->withPivot('stock')
            ->withTimestamps();
    }

    public function sizes()
    {
        return $this->belongsToMany(ProductSize::class, 'product_size_stocks', 'product_id', 'size_id')
            ->withPivot('stock')
            ->withTimestamps();
    }

    public function measurements()
    {
        return $this->belongsToMany(ProductMeasurement::class, 'product_measurement_stocks', 'product_id', 'measurement_id')
            ->withPivot('stock')
            ->withTimestamps();
    }

    public function inventoryLogs()
    {
        return $this->hasMany(ProductInventoryLog::class);
    }
}
