<?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_local_only',
        'is_active'
    ];

    protected $casts = [
        'is_local_only' => 'boolean',
        '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 defaultLanguage()
    {
        return $this->belongsTo(Language::class, 'default_language_id');
    }

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

    public function countries()
    {
        return $this->belongsToMany(\App\Models\Definition\Country::class, 'marketplace_countries', 'marketplace_id', 'country_id')->withTimestamps();
    }

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