<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class SubscriptionPlan extends Model
{
    use HasFactory;

    protected $table ="subscription_plans";
    protected $fillable = [
        'name',
        'slug',
        'description',
        'price_monthly',
        'price_yearly',
        'currency',
        'trial_days',
        'features',
        'max_listings',
        'featured_listing_allowed',
        'featured_listing_limit',
        'is_active',
        'is_popular',
        'stripe_price_id_monthly',
        'stripe_price_id_yearly',
        'stripe_product_id',
        'sort_order',
    ];

    protected $casts = [
        'features' => 'array',
        'is_active' => 'boolean',
        'is_popular' => 'boolean',
        'featured_listing_allowed' => 'boolean',
        'price_monthly' => 'decimal:2',
        'price_yearly' => 'decimal:2',
        'max_listings' => 'integer',
        'featured_listing_limit' => 'integer',
        'trial_days' => 'integer',
    ];

    public function hostSubscriptions()
    {
        return $this->hasMany(HostSubscription::class);
    }

    public function getFormattedPriceMonthlyAttribute()
    {
        return '$' . number_format($this->price_monthly, 2);
    }

    public function getFormattedPriceYearlyAttribute()
    {
        return '$' . number_format($this->price_yearly, 2);
    }

    public function getFeaturesListAttribute()
    {
        return is_array($this->features) ? $this->features : [];
    }
}