<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Str;

class CreativeArt extends Model
{
    protected $table = 'art';

    protected $fillable = [
        'slug', 'artist_id', 'painting_category_id', 'name_en', 'medium',
        'width', 'height', 'year', 'price', 'old_price', 'description',
        'about_art', 'shipping_package', 'Certificate_of_Authenticity',
        'return_refund_policy', 'is_delete', 'status', 'art_unique_id'
    ];

    protected $casts = [
        'is_delete' => 'integer',
        'status' => 'integer',
        'price' => 'integer',
        'old_price' => 'integer',
        'artist_id' => 'integer',
        'painting_category_id' => 'integer',
    ];

    protected static function boot()
    {
        parent::boot();

        static::creating(function ($art) {
            if (empty($art->slug)) {
                $art->slug = Str::slug($art->name_en);
            }
            if (empty($art->art_unique_id)) {
                $art->art_unique_id = 'ART-' . strtoupper(Str::random(8));
            }
        });

        static::updating(function ($art) {
            if ($art->isDirty('name_en')) {
                $art->slug = Str::slug($art->name_en);
            }
        });
    }

    public function artist()
    {
        return $this->belongsTo(Artist::class, 'artist_id');
    }

    public function category()
    {
        return $this->belongsTo(PaintingCategory::class, 'painting_category_id');
    }

    public function images()
    {
        return $this->hasMany(CreativeArtImage::class, 'art_id');
    }
}
