<?php

namespace Database\Seeders;

use App\Models\BotSetting;
use App\Models\BrokerAccount;
use App\Models\Strategy;
use App\Models\User;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\Hash;

class DatabaseSeeder extends Seeder
{
    public function run(): void
    {
        $rulebook = json_decode(
            file_get_contents(config_path('rulebook/default.json')),
            true
        );

        $admin = User::updateOrCreate(
            ['email' => 'admin@autotrade.local'],
            [
                'name' => 'Admin',
                'password' => Hash::make('password'),
                'role' => 'admin',
                'email_verified_at' => now(),
            ]
        );

        $trader = User::updateOrCreate(
            ['email' => 'trader@autotrade.local'],
            [
                'name' => 'Trader',
                'password' => Hash::make('password'),
                'role' => 'user',
                'email_verified_at' => now(),
            ]
        );

        foreach ([$admin, $trader] as $user) {
            Strategy::updateOrCreate(
                ['user_id' => $user->id],
                [
                    'name' => 'NIFTY Option Buying',
                    'rulebook' => $rulebook,
                    'is_active' => true,
                ]
            );

            BotSetting::updateOrCreate(
                ['user_id' => $user->id],
                [
                    'bot_enabled' => $user->isAdmin() ? false : true,
                    'auto_trade' => false,
                    'bot_status' => 'idle',
                ]
            );

            BrokerAccount::updateOrCreate(
                ['user_id' => $user->id],
                [
                    'broker' => 'flattrade',
                    'is_connected' => false,
                ]
            );
        }
    }
}
