<?php

namespace Database\Seeders;

use App\Models\Game;
use App\Models\GameCategory;
use App\Models\Jackpot;
use App\Models\PaymentMethod;
use App\Models\Promotion;
use App\Models\SliderBanner;
use App\Models\SportMatch;
use App\Models\User;
use Illuminate\Database\Seeder;
use Illuminate\Support\Str;
use Spatie\Permission\Models\Permission;
use Spatie\Permission\Models\Role;

class DatabaseSeeder extends Seeder
{
    public function run(): void
    {
        // Settings
        $this->call(SettingSeeder::class);

        // Roles & Permissions
        $adminRole = Role::firstOrCreate(['name' => 'admin', 'guard_name' => 'web']);
        $playerRole = Role::firstOrCreate(['name' => 'player', 'guard_name' => 'web']);
        $agentRole = Role::firstOrCreate(['name' => 'agent', 'guard_name' => 'web']);
        Role::firstOrCreate(['name' => 'vip', 'guard_name' => 'web']);

        $permissions = [
            'manage users', 'manage games', 'manage bets', 'manage transactions',
            'manage promotions', 'manage sport matches', 'manage settings',
            'view reports', 'manage jackpots', 'manage payment methods',
            'manage pos terminals', 'create pos sales', 'view pos receipts',
        ];
        foreach ($permissions as $perm) {
            $p = Permission::firstOrCreate(['name' => $perm, 'guard_name' => 'web']);
            $adminRole->givePermissionTo($p);
        }

        foreach (['create pos sales', 'view pos receipts'] as $perm) {
            $agentRole->givePermissionTo(Permission::firstOrCreate(['name' => $perm, 'guard_name' => 'web']));
        }

        // Admin user
        $admin = User::firstOrCreate(['email' => 'admin@vvmj8.com'], [
            'name' => 'Admin',
            'username' => 'admin',
            'password' => bcrypt('admin123'),
            'balance' => 0,
            'email_verified_at' => now(),
        ]);
        $admin->assignRole($adminRole);

        // Game Categories
        $categories = [
            ['name' => 'Popular', 'slug' => 'popular', 'icon' => 'star', 'sort_order' => 1],
            ['name' => 'Vvmj8', 'slug' => 'vvmj8', 'icon' => 'zap', 'sort_order' => 2],
            ['name' => 'Iogames', 'slug' => 'iogames', 'icon' => 'gamepad', 'sort_order' => 3],
            ['name' => 'Mascot', 'slug' => 'mascot', 'icon' => 'crown', 'sort_order' => 4],
            ['name' => 'Pragmatic Play', 'slug' => 'pragmatic-play', 'icon' => 'gem', 'sort_order' => 5],
            ['name' => 'Evolution Gaming', 'slug' => 'evolution-gaming', 'icon' => 'tv', 'sort_order' => 6],
            ['name' => 'Slots Classic', 'slug' => 'slots-classic', 'icon' => 'circle-dollar-sign', 'sort_order' => 7],
            ['name' => 'Turbogames', 'slug' => 'turbogames', 'icon' => 'rocket', 'sort_order' => 8],
            ['name' => 'Goldenrace', 'slug' => 'goldenrace', 'icon' => 'trophy', 'sort_order' => 9],
            ['name' => 'TVBet', 'slug' => 'tvbet', 'icon' => 'monitor', 'sort_order' => 10],
        ];

        $categoryModels = [];
        foreach ($categories as $cat) {
            $categoryModels[$cat['slug']] = GameCategory::firstOrCreate(['slug' => $cat['slug']], $cat);
        }

        // Games
        $gamesData = [
            ['title' => 'Keno', 'type' => 'Lottery', 'category' => 'Vvmj8', 'provider' => 'Vvmj8', 'players' => 142, 'max_win' => '10,000x', 'is_hot' => true, 'rtp' => 96.5],
            ['title' => 'American Roulette', 'type' => 'Table', 'category' => 'Vvmj8', 'provider' => 'Vvmj8', 'players' => 89, 'max_win' => '35x', 'rtp' => 94.7],
            ['title' => 'Lucky 6', 'type' => 'Lottery', 'category' => 'Vvmj8', 'provider' => 'Vvmj8', 'players' => 67, 'max_win' => '100x', 'is_new' => true, 'rtp' => 95.0],
            ['title' => '20 Dices', 'type' => 'Dice', 'category' => 'Vvmj8', 'provider' => 'Vvmj8', 'players' => 45, 'max_win' => '200x', 'rtp' => 97.0],
            ['title' => 'Lucky Wheel', 'type' => 'Wheel', 'category' => 'Vvmj8', 'provider' => 'Vvmj8', 'players' => 203, 'max_win' => '50x', 'is_hot' => true, 'rtp' => 96.0],
            ['title' => 'Joker Poker', 'type' => 'Card', 'category' => 'Vvmj8', 'provider' => 'Vvmj8', 'players' => 78, 'max_win' => '800x', 'rtp' => 98.6],
            ['title' => 'Penalty Unlimited', 'type' => 'Sports', 'category' => 'Iogames', 'provider' => 'Iogames', 'players' => 312, 'max_win' => '$5,000', 'is_hot' => true, 'rtp' => 95.5],
            ['title' => 'Diver', 'type' => 'Arcade', 'category' => 'Iogames', 'provider' => 'Iogames', 'players' => 156, 'max_win' => '$2,000', 'rtp' => 95.0],
            ['title' => 'Twister', 'type' => 'Arcade', 'category' => 'Iogames', 'provider' => 'Iogames', 'players' => 98, 'max_win' => '$3,000', 'rtp' => 94.5],
            ['title' => 'Basketball Stars', 'type' => 'Sports', 'category' => 'Iogames', 'provider' => 'Iogames', 'players' => 234, 'max_win' => '$4,000', 'is_new' => true, 'rtp' => 95.2],
            ['title' => 'Strike Bowling', 'type' => 'Sports', 'category' => 'Iogames', 'provider' => 'Iogames', 'players' => 67, 'max_win' => '$1,500', 'rtp' => 95.0],
            ['title' => 'Fighter', 'type' => 'Arcade', 'category' => 'Iogames', 'provider' => 'Iogames', 'players' => 189, 'max_win' => '$2,500', 'rtp' => 94.8],
            ['title' => 'Poker Classic', 'type' => 'Card', 'category' => 'Mascot', 'provider' => 'Mascot', 'players' => 445, 'max_win' => '400x', 'is_hot' => true, 'rtp' => 98.7],
            ['title' => 'Baccarat Pro', 'type' => 'Live', 'category' => 'Mascot', 'provider' => 'Mascot', 'players' => 567, 'max_win' => '8x', 'rtp' => 98.9],
            ['title' => 'Blackjack VIP', 'type' => 'Live', 'category' => 'Mascot', 'provider' => 'Mascot', 'players' => 234, 'max_win' => '3x', 'rtp' => 99.5],
            ['title' => 'Dragon Tiger', 'type' => 'Live', 'category' => 'Mascot', 'provider' => 'Mascot', 'players' => 389, 'max_win' => '40x', 'is_new' => true, 'rtp' => 96.3],
            ['title' => "Casino Hold'em", 'type' => 'Card', 'category' => 'Mascot', 'provider' => 'Mascot', 'players' => 112, 'max_win' => '100x', 'rtp' => 97.8],
            ['title' => 'War of Cards', 'type' => 'Card', 'category' => 'Mascot', 'provider' => 'Mascot', 'players' => 78, 'max_win' => '2x', 'rtp' => 97.1],
            ['title' => 'Gates of Olympus', 'type' => 'Slot', 'category' => 'Pragmatic Play', 'provider' => 'Pragmatic Play', 'players' => 1247, 'max_win' => '5,000x', 'is_hot' => true, 'is_featured' => true, 'rtp' => 96.5],
            ['title' => 'Sweet Bonanza', 'type' => 'Slot', 'category' => 'Pragmatic Play', 'provider' => 'Pragmatic Play', 'players' => 987, 'max_win' => '21,175x', 'is_hot' => true, 'is_featured' => true, 'rtp' => 96.5],
            ['title' => 'The Dog House', 'type' => 'Slot', 'category' => 'Pragmatic Play', 'provider' => 'Pragmatic Play', 'players' => 567, 'max_win' => '6,750x', 'rtp' => 96.5],
            ['title' => 'Wolf Gold', 'type' => 'Slot', 'category' => 'Pragmatic Play', 'provider' => 'Pragmatic Play', 'players' => 445, 'max_win' => '5,000x', 'rtp' => 96.0],
            ['title' => 'Big Bass Bonanza', 'type' => 'Slot', 'category' => 'Pragmatic Play', 'provider' => 'Pragmatic Play', 'players' => 678, 'max_win' => '2,100x', 'is_new' => true, 'rtp' => 96.7],
            ['title' => 'Starlight Princess', 'type' => 'Slot', 'category' => 'Pragmatic Play', 'provider' => 'Pragmatic Play', 'players' => 891, 'max_win' => '5,000x', 'is_hot' => true, 'rtp' => 96.5],
            ['title' => 'Live Roulette', 'type' => 'Live', 'category' => 'Evolution Gaming', 'provider' => 'Evolution Gaming', 'players' => 2345, 'max_win' => '40x', 'is_hot' => true, 'rtp' => 97.3],
            ['title' => 'Live Blackjack', 'type' => 'Live', 'category' => 'Evolution Gaming', 'provider' => 'Evolution Gaming', 'players' => 1876, 'max_win' => '3x', 'rtp' => 99.5],
            ['title' => 'Lightning Roulette', 'type' => 'Live', 'category' => 'Evolution Gaming', 'provider' => 'Evolution Gaming', 'players' => 3214, 'max_win' => '500x', 'is_hot' => true, 'is_featured' => true, 'rtp' => 97.3],
            ['title' => 'Crazy Time', 'type' => 'Live', 'category' => 'Evolution Gaming', 'provider' => 'Evolution Gaming', 'players' => 4521, 'max_win' => '20,000x', 'is_hot' => true, 'is_featured' => true, 'rtp' => 96.1],
            ['title' => 'Dream Catcher', 'type' => 'Live', 'category' => 'Evolution Gaming', 'provider' => 'Evolution Gaming', 'players' => 1234, 'max_win' => '40x', 'rtp' => 96.6],
            ['title' => 'Monopoly Live', 'type' => 'Live', 'category' => 'Evolution Gaming', 'provider' => 'Evolution Gaming', 'players' => 2109, 'max_win' => '4,000x', 'is_new' => true, 'rtp' => 96.2],
            ['title' => 'Starburst', 'type' => 'Slot', 'category' => 'Slots Classic', 'provider' => 'NetEnt', 'players' => 1567, 'max_win' => '500x', 'is_hot' => true, 'rtp' => 96.1],
            ['title' => 'Book of Ra', 'type' => 'Slot', 'category' => 'Slots Classic', 'provider' => 'Novomatic', 'players' => 987, 'max_win' => '5,000x', 'rtp' => 95.1],
            ['title' => 'Mega Moolah', 'type' => 'Slot', 'category' => 'Slots Classic', 'provider' => 'Microgaming', 'players' => 2345, 'max_win' => 'Progressive', 'is_hot' => true, 'rtp' => 88.1],
            ['title' => "Gonzo's Quest", 'type' => 'Slot', 'category' => 'Slots Classic', 'provider' => 'NetEnt', 'players' => 876, 'max_win' => '2,500x', 'rtp' => 96.0],
            ['title' => 'Cleopatra', 'type' => 'Slot', 'category' => 'Slots Classic', 'provider' => 'IGT', 'players' => 654, 'max_win' => '10,000x', 'rtp' => 95.0],
            ['title' => 'Thunderstruck II', 'type' => 'Slot', 'category' => 'Slots Classic', 'provider' => 'Microgaming', 'players' => 789, 'max_win' => '2,400x', 'rtp' => 96.7],
            ['title' => 'Speed Roulette', 'type' => 'Turbo', 'category' => 'Turbogames', 'provider' => 'Turbogames', 'players' => 456, 'max_win' => '35x', 'rtp' => 97.3],
            ['title' => 'Turbo Dice', 'type' => 'Turbo', 'category' => 'Turbogames', 'provider' => 'Turbogames', 'players' => 234, 'max_win' => '500x', 'rtp' => 97.0],
            ['title' => 'Flash Blackjack', 'type' => 'Turbo', 'category' => 'Turbogames', 'provider' => 'Turbogames', 'players' => 345, 'max_win' => '3x', 'rtp' => 99.3],
            ['title' => 'Quick Keno', 'type' => 'Turbo', 'category' => 'Turbogames', 'provider' => 'Turbogames', 'players' => 167, 'max_win' => '200x', 'is_new' => true, 'rtp' => 95.0],
            ['title' => 'Spin & Win', 'type' => 'Turbo', 'category' => 'Turbogames', 'provider' => 'Turbogames', 'players' => 289, 'max_win' => '50x', 'rtp' => 96.0],
            ['title' => 'Virtual Football', 'type' => 'Virtual', 'category' => 'Goldenrace', 'provider' => 'Goldenrace', 'players' => 789, 'max_win' => '$50,000', 'is_hot' => true, 'rtp' => 92.0],
            ['title' => 'Virtual Horses', 'type' => 'Virtual', 'category' => 'Goldenrace', 'provider' => 'Goldenrace', 'players' => 456, 'max_win' => '$25,000', 'rtp' => 92.0],
            ['title' => 'Virtual Basketball', 'type' => 'Virtual', 'category' => 'Goldenrace', 'provider' => 'Goldenrace', 'players' => 234, 'max_win' => '$30,000', 'rtp' => 92.0],
            ['title' => 'Virtual Tennis', 'type' => 'Virtual', 'category' => 'Goldenrace', 'provider' => 'Goldenrace', 'players' => 123, 'max_win' => '$20,000', 'rtp' => 92.0],
            ['title' => 'Bet on Poker', 'type' => 'TV', 'category' => 'TVBet', 'provider' => 'TVBet', 'players' => 567, 'max_win' => '$3,000', 'is_hot' => true, 'rtp' => 95.0],
            ['title' => 'War of Elements', 'type' => 'TV', 'category' => 'TVBet', 'provider' => 'TVBet', 'players' => 234, 'max_win' => '$500', 'rtp' => 95.0],
            ['title' => 'Andar Bahar', 'type' => 'TV', 'category' => 'TVBet', 'provider' => 'TVBet', 'players' => 345, 'max_win' => '$1,500', 'rtp' => 95.0],
            ['title' => 'FruitRace', 'type' => 'TV', 'category' => 'TVBet', 'provider' => 'TVBet', 'players' => 189, 'max_win' => '$2,000', 'is_new' => true, 'rtp' => 95.0],
            ['title' => 'WheelBet', 'type' => 'TV', 'category' => 'TVBet', 'provider' => 'TVBet', 'players' => 123, 'max_win' => '$1,000', 'rtp' => 95.0],
        ];

        foreach ($gamesData as $game) {
            $slug = Str::slug($game['title']);
            $catSlug = Str::slug($game['category']);
            Game::firstOrCreate(['slug' => $slug], array_merge($game, [
                'slug' => $slug,
                'game_category_id' => $categoryModels[$catSlug]->id ?? null,
                'is_hot' => $game['is_hot'] ?? false,
                'is_new' => $game['is_new'] ?? false,
                'is_featured' => $game['is_featured'] ?? false,
                'is_active' => true,
                'min_bet' => 0.10,
                'max_bet' => 10000.00,
            ]));
        }

        // Payment Methods
        $paymentMethods = [
            ['name' => 'Visa', 'type' => 'card', 'min_deposit' => 10, 'max_deposit' => 50000, 'processing_time' => 'Instant', 'sort_order' => 1],
            ['name' => 'Mastercard', 'type' => 'card', 'min_deposit' => 10, 'max_deposit' => 50000, 'processing_time' => 'Instant', 'sort_order' => 2],
            ['name' => 'Bitcoin', 'type' => 'crypto', 'min_deposit' => 20, 'max_deposit' => 100000, 'processing_time' => 'Within 1 hour', 'sort_order' => 3],
            ['name' => 'Ethereum', 'type' => 'crypto', 'min_deposit' => 20, 'max_deposit' => 100000, 'processing_time' => 'Within 1 hour', 'sort_order' => 4],
            ['name' => 'Skrill', 'type' => 'ewallet', 'min_deposit' => 10, 'max_deposit' => 25000, 'processing_time' => 'Instant', 'sort_order' => 5],
            ['name' => 'Neteller', 'type' => 'ewallet', 'min_deposit' => 10, 'max_deposit' => 25000, 'processing_time' => 'Instant', 'sort_order' => 6],
        ];

        foreach ($paymentMethods as $pm) {
            PaymentMethod::firstOrCreate(['name' => $pm['name']], array_merge($pm, ['is_active' => true]));
        }

        // Jackpots
        $jackpots = [
            ['name' => 'Silver Jackpot', 'type' => 'silver', 'amount' => 13329.00, 'seed_amount' => 10000, 'contribution_percent' => 0.5],
            ['name' => 'Gold Jackpot', 'type' => 'gold', 'amount' => 84235.00, 'seed_amount' => 50000, 'contribution_percent' => 1.0],
            ['name' => 'Max Jackpot', 'type' => 'max', 'amount' => 219573.00, 'seed_amount' => 100000, 'contribution_percent' => 2.0],
        ];

        foreach ($jackpots as $jackpot) {
            Jackpot::firstOrCreate(['type' => $jackpot['type']], array_merge($jackpot, ['is_active' => true]));
        }

        // Slider Banners
        $banners = [
            [
                'title'       => 'Welcome to the Ultimate Casino',
                'subtitle'    => 'Spin, bet & win — 500+ games awaiting you.',
                'image_url'   => 'https://images.unsplash.com/photo-1596838132731-3301c3fd4317?w=1600&q=80',
                'button_text' => 'Play Now',
                'button_url'  => '/casino',
                'badge'       => 'HOT',
                'target'      => 'all',
                'sort_order'  => 1,
            ],
            [
                'title'       => '100% First Deposit Bonus',
                'subtitle'    => 'Double your first deposit up to $500.',
                'image_url'   => 'https://images.unsplash.com/photo-1518998053901-5348d3961a04?w=1600&q=80',
                'button_text' => 'Claim Bonus',
                'button_url'  => '/promotions',
                'badge'       => 'NEW',
                'target'      => 'all',
                'sort_order'  => 2,
            ],
            [
                'title'       => 'Live Sports Betting',
                'subtitle'    => 'Best odds guaranteed on every match.',
                'image_url'   => 'https://images.unsplash.com/photo-1508098682722-e99c43a406b2?w=1600&q=80',
                'button_text' => 'Bet Now',
                'button_url'  => '/sports',
                'badge'       => null,
                'target'      => 'sports',
                'sort_order'  => 3,
            ],
            [
                'title'       => 'Live Casino — Real Dealers',
                'subtitle'    => 'Roulette, Blackjack, Baccarat & more 24/7.',
                'image_url'   => 'https://images.unsplash.com/photo-1549366021-9f761d450615?w=1600&q=80',
                'button_text' => 'Join Table',
                'button_url'  => '/casino/live',
                'badge'       => 'LIVE',
                'target'      => 'casino',
                'sort_order'  => 4,
            ],
        ];

        foreach ($banners as $banner) {
            SliderBanner::firstOrCreate(['title' => $banner['title']], array_merge($banner, ['is_active' => true]));
        }

        // Promotions
        $promotions = [
            ['title' => 'Weekly Cashback', 'slug' => 'weekly-cashback', 'type' => 'cashback', 'value' => 12, 'value_type' => 'percent', 'badge_text' => 'UP TO 12%', 'gradient' => 'from-red-600 to-red-900', 'cta_text' => 'Claim Cashback', 'sort_order' => 1, 'description' => 'Get up to 12% cashback on your weekly losses every Monday.'],
            ['title' => 'First Deposit Bonus', 'slug' => 'first-deposit', 'type' => 'welcome', 'value' => 100, 'value_type' => 'percent', 'min_deposit' => 20, 'max_bonus' => 500, 'badge_text' => '100% UP TO $500', 'gradient' => 'from-purple-600 to-purple-900', 'cta_text' => 'Get Bonus', 'sort_order' => 2, 'description' => 'Double your first deposit up to $500 with our welcome bonus.'],
            ['title' => 'VIP Program', 'slug' => 'vip-program', 'type' => 'vip', 'value' => 0, 'badge_text' => 'EXCLUSIVE REWARDS', 'gradient' => 'from-amber-500 to-amber-800', 'cta_text' => 'Join VIP', 'sort_order' => 3, 'description' => 'Unlock exclusive rewards, higher limits, and dedicated support.'],
            ['title' => 'Sports Betting Boost', 'slug' => 'sports-boost', 'type' => 'sports', 'value' => 0, 'badge_text' => 'BEST ODDS GUARANTEED', 'gradient' => 'from-emerald-500 to-emerald-800', 'cta_text' => 'Bet Now', 'sort_order' => 4, 'description' => 'We guarantee the best odds on all major sports events.'],
        ];

        foreach ($promotions as $promo) {
            Promotion::firstOrCreate(['slug' => $promo['slug']], array_merge($promo, ['is_active' => true, 'wagering_requirement' => 30]));
        }
    }
}
