<?php

namespace Database\Factories;

use App\Models\MiningSession;
use App\Models\User;
use App\Models\Plan;
use Illuminate\Database\Eloquent\Factories\Factory;
use Carbon\Carbon;

class MiningSessionFactory extends Factory
{
    protected $model = MiningSession::class;

    public function definition()
    {
        $startedAt = $this->faker->dateTimeBetween('-30 days', 'now');
        $period = $this->faker->numberBetween(12, 72);
        $expiresAt = Carbon::parse($startedAt)->addHours($period);

        return [
            'user_id' => User::factory(),
            'plan_id' => Plan::factory(),
            'started_at' => $startedAt,
            'expires_at' => $expiresAt,
            'total_mined' => $this->faker->randomFloat(8, 0, 10),
            'last_calculated_at' => $startedAt,
            'is_active' => $this->faker->boolean(30) // 30% chance of being active
        ];
    }

    public function active()
    {
        return $this->state(function (array $attributes) {
            return [
                'is_active' => true,
                'started_at' => now()->subHours(2),
                'expires_at' => now()->addHours(22),
                'last_calculated_at' => now()->subMinutes(5),
            ];
        });
    }

    public function expired()
    {
        return $this->state(function (array $attributes) {
            return [
                'is_active' => false,
                'started_at' => now()->subDays(2),
                'expires_at' => now()->subDay(),
                'last_calculated_at' => now()->subDay(),
            ];
        });
    }
}
