<?php

declare(strict_types=1);

namespace App\Models;

use App\Models\Traits\BelongsToTenant;
use Illuminate\Database\Eloquent\Concerns\HasUuids;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\SoftDeletes;

class Product extends Model
{
    use BelongsToTenant;
    use HasUuids;
    use SoftDeletes;

    protected $fillable = [
        'tenant_id', 'slug', 'name', 'description', 'category_id', 'brand', 'sku',
        'price', 'sale_price', 'currency', 'stock_qty', 'status', 'thumb_url', 'meta',
    ];

    protected $casts = [
        'meta' => 'array',
        'price' => 'integer',
        'sale_price' => 'integer',
        'stock_qty' => 'integer',
    ];

    public function category(): BelongsTo
    {
        return $this->belongsTo(ProductCategory::class, 'category_id');
    }

    public function variants(): HasMany
    {
        return $this->hasMany(ProductVariant::class);
    }

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

    public function effectivePrice(): int
    {
        return $this->sale_price ?? $this->price;
    }
}
