<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Product extends BaseModel
{
	const TYPE_OWNER = 0;
	const TYPE_STORAGE = 1;
	const TYPE_SHARING = 2;
	const TYPE_PRODUCT = 3;

	const ORDER_STATUS_PREPARATION = 0;
	const ORDER_STATUS_IN_STORAGE = 1;
	const ORDER_STATUS_RENTING = 2;
	const ORDER_STATUS_PROCESSING = 3;
	const ORDER_STATUS_RETURNED = 4;
	const ORDER_STATUS_TEMPORARY_RETURN = 5;
	const ORDER_STATUS_CANCELLED = 6;

	protected $table = 'products';
	
	protected $appends = ['totalItem'];
	
	protected $fillable = ['id', 'code', 'type', 'category_id', 'scene_id', 'manufacturer_id',
						   'sku', 'sku_storage', 'name', 'slug', 'image', 'tryon_model_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', 'reward_amount', 'standard_construction_fee', 'shipping_cost',
						   'note', 'policy', 'possible_delivery_time', 'rank', 'storage_date', 'storage_cabinet_code', 'owner_id', 'wish_list', 'view', 'preview', 'is_allow_faceswap',
						   'is_pricing_manual', 'is_available_in_stock', 'is_cleaning_service', 'is_storage_period', 'order_status', 'status', '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 owner()
	{
		return $this->belongsTo(User::class, 'owner_id', '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');
	}
	
	public function accessories()
	{
		return $this->hasMany(ProductAccessory::class, 'product_id', 'id')->with('accessory');
	}
	
	public function skus()
	{
		return $this->hasMany(ProductSku::class, 'product_id', 'id');
	}

	public function storageContract()
	{
		return $this->hasOne(StorageContract::class, 'product_id', 'id');
	}

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

	// 出品開始日: 完了した出品申請(出品承認)の最新の完了日時。保管→出品で created_at が
	// 元の保管登録日のままになるため、出品開始日のデータソースとして使う。
	public function listingRequest()
	{
		return $this->hasOne(ServiceRequest::class, 'product_id', 'id')
			->where('type', ServiceRequest::TYPE_LISTING)
			->where('status', ServiceRequest::STATUS_COMPLETED)
			->latest('completed_at');
	}

	public function isOwnerProduct()
	{
		return $this->type == self::TYPE_OWNER;
	}

	public function isStorageProduct()
	{
		return $this->type == self::TYPE_STORAGE;
	}
}

