<?php

namespace App\Database\Seeds;

use CodeIgniter\Database\Seeder;
use App\Models\CategoryModel;
use App\Models\ProductModel;

class ProductSeeder extends Seeder
{
    public function run()
    {
        // Truncate tables first
        $this->db->query('SET FOREIGN_KEY_CHECKS=0;');
        $this->db->table('products')->truncate();
        $this->db->table('categories')->truncate();
        $this->db->query('SET FOREIGN_KEY_CHECKS=1;');

        $categoryModel = new CategoryModel();
        $productModel = new ProductModel();

        // 1. Insert Categories
        $categories = [
            ['name' => 'Oleoresin', 'slug' => 'oleoresin', 'status' => 1],
            ['name' => 'Essential Oil', 'slug' => 'essential-oil', 'status' => 1],
            ['name' => 'Herbal extract', 'slug' => 'herbal-extract', 'status' => 1],
        ];

        $categoryIds = [];
        foreach ($categories as $cat) {
            $categoryModel->insert($cat);
            $categoryIds[$cat['name']] = $categoryModel->getInsertID();
        }

        // 2. Insert Products
        $products = [];

        $oleoresins = [
            'Cardamom Oleoresin', 'Nutmeg Oleoresin', 'Ginger Oleoresin', 'Turmeric Oleoresin',
            'Clove Oleoresin', 'Black pepper Oleoresin', 'Celery Oleoresin', 'Paparika Oleoresin',
            'Capsicum Oleoresin', 'Fenugreek Oleoresin', 'Bromelian'
        ];

        foreach ($oleoresins as $name) {
            $products[] = [
                'category_id' => $categoryIds['Oleoresin'],
                'name' => $name,
                'slug' => strtolower(str_replace(' ', '-', $name)),
                'short_description' => 'High quality ' . $name . ' extracted for maximum purity.',
                'status' => 1,
                'is_featured' => 0
            ];
        }

        $essentialOils = [
            'Nutmeg oil', 'Ginger oil', 'Turmeric oil', 'Clove bud oil', 'Cardamom oil'
        ];

        foreach ($essentialOils as $name) {
            $products[] = [
                'category_id' => $categoryIds['Essential Oil'],
                'name' => $name,
                'slug' => strtolower(str_replace(' ', '-', $name)),
                'short_description' => 'Pure ' . $name . ' with intense aroma.',
                'status' => 1,
                'is_featured' => 0
            ];
        }

        $herbalExtracts = [
            'Curcumin 95'
        ];

        foreach ($herbalExtracts as $name) {
            $products[] = [
                'category_id' => $categoryIds['Herbal extract'],
                'name' => $name,
                'slug' => strtolower(str_replace(' ', '-', $name)),
                'short_description' => 'Premium ' . $name . ' herbal extract.',
                'status' => 1,
                'is_featured' => 0
            ];
        }

        foreach ($products as $prod) {
            $productModel->insert($prod);
        }
    }
}

