<?php

namespace App\Models\Ecommerce\Product\Product\Concerns;

use App\Models\Common\Comment;
use App\Models\Ecommerce\Channel\Channel;
use App\Models\Ecommerce\Product\Brand\Brand;
use App\Models\Ecommerce\Product\Cart\CartItem;
use App\Models\Ecommerce\Product\Category\Category;
use App\Models\Ecommerce\Product\Collection\Collection;
use App\Models\Ecommerce\Product\Inventory\Inventory;
use App\Models\Ecommerce\Product\Product\Product;
use App\Models\Ecommerce\Product\Property\ProductProperty;
use App\Models\Ecommerce\Product\Review\Review;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\HasMany;

trait ProductRelationships
{
    /**
     * Product variations
     */
    public function variations(): HasMany
    {
        return $this->hasMany(Product::class, 'parent_id');
    }

    /**
     * Parent
     */
    public function parent(): BelongsTo
    {
        return $this->belongsTo(Product::class, 'parent_id');
    }

    /**
     * Related Channels
     */
    public function channels(): BelongsToMany
    {
        return $this->belongsToMany(Channel::class, 'channel_product');
    }

    /**
     * Firt channel
     */
    public function owner(): ?Channel
    {
        return $this->relationLoaded('channels')
                    ? $this->channels->first()
                    : $this->channels()->first();
    }

    /**
     * Related products (similar)
     */
    public function relatedProducts(): BelongsToMany
    {
        return $this->belongsToMany(Product::class, 'product_related', 'product_id', 'related_product_id');
    }

    /**
     * Related categories
     */
    public function categories(): BelongsToMany
    {
        return $this->belongsToMany(Category::class, 'category_product');
    }

    /**
     * Related Collections
     */
    public function collections(): BelongsToMany
    {
        return $this->belongsToMany(Collection::class, 'collection_product');
    }

    /**
     * Related Brand
     */
    public function brand(): BelongsTo
    {
        return $this->belongsTo(Brand::class, 'brand_id');
    }

    /**
     * Product Properties, same as attribtues
     *
     * (size, color, material, ..)
     */
    public function properties(): HasMany
    {
        return $this->hasMany(ProductProperty::class);
    }

    /**
     * Related inventories
     */
    public function inventories(): BelongsToMany
    {
        return $this->belongsToMany(Inventory::class)
            ->withPivot('stock');
    }

    /**
     * Carts
     */
    public function carts(): BelongsTo
    {
        return $this->belongsTo(CartItem::class);
    }

    /**
     * Product's review
     */
    public function reviews(): HasMany
    {
        return $this->hasMany(Review::class);
    }

    /**
     * Product's comments
     */
    public function comments(): BelongsToMany
    {
        return $this->belongsToMany(Comment::class);
    }
}
