<?php

namespace App\Domains\Catalog\Models;

use App\Domains\Attachable\Models\Attachment;
use Illuminate\Database\Eloquent\Attributes\Fillable;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\HasMany;

#[Fillable([
    'producto_id',
    'slug',
    'nombre',
    'stock',
    'descripcion',
    'precio',
])]
class ProductVariant extends Model
{
    use HasFactory;

    protected $table = 'productos_variantes';

    protected function casts(): array
    {
        return [
            'producto_id' => 'integer',
            'stock' => 'integer',
            'precio' => 'decimal:2',
        ];
    }

    /**
     * @return BelongsTo<Product, $this>
     */
    public function product(): BelongsTo
    {
        return $this->belongsTo(Product::class, 'producto_id');
    }

    /**
     * @return HasMany<ProductVariantDefinition, $this>
     */
    public function definitions(): HasMany
    {
        return $this->hasMany(ProductVariantDefinition::class, 'producto_variante_id');
    }

    /**
     * @return BelongsToMany<Attachment, $this>
     */
    public function attachments(): BelongsToMany
    {
        return $this->belongsToMany(
            Attachment::class,
            'variantes_attachments',
            'variante_id',
            'attachment_id'
        )->withTimestamps();
    }
}
