<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Facades\Log;
use App\Models\Subcategory;
use App\Models\Category;
use App\Models\Gownsize;
use App\Models\Pavadaisattai;
use App\Models\Productimage;
use App\Models\product_variation;
use App\Models\OtherCustomization;
use App\Models\SizeChart;


class Products extends Model
{
    use SoftDeletes;
    protected $table      = 'products';
    protected $primaryKey = 'product_id';
    protected $fillable   = [
                                'category_id',
                                'subcategory_id',
                                'gown_size_id',
                                'product_name',
                                'variation_ids',
                                'gown_size_ids',
                                'size_name',
                                'pavadai_size_ids',
                                'product_slug',
                                'pavadai_size_id',
                                'product_title',
                                'product_code',
                                'product_actual_price',
                                'product_discount_price',
                                'product_discount_percentage',
                                'product_discount_value',
                                'product_description',
                                'active_status',
                            ];
    protected $hidden     = [
                                'created_at',
                                'updated_at',
                                'deleted_at',
                            ];

    public function categoryDetails() {
        return $this->hasMany(Category::class, 'category_id', 'category_id');
    }

    public function subcategoryDetails() {
        return $this->hasMany(Subcategory::class, 'subcategory_id', 'subcategory_id');
    }

    public function gownsizeDetails() {
        return $this->hasMany(Gownsize::class, 'gown_size_id', 'gown_size_id');
    }
    

    public function othercustomization(){
        return $this->hasMany(OtherCustomization::class, 'other_customization_id', 'other_customization_id');
    }

    public function pavadaisizeDetails() {
        return $this->hasMany(Pavadaisattai::class, 'pavadai_size_id', 'pavadai_size_id');
    }

    public function productimagedetails(){
        return $this->hasMany(Productimage::class, 'product_id', 'product_id')->orderBy('image_position');
    }

    public function categorieslist()
    {
       $categoryIds = explode(',', $this->attributes['other_customization_id']);
       // Retrieve the corresponding category names from the Categories model
       $customization = OtherCustomization::whereIn('other_customization_id', $categoryIds)->get();
       return $customization;
    }
    public function pavadaisizedetailses()
    {
       $categoryIds = explode(',', $this->attributes['pavadai_size_ids']);
       $customization = Pavadaisattai::with('variationdetails')->whereIn('pavadai_size_id', $categoryIds)->get();
    
       return $customization;
    }
    public function pavadaisizedetailse($productId)
    {
       $categoryIds = explode(',', $this->attributes['pavadai_size_ids']);
        $customization = Pavadaisattai::with(['variationdetails' => function($query) use ($productId) {
            $query->where('product_id', $productId);
        }])->whereIn('pavadai_size_id', $categoryIds)->get();
        return $customization;

    }
    
    public function gownsizedetailses()
    {
       $categoryIds = explode(',', $this->attributes['gown_size_ids']);
       $customization = Gownsize::with('variationdetails')->whereIn('gown_size_id', $categoryIds)->get();
       return $customization;
    }
   
    public function productvariationdetailsesstwo()
    {
       $categoryIds = explode(',', $this->attributes['variation_ids']);
       $customization = product_variation::with('pavadailist')->whereIn('variation_id', $categoryIds)->get();
       return $customization;
    }

    public function gownsizedetailsese($productId)
    {
        // $categoryIds = explode(',', $this->attributes['gown_size_ids']);
        // $customization = Gownsize::with(['variationdetails' => function($query) use ($productId) {
        //     $query->where('product_id', $productId);
        // }])->whereIn('gown_size_id', $categoryIds)->get();
        // return $customization;
        $categoryIds = explode(',', $this->attributes['gown_size_ids']);
       $customization = Gownsize::with('variationdetails')->whereIn('gown_size_id', $categoryIds)->get();
       return $customization;
    }

     public function productvariationdetailsess()
    {
       $categoryIds = explode(',', $this->attributes['variation_ids']);
       $customization = product_variation::with('gownlist')->whereIn('variation_id', $categoryIds)->get();
       return $customization;
    }
    public function cardDetails() {
        return $this->hasMany('App\Models\Cart', 'customer_id', 'customer_id')->with('productDetailsOrder','customerDetailsOrder');
        }
    public function productvariationdetailses()
    {
       $categoryIds = explode(',', $this->attributes['variation_ids']);
       $customization = product_variation::whereIn('variation_id', $categoryIds)->get();
       return $customization;
    }
    
    public function variation()
    {
       $categoryIds = explode(',', $this->attributes['variation_ids']);
       $customization = product_variation::whereIn('variation_id', $categoryIds)->get();
       return $customization;
    }

    public function sizes(){
        if ( $this->attributes['size_name'] == 'gown_size'){
        // $categoryIds = explode(',', $this->attributes['sizes_id']);
       // Retrieve the corresponding category names from the Categories model
        $size       = Gownsize::where('active_status',1)
                                            ->get();
        return $size;
        }else {
            // $categoryIds = explode(',', $this->attributes['other_customization_id']);
            // Retrieve the corresponding category names from the Categories model
            $size        = Pavadaisattai::where('active_status',1)
                                                ->get();
            return $size;
        }

    }
    // public function sizeschartimage(){
    //     if ( $this->attributes['size_name'] == 'gown_size'){
    //     // $categoryIds = explode(',', $this->attributes['sizes_id']);
    //    // Retrieve the corresponding category names from the Categories model
    //     $size       = SizeChart::where('size_name','gown_size')
    //                                         ->get();
    //     return $size;
    //     }else {
    //         // $categoryIds = explode(',', $this->attributes['other_customization_id']);
    //         // Retrieve the corresponding category names from the Categories model
    //         $size        = SizeChart::where('size_name', 'gown_size')
    //                                             ->get();
    //         return $size;
    //     }

    // }
    public function sizeschartimage() {
        return $this->hasMany(SizeChart::class, 'size_name', 'size_name');
    }



}


