*/ public function toArray(Request $request): array { $billingCycleLabels = [ 'monthly' => 'Hàng tháng', 'quarterly' => 'Hàng quý', 'semi_annually' => 'Nửa năm', 'annually' => 'Hàng năm', 'onetime' => 'Một lần', ]; $periodUnits = [ 'monthly' => 'month', 'quarterly' => 'quarter', 'semi_annually' => 'semi_year', 'annually' => 'year', 'onetime' => 'none', ]; return [ 'uuid' => $this->uuid, 'name' => $this->name, 'slug' => $this->slug, 'short_description' => $this->short_description, 'description' => $this->description, 'thumbnail' => $this->thumbnail ? asset($this->thumbnail) : null, 'product_type' => $this->product_type, 'specs' => [ 'cpu' => (int) $this->cpu, 'ram' => (int) $this->ram, 'disk' => (int) $this->disk, 'bandwidth' => (int) $this->bandwidth, 'gpu' => $this->gpu ], 'stock' => $this->stock, 'price' => (int) $this->price, 'price_sale' => $this->price_sale ? (int) $this->price_sale : null, 'group' => $this->category->name ?? null, 'os' => $this->osVersions->groupBy('os_group_id')->map(function ($versions) { $group = $versions->first()->osGroup; return [ 'name' => $group?->name, 'thumbnail' => $group?->thumbnail, 'versions' => $versions->map(fn($os) => [ 'id' => $os->id, 'version' => $os->version, 'name' => $os->name, ])->values(), ]; })->values(), 'pricing' => $this->pricings->map(fn($p) => [ 'id' => $p->id, 'name' => $billingCycleLabels[$p->billing_cycle] ?? $p->billing_cycle, 'price' => (int) $p->price, 'price_sale' => $p->price_sale ? (int) $p->price_sale : null, 'period' => $p->billing_cycle ? (int) match($p->billing_cycle) { 'monthly' => 1, 'quarterly' => 3, 'semi_annually' => 6, 'annually' => 12, default => 1, } : 1, 'period_unit' => $periodUnits[$p->billing_cycle] ?? 'month', ]), ]; } }