<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\HasMany;

class MexcoAddon extends Model
{
    protected $table = 'mexco_addons';

    protected $fillable = [
        'code',
        'name',
        'description',
        'price_cents',
        'currency',
        'is_active',
        'sort_order',
    ];

    protected function casts(): array
    {
        return [
            'is_active' => 'boolean',
        ];
    }

    public function features(): BelongsToMany
    {
        return $this->belongsToMany(MexcoFeature::class, 'mexco_addon_features', 'addon_id', 'feature_id')
            ->withPivot(['enabled', 'limit_value'])
            ->withTimestamps();
    }

    public function plans(): BelongsToMany
    {
        return $this->belongsToMany(MexcoPlan::class, 'mexco_plan_addons', 'addon_id', 'plan_id')
            ->withPivot(['is_active'])
            ->withTimestamps();
    }

    public function eventAddons(): HasMany
    {
        return $this->hasMany(MexcoEventAddon::class, 'addon_id');
    }

    public function formattedPrice(): string
    {
        return $this->currency.' '.number_format($this->price_cents / 100, 0);
    }
}
