33namespace Eclipse \Catalogue \Seeders ;
44
55use Eclipse \Catalogue \Models \Category ;
6+ use Eclipse \Catalogue \Models \Group ;
67use Eclipse \Catalogue \Models \Product ;
78use Eclipse \Catalogue \Models \ProductData ;
89use Eclipse \Catalogue \Models \ProductType ;
@@ -17,6 +18,7 @@ public function run(): void
1718 {
1819 $ this ->ensureSampleImagesExist ();
1920 $ this ->ensureProductTypesExist ();
21+ $ this ->ensureGroupsExist ();
2022
2123 $ productTypes = ProductType::all ();
2224
@@ -33,7 +35,7 @@ public function run(): void
3335
3436 $ products = Product::query ()->latest ('id ' )->take (100 )->get ();
3537
36- foreach ($ products as $ product ) {
38+ foreach ($ products as $ index => $ product ) {
3739 if ($ tenantFK && $ tenantModel && class_exists ($ tenantModel )) {
3840 $ tenants = $ tenantModel ::all ();
3941 foreach ($ tenants as $ tenant ) {
@@ -50,6 +52,14 @@ public function run(): void
5052 'has_free_delivery ' => false ,
5153 'category_id ' => $ categoryId ,
5254 ]);
55+
56+ // Get groups for this specific tenant
57+ $ tenantGroups = Group::where ($ tenantFK , $ tenant ->id )->get ();
58+ $ groupsToAdd = $ this ->determineGroupsForProduct ($ index , $ tenantGroups );
59+
60+ foreach ($ groupsToAdd as $ group ) {
61+ $ group ->addProduct ($ product );
62+ }
5363 }
5464 } else {
5565 $ categoryId = Category::query ()->inRandomOrder ()->value ('id ' );
@@ -60,10 +70,34 @@ public function run(): void
6070 'has_free_delivery ' => false ,
6171 'category_id ' => $ categoryId ,
6272 ]);
73+
74+ // For non-tenant scenarios, use all groups
75+ $ groups = Group::all ();
76+ $ groupsToAdd = $ this ->determineGroupsForProduct ($ index , $ groups );
77+ foreach ($ groupsToAdd as $ group ) {
78+ $ group ->addProduct ($ product );
79+ }
6380 }
6481 }
6582 }
6683
84+ private function determineGroupsForProduct (int $ productIndex , $ groups ): array
85+ {
86+ $ groupsToAdd = [];
87+
88+ // Randomly assign 1-3 groups per product
89+ $ numGroupsToAdd = rand (1 , min (3 , $ groups ->count ()));
90+
91+ // Get random groups for this product
92+ $ randomGroups = $ groups ->random ($ numGroupsToAdd );
93+
94+ foreach ($ randomGroups as $ group ) {
95+ $ groupsToAdd [] = $ group ;
96+ }
97+
98+ return $ groupsToAdd ;
99+ }
100+
67101 private function ensureSampleImagesExist (): void
68102 {
69103 Storage::disk ('public ' )->makeDirectory ('sample-products ' );
@@ -109,4 +143,13 @@ private function ensureProductTypesExist(): void
109143 $ this ->call (ProductTypeSeeder::class);
110144 }
111145 }
146+
147+ private function ensureGroupsExist (): void
148+ {
149+ $ groups = Group::all ();
150+
151+ if ($ groups ->isEmpty ()) {
152+ $ this ->call (GroupSeeder::class);
153+ }
154+ }
112155}
0 commit comments