# Product CSV Import

Install the dependency after freeing enough space for Composer's cache:

```bash
composer update maatwebsite/excel
php artisan migrate
```

The protected endpoint is:

```text
POST /api/admin/products/import
```

Send the spreadsheet as a multipart `file` field. Accepted formats are CSV, TXT, XLS, and XLSX, up to 20 MB. The first row must contain headings. `category_id` may be a numeric category ID, or use `category` / `category_name` with the category's exact name (case-insensitive).

Required columns are `name`, `carat_weight`, and `price`, plus a category column (`category_id`, `category`, or `category_name`). `stock` and `quantity` are not CSV columns; imported products default to stock 10 and gemstone quantity 1. Optional product columns are `description` and `video_url`; video URLs may point to YouTube or another publicly accessible video host. Optional gemstone-specific CSV columns are `type`, `sub_type`, `insert_type`, `color`, `size`, `quality`, `length`, `width`, `height`, and `cut_shapes`. Clarity is not imported from CSV. The gemstone's `name`, `slug`, `price`, and `description` are populated from the matching product values; `stock_status` is set to `in_stock`, and `product_id` is assigned automatically. Imported products are published. The storefront product APIs include the saved URL in the `video_url` field, just like products created through the add-product form.

To attach FTP-uploaded images, include the optional `image_names` column. Put the exact filenames in display order, separated by `|` (pipe), for example `sapphire-front.jpg|sapphire-side.jpg`. Upload those files directly into this folder before importing (do not create an `import` subfolder):

```text
storage/app/public/products/
```

For example, `oval_gem.png` must be at `storage/app/public/products/oval_gem.png`. On a standard Laravel hosting setup this folder is exposed through `public/storage/products/`. Use filenames only (no paths or subfolders); supported extensions are JPG, JPEG, PNG, GIF, and WebP. Filenames are matched exactly, including letter case. Every named file must exist or that CSV row is skipped with a validation failure. The first image becomes the product's primary image; all images are also attached as product images, preserving the CSV order.

Example CSV:

```csv
name,category_id,carat_weight,price,description,type,sub_type,insert_type,color,size,quality,length,width,height,cut_shapes,image_names,video_url
"Natural Blue Sapphire",2,3.25,1250.00,"Oval blue sapphire with excellent brilliance",Sapphire,Corundum,Loose,Blue,8x6 mm,"VVS(eye-clean)",8,6,4,"Oval Cut",sapphire-front.jpg|sapphire-side.jpg,https://www.youtube.com/watch?v=sample123
"Rose Quartz Cabochon",2,12.50,85.00,"Polished rose quartz cabochon",Quartz,,Cabochon,Pink,20 mm,"SI(Loupe clean)",20,15,8,"Dome (Cabochon Cut)",rose-quartz.jpg,
```

Each valid row creates a `products` record and its related `gemstones` record. Invalid rows are skipped and returned in `validation_failures`; unexpected row errors are returned in `errors` and logged.