<?php

namespace Database\Seeders;

use App\Models\Product;
use App\Models\ProductCategory;
use App\Models\ProductImage;
use App\Models\ProductVariant;
use App\Models\LabReport;
use Illuminate\Database\Seeder;
use Illuminate\Support\Str;

class ProductSeeder extends Seeder
{
    public function run(): void
    {
        // Categories
        $peptides = ProductCategory::firstOrCreate(
            ['slug' => 'peptides'],
            ['name' => 'Peptides', 'description' => 'Research peptides', 'sort_order' => 1]
        );

        $growthFactors = ProductCategory::firstOrCreate(
            ['slug' => 'growth-factors'],
            ['name' => 'Growth Factors', 'description' => 'Growth factor peptides', 'sort_order' => 2]
        );

        $antiAging = ProductCategory::firstOrCreate(
            ['slug' => 'anti-aging'],
            ['name' => 'Anti-Aging', 'description' => 'Anti-aging compounds', 'sort_order' => 3]
        );

        // Products
        $products = [
            [
                'name' => 'BPC-157 5mg',
                'slug' => 'bpc-157-5mg',
                'short_description' => 'Body Protection Compound. 99%+ purity, HPLC tested.',
                'description' => '<p>BPC-157 (Body Protection Compound) is a pentadecapeptide made up of 15 amino acids. It is a partial sequence of body protection compound that is found in and isolated from human gastric juice.</p><p>Our BPC-157 is produced to the highest research standards with a purity of ≥99% as verified by third-party HPLC testing.</p>',
                'price' => 39.99,
                'compare_price' => 49.99,
                'stock_quantity' => 150,
                'is_featured' => true,
                'is_bestseller' => true,
                'weight_grams' => 5,
                'categories' => [$peptides->id],
                'lab' => ['lab_name' => 'Janoshik', 'test_type' => 'HPLC', 'purity' => 99.4],
            ],
            [
                'name' => 'TB-500 5mg',
                'slug' => 'tb-500-5mg',
                'short_description' => 'Thymosin Beta-4 fragment. 99%+ purity, mass spec verified.',
                'description' => '<p>TB-500 is a synthetic fraction of the protein Thymosin Beta-4, present in virtually all human and animal cells. The main purpose of this peptide is to promote healing.</p><p>Each vial contains 5mg of TB-500 with ≥99% purity confirmed by mass spectrometry analysis.</p>',
                'price' => 44.99,
                'compare_price' => null,
                'stock_quantity' => 85,
                'is_featured' => true,
                'is_bestseller' => false,
                'weight_grams' => 5,
                'categories' => [$peptides->id, $antiAging->id],
                'lab' => ['lab_name' => 'Janoshik', 'test_type' => 'mass_spec', 'purity' => 99.1],
            ],
            [
                'name' => 'Ipamorelin 5mg',
                'slug' => 'ipamorelin-5mg',
                'short_description' => 'Selective GH secretagogue. 99%+ purity, HPLC tested.',
                'description' => '<p>Ipamorelin is a pentapeptide (Aib-His-D-2-Nal-D-Phe-Lys-NH2) and a selective growth hormone secretagogue and ghrelin mimetic.</p>',
                'price' => 34.99,
                'compare_price' => null,
                'stock_quantity' => 0,
                'allow_preorder' => true,
                'preorder_note' => 'New stock arriving within 7–10 days',
                'is_featured' => false,
                'is_bestseller' => true,
                'weight_grams' => 5,
                'categories' => [$peptides->id, $growthFactors->id],
                'lab' => ['lab_name' => 'PeptideVerify', 'test_type' => 'both', 'purity' => 99.7],
            ],
            [
                'name' => 'CJC-1295 DAC 2mg',
                'slug' => 'cjc-1295-dac-2mg',
                'short_description' => 'Long-acting GHRH analogue. 98%+ purity.',
                'description' => '<p>CJC-1295 with DAC (Drug Affinity Complex) is a synthetic analogue of growth hormone-releasing hormone (GHRH). The addition of DAC significantly extends its half-life.</p>',
                'price' => 29.99,
                'compare_price' => null,
                'stock_quantity' => 200,
                'is_featured' => false,
                'is_bestseller' => false,
                'weight_grams' => 2,
                'categories' => [$growthFactors->id],
                'lab' => ['lab_name' => 'Janoshik', 'test_type' => 'HPLC', 'purity' => 98.8],
            ],
            [
                'name' => 'Epithalon 10mg',
                'slug' => 'epithalon-10mg',
                'short_description' => 'Tetrapeptide telomerase activator. 99%+ purity.',
                'description' => '<p>Epithalon (also known as Epitalon or Epithalone) is a synthetic tetrapeptide (Ala-Glu-Asp-Gly) which is the synthetic version of Epithalamin.</p>',
                'price' => 49.99,
                'compare_price' => 59.99,
                'stock_quantity' => 60,
                'is_featured' => true,
                'is_bestseller' => false,
                'weight_grams' => 10,
                'categories' => [$antiAging->id],
                'lab' => ['lab_name' => 'PeptideVerify', 'test_type' => 'both', 'purity' => 99.2],
            ],
        ];

        foreach ($products as $data) {
            $categories = $data['categories'];
            $labData = $data['lab'];
            unset($data['categories'], $data['lab']);

            $product = Product::updateOrCreate(
                ['slug' => $data['slug']],
                array_merge($data, [
                    'status' => 'active',
                    'track_stock' => true,
                    'meta_title' => $data['name'] . ' | BioSupply UK',
                    'meta_description' => $data['short_description'],
                ])
            );

            $product->categories()->sync($categories);

            ProductImage::firstOrCreate(
                ['product_id' => $product->id, 'sort_order' => 0],
                [
                    'url' => "https://placehold.co/600x600/065C5E/white?text={$product->name}",
                    'alt_text' => $product->name,
                    'is_primary' => true,
                ]
            );

            LabReport::firstOrCreate(
                ['product_id' => $product->id, 'batch_number' => 'BSU-2024-001'],
                [
                    'test_date' => now()->subDays(rand(10, 60)),
                    'lab_name' => $labData['lab_name'],
                    'test_type' => $labData['test_type'],
                    'purity_percentage' => $labData['purity'],
                    'pdf_url' => '/lab-reports/sample.pdf',
                    'certificate_number' => 'CERT-' . strtoupper(Str::random(8)),
                    'is_active' => true,
                ]
            );
        }
    }
}
