<?php

namespace Database\Seeders;

use App\Enums\RecordStatus;
use App\Enums\UserRole;
use App\Models\Deposit;
use App\Models\Market;
use App\Models\SiteSetting;
use App\Models\Trade;
use App\Models\TradeActivity;
use App\Models\TradePeriod;
use App\Models\User;
use App\Models\Withdrawal;
use App\Services\TransactionLogger;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\Hash;

class DatabaseSeeder extends Seeder
{
    public function run(): void
    {
        $super = User::updateOrCreate(
            ['email' => 'super@web3derp.com'],
            [
                'name' => 'Super Admin',
                'username' => 'superadmin',
                'password' => Hash::make('password'),
                'role' => UserRole::SuperAdmin,
                'balance' => 0,
                'status' => 'active',
                'email_verified_at' => now(),
            ]
        );

        User::updateOrCreate(
            ['email' => 'staff@web3derp.com'],
            [
                'name' => 'Admin Staff',
                'username' => 'adminstaff',
                'password' => Hash::make('password'),
                'role' => UserRole::AdminStaff,
                'balance' => 0,
                'status' => 'active',
                'email_verified_at' => now(),
            ]
        );

        $defaults = [
            'site_title' => 'Web3 Derp',
            'site_currency' => 'USDT',
            'currency_symbol' => '$',
            'signup_bonus' => '0',
            'transfer_min_limit' => '0',
            'transfer_charge' => '0',
            'crypto_api_key' => '',
            'admin_whatsapp' => '',
            'admin_telegram' => '',
            'wallet_btc' => '',
            'wallet_eth' => '',
            'wallet_bsc' => '',
            'wallet_trc20' => '',
            'wallet_polygon' => '',
            'wallet_solana' => '',
            'wallet_usdt_erc20' => '',
            'wallet_usdc' => '',
            'trading_mode' => 'auto',
            'daily_bonus_amount' => '0',
        ];

        foreach ($defaults as $key => $value) {
            SiteSetting::updateOrCreate(['key' => $key], ['value' => $value]);
        }

        $this->call(MarketCatalogSeeder::class);

        $client = User::updateOrCreate(
            ['email' => 'client@web3derp.com'],
            [
                'name' => 'Demo Client',
                'username' => 'democlient',
                'password' => Hash::make('password'),
                'role' => UserRole::User,
                'balance' => 95000,
                'status' => 'active',
                'email_verified_at' => now(),
            ]
        );

        if (Deposit::count() === 0) {
            $deposit = Deposit::create([
                'user_id' => $client->id,
                'network' => 'ETH',
                'wallet_address' => '0xDemoWalletAddress1234567890',
                'amount' => 1000,
                'status' => RecordStatus::Pending,
            ]);
            TransactionLogger::fromDeposit($deposit);
        }

        if (Withdrawal::count() === 0) {
            $withdrawal = Withdrawal::create([
                'user_id' => $client->id,
                'network' => 'TRC20',
                'wallet_address' => 'TDemoWalletAddress1234567890',
                'amount' => 500,
                'status' => RecordStatus::Pending,
            ]);
            TransactionLogger::fromWithdrawal($withdrawal);
        }

        $this->call(TradePeriodSeeder::class);

        if (TradeActivity::count() === 0) {
            TradeActivity::create([
                'user_id' => $client->id,
                'type' => 'trade',
                'amount' => 10000,
                'outcome' => 'Win',
                'description' => 'Trade',
            ]);
        }

        if (Trade::count() === 0) {
            $btc = Market::where('symbol', 'BTC')->first();
            $period60 = TradePeriod::where('duration_seconds', 60)->whereNull('market_type')->first();

            if ($btc && $period60) {
                Trade::create([
                    'user_id' => $client->id,
                    'market_id' => $btc->id,
                    'trade_period_id' => $period60->id,
                    'direction' => 'buy',
                    'amount' => 5000,
                    'status' => 'completed',
                    'outcome' => 'lost',
                    'ends_at' => now()->subHour(),
                    'closed_at' => now()->subHour(),
                ]);
            }

            $gold = Market::where('symbol', 'XAUUSD')->first();
            $goldPeriod = TradePeriod::where('duration_seconds', 60)->where('market_type', 'metal')->first();

            if ($gold && $goldPeriod) {
                Trade::create([
                    'user_id' => $client->id,
                    'market_id' => $gold->id,
                    'trade_period_id' => $goldPeriod->id,
                    'direction' => 'buy',
                    'amount' => 500,
                    'status' => 'completed',
                    'outcome' => 'lost',
                    'ends_at' => now()->subMinutes(30),
                    'closed_at' => now()->subMinutes(30),
                ]);
            }
        }
    }
}
