<?php

declare(strict_types=1);

namespace App\Models\Subscription;

use Hyperf\DbConnection\Model\Model;
use App\Models\User;

class Subscription extends Model
{
    protected ?string $table = 'subscriptions';

    public bool $incrementing = true;

    protected array $fillable = [
        'hashkey',
        'user_id',
        'plan_id',
        'status',
        'starts_at',
        'expires_at',
        'payment_method',
        'additional_details',
        'created_by',
        'updated_by',
    ];

    protected array $casts = [
        'starts_at' => 'datetime',
        'expires_at' => 'datetime',
        'additional_details' => 'array',
    ];

    public function user()
    {
        return $this->belongsTo(User::class, 'user_id');
    }

    public function plan()
    {
        return $this->belongsTo(SubscriptionPlan::class, 'plan_id');
    }

    public function invoices()
    {
        return $this->hasMany(SubscriptionInvoice::class, 'subscription_id');
    }

    public function isActive(): bool
    {
        return $this->status === 'active' && $this->expires_at && $this->expires_at->isFuture();
    }
}
