<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Spatie\Sluggable\HasSlug;
use Spatie\Sluggable\SlugOptions;

class Product extends Model
{
    use HasFactory, SoftDeletes, HasSlug;

    protected $fillable = [
        'category_id',
        'name',
        'slug',
        'description',
        'features',
        'specifications',
        'sizes',
        'colors',
        'featured_image',
        'gallery',
        'gallery_2',              // ✅ NEW
        'youtube_video_url',      // ✅ NEW
        'gallery_1_title',        // ✅ NEW
        'gallery_2_title',        // ✅ NEW
        'price',
        'sku',
        'stock',
        'is_featured',
        'is_active',
        'views',
        'order',
    ];

    protected $casts = [
        'specifications' => 'array',
        'sizes' => 'array',
        'colors' => 'array',
        'gallery' => 'array',
        'gallery_2' => 'array',   // ✅ NEW
        'is_featured' => 'boolean',
        'is_active' => 'boolean',
        'price' => 'decimal:2',
        'stock' => 'integer',
        'views' => 'integer',
        'order' => 'integer',
    ];

    public function getSlugOptions(): SlugOptions
    {
        return SlugOptions::create()
            ->generateSlugsFrom('name')
            ->saveSlugsTo('slug');
    }

    public function category()
    {
        return $this->belongsTo(Category::class);
    }

    public function scopeActive($query)
    {
        return $query->where('is_active', true);
    }

    public function scopeFeatured($query)
    {
        return $query->where('is_featured', true);
    }

    public function incrementViews()
    {
        $this->increment('views');
    }

    // Helper method to get gallery 1 images with captions
    public function getGalleryImagesAttribute()
    {
        if (!$this->gallery || !is_array($this->gallery)) {
            return [];
        }

        return collect($this->gallery)->map(function ($item) {
            if (is_string($item)) {
                return ['image' => $item, 'caption' => null];
            }
            return [
                'image' => $item['image'] ?? $item,
                'caption' => $item['caption'] ?? null,
            ];
        })->toArray();
    }

    // ✅ NEW: Helper method to get gallery 2 images with captions
    public function getGallery2ImagesAttribute()
    {
        if (!$this->gallery_2 || !is_array($this->gallery_2)) {
            return [];
        }

        return collect($this->gallery_2)->map(function ($item) {
            if (is_string($item)) {
                return ['image' => $item, 'caption' => null];
            }
            return [
                'image' => $item['image'] ?? $item,
                'caption' => $item['caption'] ?? null,
            ];
        })->toArray();
    }

    // ✅ NEW: Helper to get YouTube video embed ID
    public function getYoutubeVideoIdAttribute()
    {
        if (!$this->youtube_video_url) return null;

        // Extract video ID from various YouTube URL formats
        $pattern = '/(?:youtube\.com\/(?:[^\/]+\/.+\/|(?:v|e(?:mbed)?)\/|.*[?&]v=)|youtu\.be\/)([^"&?\/ ]{11})/';
        
        if (preg_match($pattern, $this->youtube_video_url, $matches)) {
            return $matches[1];
        }

        return null;
    }
}