<?php

namespace Database\Seeders;

use App\Models\Product;
use Illuminate\Database\Seeder;

class ProductSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        Product::truncate();
        $services = [
            [
                'name' => 'Luxurious Vanilla',
                'price' => '4.99',
                'category_id' => 1,
                'color_id' => [1, 2],
                'description' => 'Lorem ipsum dolor sit amet, consectetur adipiscing elit,
                 sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
                 Ut enim ad minim veniam, quis nostrud exercitation ullamco 
                 laboris nisi ut aliquip ex ea commodo consequat.',
                'product_image' => 'front/images/product19.jpeg',
                'product_gallery_images' => [
                    'front/images/product10.jpeg',
                    'front/images/product10.jpeg',
                    'front/images/product10.jpeg',
                ],
                
            ],
            [
                'name' => 'Paris Romance',
                'price' => '22.50',
                'category_id' => 1,
                'color_id' => [1, 2],

                'description' => 'Lorem ipsum dolor sit amet, consectetur adipiscing elit,
                sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
                Ut enim ad minim veniam, quis nostrud exercitation ullamco 
                laboris nisi ut aliquip ex ea commodo consequat.',
                'product_image' => 'front/images/product23.jpg',
                'product_gallery_images' => [
                    'front/images/product10.jpeg',
                    'front/images/product10.jpeg',
                    'front/images/product10.jpeg',
                ],
            ],
            [
                'name' => 'Fifth Avenue',
                'price' => '42.00',
                'category_id' => 1,
                'color_id' => [1, 2],

                'description' => 'Lorem ipsum dolor sit amet, consectetur adipiscing elit,
                sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
                Ut enim ad minim veniam, quis nostrud exercitation ullamco 
                laboris nisi ut aliquip ex ea commodo consequat.',
                'product_image' => 'front/images/product14.jpeg',
                'product_gallery_images' => [
                    'front/images/product10.jpeg',
                    'front/images/product10.jpeg',
                    'front/images/product10.jpeg',
                ],
            ],
            [
                'name' => 'The Whispers of Greece',
                'price' => '24.00',
                'category_id' => 1,
                'color_id' => [1, 2],

                'colors_ids' => ["1", "3", "4"],
                'description' => 'Lorem ipsum dolor sit amet, consectetur adipiscing elit,
                sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
                Ut enim ad minim veniam, quis nostrud exercitation ullamco 
                laboris nisi ut aliquip ex ea commodo consequat.',
                'product_image' => 'front/images/product13.jpeg',
                'product_gallery_images' => [
                    'front/images/product10.jpeg',
                    'front/images/product10.jpeg',
                    'front/images/product10.jpeg',
                ],
            ],

            [
                'name' => 'Barcelona Breeze',
                'price' => '44.00',
                'category_id' => 1,
                'color_id' => [1, 2],

                'colors_ids' => ["4", "2", "1"],
                'description' => 'Lorem ipsum dolor sit amet, consectetur adipiscing elit,
                sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
                Ut enim ad minim veniam, quis nostrud exercitation ullamco 
                laboris nisi ut aliquip ex ea commodo consequat.',
                'product_image' => 'front/images/product15.jpeg',
                'product_gallery_images' => [
                    'front/images/product10.jpeg',
                    'front/images/product10.jpeg',
                    'front/images/product10.jpeg',
                ],
            ],

            [
                'name' => 'Eucalyptus Essential Oil',
                'price' => '4.99',
                'category_id' => 2,
                'color_id' => [1, 2],

                'description' => 'Lorem ipsum dolor sit amet, consectetur adipiscing elit,
                sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
                Ut enim ad minim veniam, quis nostrud exercitation ullamco 
                laboris nisi ut aliquip ex ea commodo consequat.',
                'product_image' => 'front/images/product22.jpg',
                'product_gallery_images' => [
                    'front/images/product10.jpeg',
                    'front/images/product10.jpeg',
                    'front/images/product10.jpeg',
                ],
            ],

            [
                'name' => 'Eucalyptus Essential Oil',
                'price' => '4.99',
                'category_id' => 2,
                'color_id' => [1, 2],

                'description' => 'Lorem ipsum dolor sit amet, consectetur adipiscing elit,
                sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
                Ut enim ad minim veniam, quis nostrud exercitation ullamco 
                laboris nisi ut aliquip ex ea commodo consequat.',
                'product_image' => 'front/images/product21.jpg',
                'product_gallery_images' => [
                    'front/images/product10.jpeg',
                    'front/images/product10.jpeg',
                    'front/images/product10.jpeg',
                ],
            ],

        ];


        foreach ($services as $serviceData) {
            $ourService = Product::create([
                'name' => $serviceData['name'],
                'description' => $serviceData['description'],
                'price' => $serviceData['price'],
                'category_id' => $serviceData['category_id'],
            ]);

            $fullPath = public_path($serviceData['product_image']);
            if (file_exists($fullPath)) {
                if (!$ourService->getMedia('product_image')->where('file_name', basename($fullPath))->first()) {
                    $ourService->addMedia($fullPath)
                        ->preservingOriginal()
                        ->toMediaCollection('product_image');
                }
            }
            if (!empty($serviceData['product_gallery_images'])) {
                foreach ($serviceData['product_gallery_images'] as $galleryImagePath) {
                    $galleryFullPath = public_path($galleryImagePath);
                    if (file_exists($galleryFullPath)) {
                        if (!$ourService->getMedia('product_gallery_images')->where('file_name', basename($galleryFullPath))->first()) {
                            $ourService->addMedia($galleryFullPath)
                                ->preservingOriginal()
                                ->toMediaCollection('product_gallery_images');
                        }
                    }
                }
            }
            if (!empty($serviceData['color_id'])) {
                $ourService->colors()->sync($serviceData['color_id']);
            }
        }

    }
}
