<?php

namespace Database\Seeders;

use App\Models\Branch;
use App\Models\Customer;
use Illuminate\Database\Seeder;

class CustomerSeeder extends Seeder
{
    public function run(): void
    {
        $branches = Branch::all();
        if ($branches->isEmpty()) {
            return;
        }

        $names = [
            ['first' => 'Emily', 'last' => 'Davis', 'type' => 'vip', 'gender' => 'female'],
            ['first' => 'Robert', 'last' => 'Brown', 'type' => 'regular', 'gender' => 'male'],
            ['first' => 'Lisa', 'last' => 'Anderson', 'type' => 'new', 'gender' => 'female'],
            ['first' => 'Maria', 'last' => 'Garcia', 'type' => 'regular', 'gender' => 'female'],
            ['first' => 'James', 'last' => 'Wilson', 'type' => 'vip', 'gender' => 'male'],
            ['first' => 'Sophie', 'last' => 'Taylor', 'type' => 'new', 'gender' => 'female'],
            ['first' => 'David', 'last' => 'Martinez', 'type' => 'regular', 'gender' => 'male'],
            ['first' => 'Emma', 'last' => 'Lee', 'type' => 'regular', 'gender' => 'female'],
        ];

        $counter = 1;
        foreach ($branches as $branch) {
            foreach ($names as $name) {
                $code = 'CUS' . str_pad($counter, 5, '0', STR_PAD_LEFT);
                $fullName = $name['first'] . ' ' . $name['last'];
                Customer::firstOrCreate(
                    ['customer_code' => $code],
                    [
                        'first_name' => $name['first'],
                        'last_name' => $name['last'],
                        'full_name' => $fullName,
                        'mobile' => '+1-555-' . str_pad($counter + 100, 4, '0', STR_PAD_LEFT),
                        'email' => strtolower($name['first']) . $counter . '@example.com',
                        'gender' => $name['gender'],
                        'customer_type' => $name['type'],
                        'status' => 'active',
                        'branch_id' => $branch->id,
                        'visit_count' => rand(1, 25),
                        'last_visit_at' => now()->subDays(rand(1, 45)),
                    ]
                );
                $counter++;
            }
        }
    }
}
