<?php

namespace App\Models;

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

class MiningSession extends Model
{
    use HasFactory;

    protected $fillable = [
        'user_id',
        'plan_id',
        'started_at',
        'expires_at',
        'total_mined',
        'last_calculated_at',
        'is_active',
        'daily_earnings_date',
        'daily_earnings'
    ];

    protected $casts = [
        'started_at' => 'datetime',
        'expires_at' => 'datetime',
        'last_calculated_at' => 'datetime',
        'total_mined' => 'decimal:8',
        'is_active' => 'boolean'
    ];

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

    public function plan()
    {
        return $this->belongsTo(Plan::class);
    }

    public function isExpired()
    {
        return Carbon::now()->isAfter($this->expires_at);
    }

    public function getRemainingTimeInSeconds()
    {
        if ($this->isExpired()) {
            return 0;
        }
        return Carbon::now()->diffInSeconds($this->expires_at, false);
    }
}