findCustomerMatch($row); $payload = $this->buildCustomerPayload($row); if (! $match) { $existingById = Customer::withTrashed()->find($sourceId); if ($preview) { $summary['customers']['created']++; $idMap[$sourceId] = $existingById?->id ?? $sourceId; continue; } if (! $existingById) { $payload['id'] = $sourceId; } $payload['uuid'] = (string) Str::uuid(); $customerId = DB::table('customers')->insertGetId($payload); $summary['customers']['created']++; $idMap[$sourceId] = $customerId; continue; } $idMap[$sourceId] = $match->id; if ($preview) { $summary['customers']['updated']++; continue; } $restored = method_exists($match, 'trashed') && $match->trashed(); if ($restored) { $match->restore(); $summary['customers']['restored']++; } DB::table('customers') ->where('id', $match->id) ->update($payload); $summary['customers']['updated']++; } return $idMap; } private function findCustomerMatch(array $row): ?Customer { $email = $this->nullableString($row['email'] ?? null); if ($email) { $match = Customer::withTrashed()->where('email', $email)->first(); if ($match) { return $match; } } $username = $this->nullableString($row['username'] ?? null); if ($username && Schema::hasColumn('customers', 'username')) { return Customer::withTrashed()->where('username', $username)->first(); } return null; } private function buildCustomerPayload(array $row): array { $email = $this->nullableString($row['email'] ?? null); return [ 'fullname' => $this->nullableString($row['name'] ?? null) ?? $this->nullableString($row['username'] ?? null) ?? $email, 'username' => $this->nullableString($row['username'] ?? null), 'email' => $email, 'email_verified_at' => $this->normalizeDateTime($row['email_verified_at'] ?? null), 'password' => $this->nullableString($row['password'] ?? null), 'type' => $this->nullableString($row['type'] ?? null, 'guest'), 'remember_token' => $this->nullableString($row['remember_token'] ?? null), 'status' => $this->mapCustomerStatus($row['status'] ?? null), 'created_at' => $this->normalizeDateTime($row['created_at'] ?? null) ?? now(), 'updated_at' => $this->normalizeDateTime($row['updated_at'] ?? null) ?? now(), ]; } private function mapCustomerStatus(mixed $status): int { return match (strtolower(trim((string) $status))) { 'verified', 'active', 'activated', '1' => 1, 'unverified', 'inactive', 'deactivated', '0', '2' => 2, default => 2, }; } }