<?php

declare(strict_types=1);

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\HasMany;

final class Tag extends Model
{
    use HasFactory;

    protected $table = 'tags';

    protected $fillable = [
        'name',
        'slug',
        'usage_count',
        'artworks_count',
        'is_active',
    ];

    protected $casts = [
        'usage_count' => 'integer',
        'artworks_count' => 'integer',
        'is_active' => 'boolean',
    ];

    public function artworks(): BelongsToMany
    {
        return $this->belongsToMany(Artwork::class, 'artwork_tag', 'tag_id', 'artwork_id')
            ->withPivot(['source', 'confidence']);
    }

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

    public function getRouteKeyName(): string
    {
        return 'slug';
    }
}
