<?php

namespace App\Models\Agency;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use App\Models\Definition\Language;
use App\Models\Product\Product;

class Marketplace extends Model
{
    use HasFactory;

    protected $table = 'marketplaces';

    protected $fillable = [
        'agency_id',
        'name',
        'slug',
        'default_language_id',
        'default_currency_id',
        'is_active'
    ];

    protected $casts = [
        'is_active' => 'boolean'
    ];

    public function agency()
    {
        return $this->belongsTo(Agency::class);
    }

    public function languages()
    {
        return $this->belongsToMany(Language::class, 'marketplace_languages', 'marketplace_id', 'language_id')->withTimestamps();
    }

    public function currencies()
    {
        return $this->belongsToMany(Currency::class, 'marketplace_currencies', 'marketplace_id', 'currency_id')->withTimestamps();
    }

    public function defaultLanguage()
    {
        return $this->belongsTo(Language::class, 'default_language_id');
    }

    public function defaultCurrency()
    {
        return $this->belongsTo(Currency::class, 'default_currency_id');
    }

    public function products()
    {
        return $this->belongsToMany(Product::class, 'product_marketplaces', 'marketplace_id', 'product_id')->withTimestamps();
    }
}
