<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Subscription extends Model
{
    const STATUS_ACTIVE = 'active';
    const STATUS_INACTIVE = 'inactive';

    const PERIODICITY_ANNUAL = 'annual';

    protected $casts = [
        'start_date' => 'date',
        'end_date' => 'date',
        'trial_end_date' => 'date',
        'renewed_date' => 'date',
        'cancel_on' => 'date',
        'price' => 'float'
    ];

    public function subscriptionPlan()
    {
        return $this->belongsTo(SubscriptionPlan::class);
    }

    public function account()
    {
        return $this->belongsTo(Account::class);
    }

    public function subscriptionReward()
    {
        return $this->belongsTo(SubscriptionReward::class);
    }

    public function scopeActive($query)
    {
        return $query->where('status', self::STATUS_ACTIVE);
    }

    public function getIsExpiredAttribute()
    {
        return $this->is_trial ? $this->trial_end_date->isPast() : $this->end_date->isPast();
    }
}
