<?php

namespace Database\Seeders;

use Illuminate\Database\Seeder;
use App\Models\User;
use App\Models\Seller;
use App\Models\Wallet;
use App\Models\Rank;
use Illuminate\Support\Facades\Hash;

class UsersSeeder extends Seeder
{
    public function run(): void
    {
        $this->createAdmin();
        $this->createTestUsers();
        $this->createTestSeller();
    }

    protected function createAdmin(): void
    {
        $admin = User::updateOrCreate(
            ['email' => 'admin@moniboss.shop'],
            [
                'name' => 'Administrateur',
                'first_name' => 'Admin',
                'last_name' => 'MoniBoss',
                'email' => 'admin@moniboss.shop',
                'password' => Hash::make('Admin123!'),
                'role' => 'admin',
                'member_code' => 'ADM001',
                'referral_code' => 'ADMIN001',
                'is_active' => true,
                'email_verified_at' => now(),
            ]
        );

        Wallet::firstOrCreate(
            ['user_id' => $admin->id],
            ['balance' => 0, 'pending_balance' => 0, 'currency' => 'XOF']
        );
    }

    protected function createTestUsers(): void
    {
        $defaultRank = Rank::where('slug', 'debutant')->first();

        $user1 = User::updateOrCreate(
            ['email' => 'member@moniboss.shop'],
            [
                'name' => 'Membre Test',
                'first_name' => 'Membre',
                'last_name' => 'Test',
                'email' => 'member@moniboss.shop',
                'password' => Hash::make('Password123!'),
                'role' => 'member',
                'member_code' => 'MEM001',
                'referral_code' => 'MEMBER001',
                'phone' => '+225 0701234567',
                'country' => 'Côte d\'Ivoire',
                'country_code' => 'CI',
                'city' => 'Abidjan',
                'address' => 'Cocody, Abidjan',
                'rank_id' => $defaultRank?->id,
                'is_active' => true,
                'email_verified_at' => now(),
            ]
        );

        Wallet::firstOrCreate(
            ['user_id' => $user1->id],
            ['balance' => 50000, 'pending_balance' => 0, 'currency' => 'XOF']
        );

        $user2 = User::updateOrCreate(
            ['email' => 'marie@moniboss.shop'],
            [
                'name' => 'Marie Koné',
                'first_name' => 'Marie',
                'last_name' => 'Koné',
                'email' => 'marie@moniboss.shop',
                'password' => Hash::make('Password123!'),
                'role' => 'member',
                'member_code' => 'MEM002',
                'referral_code' => 'MARIE002',
                'phone' => '+225 0707654321',
                'country' => 'Côte d\'Ivoire',
                'country_code' => 'CI',
                'city' => 'Abidjan',
                'address' => 'Marcory, Abidjan',
                'sponsor_id' => $user1->id,
                'rank_id' => $defaultRank?->id,
                'is_active' => true,
                'email_verified_at' => now(),
            ]
        );

        Wallet::firstOrCreate(
            ['user_id' => $user2->id],
            ['balance' => 25000, 'pending_balance' => 0, 'currency' => 'XOF']
        );
    }

    protected function createTestSeller(): void
    {
        $sellerUser = User::updateOrCreate(
            ['email' => 'seller@moniboss.shop'],
            [
                'name' => 'Boutique Mode CI',
                'first_name' => 'Amadou',
                'last_name' => 'Traoré',
                'email' => 'seller@moniboss.shop',
                'password' => Hash::make('Seller123!'),
                'role' => 'user',
                'member_code' => 'SLR001',
                'referral_code' => 'SELLER01',
                'phone' => '+225 0501234567',
                'country' => 'Côte d\'Ivoire',
                'country_code' => 'CI',
                'city' => 'Abidjan',
                'address' => 'Zone 4, Marcory',
                'is_active' => true,
                'email_verified_at' => now(),
            ]
        );

        Seller::updateOrCreate(
            ['user_id' => $sellerUser->id],
            [
                'shop_name' => 'Boutique Mode CI',
                'shop_slug' => 'boutique-mode-ci',
                'description' => 'La meilleure boutique de mode en Côte d\'Ivoire',
                'logo' => null,
                'banner' => null,
                'phone' => '+225 0501234567',
                'email' => 'seller@moniboss.shop',
                'address' => 'Zone 4, Marcory, Abidjan',
                'city' => 'Abidjan',
                'country' => 'Côte d\'Ivoire',
                'is_active' => true,
                'commission_rate' => 10,
                'is_verified' => true,
                'kyc_status' => 'approved',
            ]
        );

        Wallet::firstOrCreate(
            ['user_id' => $sellerUser->id],
            ['balance' => 100000, 'pending_balance' => 25000, 'currency' => 'XOF']
        );
    }
}
