<?php

namespace App\Models;

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

class SubscriptionPlan extends Model
{
    public const PERIOD_MONTHLY = 'monthly';
    public const PERIOD_QUARTERLY = 'quarterly';
    public const PERIOD_YEARLY = 'yearly';

    protected $fillable = [
        'name',
        'slug',
        'billing_period',
        'duration_days',
        'price',
        'currency',
        'description',
        'features',
        'max_trades',
        'can_access_analytics',
        'can_access_trading_tools',
        'can_access_forex_calendar',
        'can_access_market_news',
        'can_upload_screenshots',
        'is_active',
        'is_recommended',
        'sort_order',
    ];

    protected function casts(): array
    {
        return [
            'price' => 'decimal:2',
            'features' => 'array',
            'max_trades' => 'integer',
            'can_access_analytics' => 'boolean',
            'can_access_trading_tools' => 'boolean',
            'can_access_forex_calendar' => 'boolean',
            'can_access_market_news' => 'boolean',
            'can_upload_screenshots' => 'boolean',
            'is_active' => 'boolean',
            'is_recommended' => 'boolean',
        ];
    }

    public function subscriptions(): HasMany
    {
        return $this->hasMany(UserSubscription::class);
    }

    public function payments(): HasMany
    {
        return $this->hasMany(Payment::class);
    }

    public function scopeActive($query)
    {
        return $query->where('is_active', true);
    }

    public function allowsFeature(string $feature): bool
    {
        return match ($feature) {
            'analytics' => $this->can_access_analytics,
            'trading_tools' => $this->can_access_trading_tools,
            'forex_calendar' => $this->can_access_forex_calendar,
            'market_news' => $this->can_access_market_news,
            'screenshot_uploads' => $this->can_upload_screenshots,
            default => true,
        };
    }
}
