*/ public function toArray(Request $request): array { return [ 'id' => $this->id, 'file_id' => $this->file_id, 'label' => $this->label, 'sort_order' => $this->sort_order, 'attachable_type' => $this->attachable_type, 'attachable_id' => $this->attachable_id, 'created_at' => $this->created_at->format('Y-m-d H:i:s'), 'updated_at' => $this->updated_at->format('Y-m-d H:i:s'), // File information 'file' => $this->whenLoaded('file', function () { return [ 'id' => $this->file->id, 'name' => $this->file->name, 'original_name' => $this->file->original_name ?? $this->file->name, 'path' => $this->file->path, 'mime_type' => $this->file->mime_type, 'size' => $this->file->size, 'size_formatted' => $this->formatFileSize($this->file->size ?? 0), 'extension' => pathinfo($this->file->name, PATHINFO_EXTENSION), 'download_url' => url('/api/files/' . $this->file->id . '/download'), ]; }), // Attachable model information 'attachable' => $this->whenLoaded('attachable', function () { return [ 'id' => $this->attachable->id, 'type' => class_basename($this->attachable), 'name' => $this->getAttachableName(), ]; }), // Helper methods 'is_for_intervention' => $this->isForIntervention(), 'is_for_client' => $this->isForClient(), 'is_for_deceased' => $this->isForDeceased(), 'download_url' => $this->downloadUrl, ]; } /** * Format file size in human readable format */ private function formatFileSize(int $bytes): string { $units = ['B', 'KB', 'MB', 'GB', 'TB']; $bytes = max($bytes, 0); $pow = floor(($bytes ? log($bytes) : 0) / log(1024)); $pow = min($pow, count($units) - 1); $bytes /= (1 << (10 * $pow)); return round($bytes, 2) . ' ' . $units[$pow]; } /** * Get the display name of the attached model */ private function getAttachableName(): string { if (!$this->attachable) { return 'Unknown'; } return match (get_class($this->attachable)) { \App\Models\Intervention::class => $this->attachable->title ?? "Intervention #{$this->attachable->id}", \App\Models\Client::class => $this->attachable->name ?? "Client #{$this->attachable->id}", \App\Models\Deceased::class => $this->attachable->name ?? "Deceased #{$this->attachable->id}", default => 'Unknown Model' }; } }