<?php

namespace Database\Seeders;

use Illuminate\Database\Seeder;
use App\Models\User;
use App\Models\Customer;
use Illuminate\Support\Facades\Hash;

class UserSeeder extends Seeder
{
    public function run(): void
    {
        // 1. Super Admin
        $admin = User::firstOrCreate(
            ['email' => 'admin@motosolbd.com'],
            [
                'name' => 'MotoSolution Super Admin',
                'phone' => '+8801714116695',
                'role' => 'super_admin',
                'status' => 'active',
                'password' => Hash::make('password'),
                'email_verified_at' => now(),
            ]
        );

        // 2. Auction Manager
        User::firstOrCreate(
            ['email' => 'auction@motosolbd.com'],
            [
                'name' => 'Tariqul Islam (Auction Specialist)',
                'phone' => '+8801711223344',
                'role' => 'auction_manager',
                'status' => 'active',
                'password' => Hash::make('password'),
                'email_verified_at' => now(),
            ]
        );

        // 3. Sales Manager
        User::firstOrCreate(
            ['email' => 'sales@motosolbd.com'],
            [
                'name' => 'Mahmudul Hasan (Sales Lead)',
                'phone' => '+8801811223344',
                'role' => 'sales_manager',
                'status' => 'active',
                'password' => Hash::make('password'),
                'email_verified_at' => now(),
            ]
        );

        // 4. 10 Realistic Demo Customers in Bangladesh
        $customersData = [
            [
                'name' => 'Tanvir Ahmed',
                'email' => 'tanvir.ahmed@gmail.com',
                'phone' => '01712345678',
                'city' => 'Dhaka',
                'district' => 'Dhaka',
                'address' => 'Road 11, Block D, Banani, Dhaka 1213',
                'preferred_contact' => 'whatsapp',
                'total_bids' => 4,
                'active_bids' => 1,
                'won_bids' => 1,
                'total_orders' => 1,
                'total_spent_bdt' => 6450000.00,
            ],
            [
                'name' => 'Dr. Farhana Rahman',
                'email' => 'farhana.rahman@outlook.com',
                'phone' => '01819876543',
                'city' => 'Dhaka',
                'district' => 'Dhaka',
                'address' => 'House 42, Road 7/A, Dhanmondi, Dhaka 1209',
                'preferred_contact' => 'call',
                'total_bids' => 3,
                'active_bids' => 1,
                'won_bids' => 1,
                'total_orders' => 1,
                'total_spent_bdt' => 4850000.00,
            ],
            [
                'name' => 'Engr. Kazi Shamsul Arefin',
                'email' => 'shamsul.arefin@yahoo.com',
                'phone' => '01911223344',
                'city' => 'Dhaka',
                'district' => 'Dhaka',
                'address' => 'Sector 4, Road 14, Uttara, Dhaka 1230',
                'preferred_contact' => 'whatsapp',
                'total_bids' => 2,
                'active_bids' => 1,
                'won_bids' => 0,
                'total_orders' => 0,
                'total_spent_bdt' => 0.00,
            ],
            [
                'name' => 'Mirza Shahadat Hossain',
                'email' => 'mirza.shahadat@gmail.com',
                'phone' => '01715566778',
                'city' => 'Chittagong',
                'district' => 'Chittagong',
                'address' => 'Nasirabad Housing Society, Road 3, Chittagong',
                'preferred_contact' => 'whatsapp',
                'total_bids' => 5,
                'active_bids' => 1,
                'won_bids' => 2,
                'total_orders' => 2,
                'total_spent_bdt' => 12500000.00,
            ],
            [
                'name' => 'Syed Navidul Huq',
                'email' => 'navid.huq@northsouth.edu',
                'phone' => '01678123456',
                'city' => 'Dhaka',
                'district' => 'Dhaka',
                'address' => 'Apartment 5B, Gulshan-2, Dhaka 1212',
                'preferred_contact' => 'whatsapp',
                'total_bids' => 3,
                'active_bids' => 0,
                'won_bids' => 1,
                'total_orders' => 1,
                'total_spent_bdt' => 7200000.00,
            ],
            [
                'name' => 'Barrister Anisur Rahman',
                'email' => 'anisur.law@gmail.com',
                'phone' => '01711998877',
                'city' => 'Dhaka',
                'district' => 'Dhaka',
                'address' => 'Bashundhara R/A, Block C, Road 5, Dhaka',
                'preferred_contact' => 'email',
                'total_bids' => 1,
                'active_bids' => 1,
                'won_bids' => 0,
                'total_orders' => 0,
                'total_spent_bdt' => 0.00,
            ],
            [
                'name' => 'Fahim Morshed Chowdhury',
                'email' => 'fahim.morshed@gmail.com',
                'phone' => '01844556677',
                'city' => 'Sylhet',
                'district' => 'Sylhet',
                'address' => 'Upashahar, Block B, Main Road, Sylhet',
                'preferred_contact' => 'whatsapp',
                'total_bids' => 2,
                'active_bids' => 0,
                'won_bids' => 1,
                'total_orders' => 1,
                'total_spent_bdt' => 3950000.00,
            ],
            [
                'name' => 'Saifuddin Ahmed',
                'email' => 'saif.ahmed@primebank.com',
                'phone' => '01912345670',
                'city' => 'Dhaka',
                'district' => 'Dhaka',
                'address' => 'Niketan, Block E, Road 1, Gulshan, Dhaka 1212',
                'preferred_contact' => 'call',
                'total_bids' => 1,
                'active_bids' => 1,
                'won_bids' => 0,
                'total_orders' => 0,
                'total_spent_bdt' => 0.00,
            ],
            [
                'name' => 'Zubair Al Mamun',
                'email' => 'zubair.mamun@gmail.com',
                'phone' => '01714433221',
                'city' => 'Khulna',
                'district' => 'Khulna',
                'address' => 'Sonadanga R/A, Phase 2, Khulna',
                'preferred_contact' => 'whatsapp',
                'total_bids' => 2,
                'active_bids' => 0,
                'won_bids' => 0,
                'total_orders' => 0,
                'total_spent_bdt' => 0.00,
            ],
            [
                'name' => 'Nusrat Jahan',
                'email' => 'nusrat.jahan.bd@gmail.com',
                'phone' => '01811234987',
                'city' => 'Dhaka',
                'district' => 'Dhaka',
                'address' => 'Mirpur DOHS, Road 9, Avenue 3, Dhaka',
                'preferred_contact' => 'whatsapp',
                'total_bids' => 1,
                'active_bids' => 1,
                'won_bids' => 0,
                'total_orders' => 0,
                'total_spent_bdt' => 0.00,
            ],
        ];

        foreach ($customersData as $index => $data) {
            $user = User::firstOrCreate(
                ['email' => $data['email']],
                [
                    'name' => $data['name'],
                    'phone' => $data['phone'],
                    'role' => 'customer',
                    'status' => 'active',
                    'password' => Hash::make('password'),
                    'email_verified_at' => now(),
                    'last_login_at' => now()->subDays(rand(1, 10)),
                ]
            );

            Customer::firstOrCreate(
                ['user_id' => $user->id],
                [
                    'customer_code' => 'MS-CUST-' . (1000 + $index + 1),
                    'full_name' => $data['name'],
                    'phone' => $data['phone'],
                    'email' => $data['email'],
                    'address' => $data['address'],
                    'city' => $data['city'],
                    'district' => $data['district'],
                    'preferred_contact' => $data['preferred_contact'],
                    'total_bids' => $data['total_bids'],
                    'active_bids' => $data['active_bids'],
                    'won_bids' => $data['won_bids'],
                    'lost_bids' => max(0, $data['total_bids'] - $data['won_bids'] - $data['active_bids']),
                    'total_orders' => $data['total_orders'],
                    'total_spent_bdt' => $data['total_spent_bdt'],
                    'status' => 'verified',
                    'internal_notes' => 'Valued customer verified by MotoSolution sales team.',
                ]
            );
        }
    }
}
