<?php

namespace App;

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

class Product extends BaseModel
{
    use HasFactory;

    protected $table = 'products';

    protected $fillable = [
        'id', 'name', 'slug', 'sku', 'price', 'discount_amount', 'discount_price',
        'thumbnail', 'weight', 'dimensions', 'short_description', 'description',
        'information', 'note', 'in_stock', 'rating_avg', 'total_review', 'total_share', 'total_view', 'preview', 'publish_at',
        'crawler_url', 'crawler_sku', 'crawler_shop', 'crawler_site', 'crawler_sales', 'crawler_price',
        'crawler_rating', 'crawler_availability', 'crawler_videos', 'crawler_images', 'crawler_categories',
        'crawler_highlights', 'crawler_reviews_shop', 'crawler_reviews_item', 'crawler_status', 'crawler_collection_at',
        'is_activated', 'is_deleted', 'created_at', 'updated_at'
    ];

    public function seo() {
        return $this->belongsTo(Seo::class, 'id', 'post_id')->where('type', 'product')->select('id', 'title', 'keywords', 'description', 'image', 'type', 'post_id');
    }

    public function category_detail() {
        return $this->hasMany(ProductCategoryDetail::class, 'product_id', 'id')->select('id', 'product_id', 'product_category_id');
    }

    public function brand_detail() {
        return $this->hasMany(ProductBrandDetail::class, 'product_id', 'id')->select('id', 'product_id', 'product_brand_id');
    }

    public function brand_list() {
        return $this->hasMany(ProductBrandDetail::class, 'product_id', 'id')->with('brand')->select('id', 'product_id', 'product_brand_id');
    }

    public function size_detail() {
        return $this->hasMany(ProductSizeDetail::class, 'product_id', 'id')->select('id', 'product_id', 'product_size_id');
    }

    public function color_detail() {
        return $this->hasMany(ProductColorDetail::class, 'product_id', 'id')->select('id', 'product_id', 'product_color_id');
    }

    public function keyword_detail() {
        return $this->hasMany(KeywordDetail::class, 'ref_id', 'id')->with('keyword')->select('id', 'keyword_id', 'ref_id')->where('type', 'product');
    }

    public function slider() {
        return $this->hasMany(ProductImage::class, 'product_id', 'id')->select('id', 'product_id', 'file_name', 'name');
    }
}
