<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Product extends BaseModel
{
    protected $table = 'products';

    protected $appends = ['totalItem'];

    protected $fillable = ['id', 'code', 'type', 'category_id', 'scene_id', 'manufacturer_id',
                           'sku', 'name', 'slug', 'image', 'gender', 'size', 'color', 'material', 'dimension_cm', 'dimension_scale',
                           'height', 'sleeve', 'sleeve_length', 'hip', 'primary_width', 'second_width', 'undergarment_length', 'explanation', 'kimimono',
                           'description', 'price_from', 'price_to', 'discount', 'discount_type', 'discount_start_time', 'discount_end_time',
                           'regular_price_from', 'regular_price_to', 'default_price', 'standard_construction_fee', 'shipping_cost',
                           'note', 'policy', 'possible_delivery_time', 'rank', 'storage_date', 'wish_list', 'view', 'preview', 'is_allow_faceswap',
                           'is_pricing_manual', 'is_available_in_stock', 'is_cleaning_service', 'is_storage_period', 'created_by', 'is_activated', 'is_deleted', 'created_at', 'updated_at'];

    public function category() {
        return $this->hasMany(ProductCategory::class, 'product_id', 'id')->with('category');
    }

    public function color_detail() {
        return $this->hasMany(ProductColor::class, 'product_id', 'id')->with('color');
    }

    public function schedule() {
        return $this->hasMany(ProductSchedule::class, 'product_id', 'id')
            ->select('id', 'product_id', 'short_date', 'full_date', 'note')
            ->where('short_date', '>=', date('Y-m-d'))
            ->orderBy('short_date');
    }

    public function user() {
        return $this->belongsTo(User::class, 'created_by', 'id')->select('id', 'name', 'slug', 'full_name', 'furigana_name');
    }

    public function scene() {
        return $this->belongsTo(Scene::class, 'scene_id', 'id');
    }

    public function manufacturer() {
        return $this->belongsTo(Manufacturer::class, 'manufacturer_id', 'id')->select('id', 'name', 'slug', 'image');
    }

    public function options() {
        return $this->hasMany(ProductOption::class, 'product_id', 'id')->with('details');
    }

    public function options_public() {
        return $this->hasMany(ProductOptionPublic::class, 'product_id', 'id')
            ->with('details')
            ->where(function($query) {
                $query->where('is_check_all', true)
                    ->orWhere('is_indeterminate', true);
            })
            ->orderBy('name');
    }

    public function keyword_detail() {
        return $this->hasMany(ProductKeyword::class, 'product_id', 'id')->with('keyword');
    }

    public function purchases() {
        return $this->hasMany(ProductPurchaseMethod::class, 'product_id', 'id');
    }

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

    public function details() {
        return $this->hasMany(ProductDetail::class, 'product_id', 'id')->with('option_detail');
    }

    public function getTotalItemAttribute() {
        return $this->details()->count();
    }

    public function pricing_rules() {
        return $this->hasMany(PricingRule::class, 'product_id', 'id')->with('detail', 'pricing');
    }
}

