<?php

namespace App\Models;

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

class Product extends Model
{
    use HasFactory;

    /**
     * The attributes that are mass assignable.
     *
     * @var array<int, string>
     */
    protected $fillable = [
        'name',
        'name_km',
        'price',
        'original_price',
        'category',
        'category_id',
        'description',
        'description_km',
        'short_description',
        'short_description_km',
        'in_stock',
        'on_sale',
        'images',
    ];

    /**
     * Get the attributes that should be cast.
     *
     * @return array<string, string>
     */
    protected function casts(): array
    {
        return [
            'price' => 'decimal:2',
            'original_price' => 'decimal:2',
            'images' => 'array',
            'in_stock' => 'boolean',
            'on_sale' => 'boolean',
        ];
    }



    /**
     * Get the category of the product (using category_id).
     */
    public function categoryRelation()
    {
        return $this->belongsTo(Category::class, 'category_id');
    }

    /**
     * Get the category of the product (legacy - using category code).
     */
    public function getCategoryAttribute($value)
    {
        return $value;
    }

    /**
     * Scope a query to only include products in stock.
     */
    public function scopeInStock($query)
    {
        return $query->where('in_stock', true);
    }

    /**
     * Scope a query to only include products on sale.
     */
    public function scopeOnSale($query)
    {
        return $query->where('on_sale', true);
    }

    /**
     * Scope a query to filter by category (legacy - using category code).
     */
    public function scopeByCategory($query, $category)
    {
        return $query->where('category', $category);
    }

    /**
     * Scope a query to filter by category_id.
     */
    public function scopeByCategoryId($query, $categoryId)
    {
        return $query->where('category_id', $categoryId);
    }

    /**
     * Get the featured/main image of the product (image_profile).
     */
    public function getFeaturedImageAttribute(): ?string
    {
        $images = $this->images ?? [];

        return ! empty($images) ? $images[0] : null;
    }

    /**
     * Get the image profile (first/main image).
     */
    public function getImageProfileAttribute(): ?string
    {
        $images = $this->images ?? [];

        return ! empty($images) ? $images[0] : null;
    }

    /**
     * Get all gallery images except the featured one (image_gallery).
     */
    public function getGalleryImagesAttribute(): array
    {
        $images = $this->images ?? [];

        return count($images) > 1 ? array_slice($images, 1) : [];
    }

    /**
     * Get the image gallery (all images except the first one).
     */
    public function getImageGalleryAttribute(): array
    {
        $images = $this->images ?? [];

        return count($images) > 1 ? array_slice($images, 1) : [];
    }

    /**
     * Check if product has images.
     */
    public function hasImages(): bool
    {
        return ! empty($this->images) && count($this->images) > 0;
    }

    /**
     * Get total number of images.
     */
    public function getImageCountAttribute(): int
    {
        return count($this->images ?? []);
    }

    /**
     * Get the order items for the product.
     */
    public function orderItems()
    {
        return $this->hasMany(OrderItem::class);
    }

    /**
     * Get the wishlist items for the product.
     */
    public function wishlists()
    {
        return $this->hasMany(Wishlist::class);
    }

    /**
     * Get the users who have this product in their wishlist.
     */
    public function wishlistedByUsers()
    {
        return $this->belongsToMany(User::class, 'wishlists', 'product_id', 'user_id')->withTimestamps();
    }
}
