<?php

namespace Database\Factories;

use App\Models\Transaction;
use App\Models\Wallet;
use Illuminate\Database\Eloquent\Factories\Factory;

/**
 * @extends Factory<Wallet>
 */
class WalletFactory extends Factory
{
    /**
     * Configure the model factory.
     *
     * @return $this
     */
    public function configure()
    {
        return $this->afterCreating(function (Wallet $wallet) {
//            $wallet->payments()->saveMany(Transaction::factory(4)->create(
//                [
//                    'from' => $wallet->id,
//                    'to' => 1,
//                    'type' => 'Payment'
//                ],
//            ));
//            $wallet->payouts()->saveMany(Transaction::factory(3)->create(
//                [
//                    'to' => $wallet->id,
//                    'from' => 1,
//                    'type' => 'Payout',
//                ],
//            ));
//            $total_spent = $wallet->payouts()->sum('amount');
//            $upcoming = $wallet->payments()->sum('amount');
//            $reserved = $upcoming - $total_spent;
//            $wallet->update(
//                [
//                    'reserved_funds' => $reserved,
//                    'total_spent' => $total_spent,
//                    'upcoming_payments' => $upcoming,
//                ]
//            );
//            $wallet->save();
        });
    }

    /**
     * Define the model's default state.
     *
     * @return array<string, mixed>
     */
    public function definition()
    {
        return [
            'owner_id' => $this->faker->numberBetween(1, 5),
            'owner_type' => $this->faker->randomElement(['App\Models\Engineer', 'App\Models\Firm', 'App\Models\User']),
            'reserved_funds' => 0,
            'total_spent' => 0,
            'upcoming_payments' => 0,
        ];
    }
}
