<?php

namespace App\Models;

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

class MexcoPlan extends Model
{
    protected $table = 'mexco_plans';

    protected $fillable = [
        'code',
        'name',
        'description',
        'price_cents',
        'currency',
        'is_custom_pricing',
        'is_active',
        'sort_order',
        'default_duration_days',
        'participant_limit',
        'session_limit',
        'booth_limit',
        'storage_limit_mb',
        'retention_days',
        'metadata',
    ];

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

    public function planFeatures(): HasMany
    {
        return $this->hasMany(MexcoPlanFeature::class, 'plan_id');
    }

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

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

    public function eventLicenses(): HasMany
    {
        return $this->hasMany(MexcoEventLicense::class, 'plan_id');
    }

    public function formattedPrice(): string
    {
        if ($this->is_custom_pricing) {
            return 'Custom';
        }

        return $this->currency.' '.number_format($this->price_cents / 100, 0);
    }
}
