<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class PatientSubscription extends Model
{
    protected $table = 'patient_subscriptions';

    protected $fillable = [
        'patient_id','plan_id','billing_cycle','amount_paid','currency',
        'status','starts_at','ends_at','cancelled_at',
        'payment_method','transaction_id','payment_data','auto_renew',
    ];

    protected $casts = [
        'starts_at'    => 'datetime',
        'ends_at'      => 'datetime',
        'cancelled_at' => 'datetime',
        'payment_data' => 'array',
        'auto_renew'   => 'boolean',
        'amount_paid'  => 'decimal:2',
    ];

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

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

    public function daysRemaining(): int
    {
        return max(0, (int) now()->diffInDays($this->ends_at, false));
    }
}
