where('is_active', true) ->orderBy('sort_order', 'asc') ->orderBy('name', 'asc') ->get() ->toArray(); return array_map(function ($item) { return [ 'id' => $item->id, 'name' => $item->name, 'position' => $item->position, 'email' => $item->email, 'phone' => $item->phone, 'department' => $item->department, 'sort_order' => $item->sort_order, ]; }, $contacts); } catch (\Exception $e) { Log::error('Error fetching active contacts: ' . $e->getMessage()); throw $e; } } /** * Get all contact settings (including inactive) for admin */ public function getAllContactSettings(): array { try { $settings = DB::table('contact_settings') ->orderBy('sort_order', 'asc') ->orderBy('name', 'asc') ->get() ->toArray(); return array_map(function ($item) { return [ 'id' => $item->id, 'name' => $item->name, 'position' => $item->position, 'email' => $item->email, 'phone' => $item->phone, 'department' => $item->department, 'is_active' => (bool) $item->is_active, 'sort_order' => $item->sort_order, 'created_at' => $item->created_at, 'updated_at' => $item->updated_at, ]; }, $settings); } catch (\Exception $e) { Log::error('Error fetching all contact settings: ' . $e->getMessage()); throw $e; } } /** * Update contact settings (bulk update) */ public function updateContactSettings(array $contactData): array { try { // Clear existing settings DB::table('contact_settings')->truncate(); // Insert new settings $insertData = []; foreach ($contactData as $index => $item) { $insertData[] = [ 'name' => $item['name'], 'position' => $item['position'] ?? null, 'email' => $item['email'] ?? null, 'phone' => $item['phone'] ?? null, 'department' => $item['department'] ?? null, 'is_active' => $item['is_active'] ?? true, 'sort_order' => $item['sort_order'] ?? $index, 'created_at' => now(), 'updated_at' => now(), ]; } DB::table('contact_settings')->insert($insertData); // Return updated settings return $this->getAllContactSettings(); } catch (\Exception $e) { Log::error('Error updating contact settings: ' . $e->getMessage()); throw $e; } } /** * Add a single contact person */ public function addContactPerson(array $data): array { try { $id = DB::table('contact_settings')->insertGetId([ 'name' => $data['name'], 'position' => $data['position'] ?? null, 'email' => $data['email'] ?? null, 'phone' => $data['phone'] ?? null, 'department' => $data['department'] ?? null, 'is_active' => $data['is_active'] ?? true, 'sort_order' => $data['sort_order'] ?? 0, 'created_at' => now(), 'updated_at' => now(), ]); return $this->getAllContactSettings(); } catch (\Exception $e) { Log::error('Error adding contact person: ' . $e->getMessage()); throw $e; } } /** * Update a single contact person */ public function updateContactPerson(int $id, array $data): array { try { DB::table('contact_settings') ->where('id', $id) ->update([ 'name' => $data['name'], 'position' => $data['position'] ?? null, 'email' => $data['email'] ?? null, 'phone' => $data['phone'] ?? null, 'department' => $data['department'] ?? null, 'is_active' => $data['is_active'] ?? true, 'sort_order' => $data['sort_order'] ?? 0, 'updated_at' => now(), ]); return $this->getAllContactSettings(); } catch (\Exception $e) { Log::error('Error updating contact person: ' . $e->getMessage()); throw $e; } } /** * Delete a contact person */ public function deleteContactPerson(int $id): array { try { DB::table('contact_settings')->where('id', $id)->delete(); return $this->getAllContactSettings(); } catch (\Exception $e) { Log::error('Error deleting contact person: ' . $e->getMessage()); throw $e; } } /** * Toggle active status of a contact person */ public function toggleContactPerson(int $id): array { try { $contact = DB::table('contact_settings')->where('id', $id)->first(); if (!$contact) { throw new \Exception('Contact person not found'); } DB::table('contact_settings') ->where('id', $id) ->update([ 'is_active' => !$contact->is_active, 'updated_at' => now(), ]); return $this->getAllContactSettings(); } catch (\Exception $e) { Log::error('Error toggling contact person: ' . $e->getMessage()); throw $e; } } }