<?php

namespace App\Database\Seeds;

use CodeIgniter\Database\Seeder;

class KopmartSeeder extends Seeder
{
    public function run()
    {
        // Disable foreign key checks
        $this->db->disableForeignKeyChecks();

        // 1. Roles
        $roles = [
            ['id' => 1, 'role_name' => 'Super Admin'],
            ['id' => 2, 'role_name' => 'Admin'],
            ['id' => 3, 'role_name' => 'User'],
            ['id' => 4, 'role_name' => 'Customer'],
        ];
        $this->db->table('roles')->insertBatch($roles);

        // 2. Koperasi
        $koperasi = [
            [
                'id' => 1,
                'kode_koperasi' => 'KOP-PEGAWAI-A',
                'nama_koperasi' => 'Koperasi Pegawai Republik Indonesia (KPRI) Gorontalo',
                'alamat' => 'Jl. Jenderal Sudirman No. 12, Kota Gorontalo',
                'kabupaten' => 'Kota Gorontalo',
                'provinsi' => 'Gorontalo',
                'logo' => 'https://images.unsplash.com/photo-1543269865-cbf427effbad?w=150',
                'status' => 'aktif',
                'created_at' => date('Y-m-d H:i:s'),
            ],
            [
                'id' => 2,
                'kode_koperasi' => 'KOP-MERAH-PUTIH',
                'nama_koperasi' => 'Koperasi Merah Putih Sejahtera',
                'alamat' => 'Jl. Merdeka No. 45, Limboto, Kab. Gorontalo',
                'kabupaten' => 'Kabupaten Gorontalo',
                'provinsi' => 'Gorontalo',
                'logo' => 'https://images.unsplash.com/photo-1556742049-0cfed4f6a45d?w=150',
                'status' => 'aktif',
                'created_at' => date('Y-m-d H:i:s'),
            ],
            [
                'id' => 3,
                'kode_koperasi' => 'KOP-UMKM-MAJU',
                'nama_koperasi' => 'Koperasi UMKM Bina Mandiri',
                'alamat' => 'Jl. Trans Sulawesi, Boalemo',
                'kabupaten' => 'Kabupaten Boalemo',
                'provinsi' => 'Gorontalo',
                'logo' => 'https://images.unsplash.com/photo-1513519245088-0e12902e5a38?w=150',
                'status' => 'pending',
                'created_at' => date('Y-m-d H:i:s'),
            ],
            [
                'id' => 4,
                'kode_koperasi' => 'KOP-TANI-MAKMUR',
                'nama_koperasi' => 'Koperasi Tani Makmur Gorontalo',
                'alamat' => 'Kawasan Pertanian Raya, Kabila, Bone Bolango',
                'kabupaten' => 'Kabupaten Bone Bolango',
                'provinsi' => 'Gorontalo',
                'logo' => 'https://images.unsplash.com/photo-1500937386664-56d1dfef3854?w=150',
                'status' => 'aktif',
                'created_at' => date('Y-m-d H:i:s'),
            ],
        ];
        $this->db->table('koperasi')->insertBatch($koperasi);

        // 3. Users (Password: password123)
        $password = password_hash('password123', PASSWORD_BCRYPT);
        $users = [
            [
                'id' => 1,
                'koperasi_id' => null,
                'role_id' => 1,
                'nama' => 'Yusuf Fikri (Super Admin)',
                'email' => 'superadmin@kopdigi.id',
                'phone' => '081234567890',
                'password' => $password,
                'status' => 'active',
                'created_at' => date('Y-m-d H:i:s'),
            ],
            [
                'id' => 2,
                'koperasi_id' => null,
                'role_id' => 2,
                'nama' => 'Hendra Wijaya (Admin Dinas)',
                'email' => 'dinas@kopdigi.id',
                'phone' => '081234567891',
                'password' => $password,
                'status' => 'active',
                'created_at' => date('Y-m-d H:i:s'),
            ],
            [
                'id' => 3,
                'koperasi_id' => 1,
                'role_id' => 3,
                'nama' => 'Admin Koperasi Pegawai A',
                'email' => 'admin.kopa@kopdigi.id',
                'phone' => '081234567892',
                'password' => $password,
                'status' => 'active',
                'created_at' => date('Y-m-d H:i:s'),
            ],
            [
                'id' => 4,
                'koperasi_id' => 2,
                'role_id' => 3,
                'nama' => 'Admin Koperasi Merah Putih',
                'email' => 'admin.kopmp@kopdigi.id',
                'phone' => '081234567893',
                'password' => $password,
                'status' => 'active',
                'created_at' => date('Y-m-d H:i:s'),
            ],
            [
                'id' => 5,
                'koperasi_id' => 3,
                'role_id' => 3,
                'nama' => 'Admin Koperasi UMKM B',
                'email' => 'admin.kopumkm@kopdigi.id',
                'phone' => '081234567894',
                'password' => $password,
                'status' => 'active',
                'created_at' => date('Y-m-d H:i:s'),
            ],
            [
                'id' => 6,
                'koperasi_id' => 4,
                'role_id' => 3,
                'nama' => 'Admin Koperasi Tani C',
                'email' => 'admin.koptani@kopdigi.id',
                'phone' => '081234567895',
                'password' => $password,
                'status' => 'active',
                'created_at' => date('Y-m-d H:i:s'),
            ],
            [
                'id' => 7,
                'koperasi_id' => null,
                'role_id' => 4,
                'nama' => 'Budi Santoso (Customer)',
                'email' => 'budi@gmail.com',
                'phone' => '081234567896',
                'password' => $password,
                'status' => 'active',
                'created_at' => date('Y-m-d H:i:s'),
            ],
            [
                'id' => 8,
                'koperasi_id' => null,
                'role_id' => 4,
                'nama' => 'Siti Aminah (Customer)',
                'email' => 'siti@gmail.com',
                'phone' => '081234567897',
                'password' => $password,
                'status' => 'active',
                'created_at' => date('Y-m-d H:i:s'),
            ],
        ];
        $this->db->table('users')->insertBatch($users);

        // 4. Categories
        $categories = [
            ['id' => 1, 'category_name' => 'Sembako', 'icon' => 'shopping_cart'],
            ['id' => 2, 'category_name' => 'Elektronik', 'icon' => 'devices'],
            ['id' => 3, 'category_name' => 'UMKM', 'icon' => 'storefront'],
            ['id' => 4, 'category_name' => 'Pertanian', 'icon' => 'agriculture'],
        ];
        $this->db->table('categories')->insertBatch($categories);

        // 5. Products
        $products = [
            [
                'id' => 1,
                'koperasi_id' => 1,
                'kategori_id' => 1,
                'nama_produk' => 'Beras Pandan Wangi 5kg',
                'harga' => 78000.00,
                'stok' => 40,
                'deskripsi' => 'Beras Pandan Wangi kualitas premium asli Gorontalo. Butiran bersih, pulen, dan harum alami.',
                'foto' => 'https://images.unsplash.com/photo-1586201375761-83865001e31c?w=500',
                'rating' => 4.9,
                'tags' => 'beras, sembako, beras premium, cianjur',
                'status' => 'aktif',
                'created_at' => date('Y-m-d H:i:s'),
            ],
            [
                'id' => 2,
                'koperasi_id' => 1,
                'kategori_id' => 1,
                'nama_produk' => 'Minyak Goreng Kita 2L',
                'harga' => 34000.00,
                'stok' => 100,
                'deskripsi' => 'Minyak goreng kelapa sawit berkualitas, jernih, dan tidak mudah berbusa. Cocok untuk hidangan gorengan keluarga.',
                'foto' => 'https://images.unsplash.com/photo-1474979266404-7eaacbcd87c5?w=500',
                'rating' => 4.8,
                'tags' => 'minyak, minyak goreng, sembako, minyak kita',
                'status' => 'aktif',
                'created_at' => date('Y-m-d H:i:s'),
            ],
            [
                'id' => 3,
                'koperasi_id' => 2,
                'kategori_id' => 3,
                'nama_produk' => 'Kaos Merah Putih Digital Edisi 2026',
                'harga' => 95000.00,
                'stok' => 25,
                'deskripsi' => 'Kaos eksklusif Koperasi Merah Putih dengan bahan Cotton Combed 30s. Dingin, menyerap keringat, sablon plastisol premium.',
                'foto' => 'https://images.unsplash.com/photo-1521572267360-ee0c2909d518?w=500',
                'rating' => 5.0,
                'tags' => 'kaos, baju, umkm, merah putih, pakaian',
                'status' => 'aktif',
                'created_at' => date('Y-m-d H:i:s'),
            ],
            [
                'id' => 4,
                'koperasi_id' => 2,
                'kategori_id' => 3,
                'nama_produk' => 'Tas Anyaman Bambu Limboto',
                'harga' => 120000.00,
                'stok' => 15,
                'deskripsi' => 'Tas anyaman bambu premium kerajinan tangan lokal Gorontalo. Kuat, awet, dan bergaya khas nusantara.',
                'foto' => 'https://images.unsplash.com/photo-1544816155-12df9643f363?w=500',
                'rating' => 4.7,
                'tags' => 'tas, anyaman, kerajinan, tas bambu, umkm',
                'status' => 'aktif',
                'created_at' => date('Y-m-d H:i:s'),
            ],
            [
                'id' => 5,
                'koperasi_id' => 2,
                'kategori_id' => 1,
                'nama_produk' => 'Kopi Arabika Pinogu 250g',
                'harga' => 75000.00,
                'stok' => 35,
                'deskripsi' => 'Kopi Arabika organik khas daerah terpencil Pinogu, Bone Bolango, Gorontalo. Diolah secara higienis, rasa khas buah-buahan.',
                'foto' => 'https://images.unsplash.com/photo-1559056199-641a0ac8b55e?w=500',
                'rating' => 4.9,
                'tags' => 'kopi, pinogu, kopi lokal, gayo, arabika',
                'status' => 'aktif',
                'created_at' => date('Y-m-d H:i:s'),
            ],
            [
                'id' => 6,
                'koperasi_id' => 4,
                'kategori_id' => 4,
                'nama_produk' => 'Pupuk Organik Cair Makmur',
                'harga' => 45000.00,
                'stok' => 80,
                'deskripsi' => 'Pupuk organik cair formula khusus Koperasi Tani Makmur. Mempercepat pertumbuhan tanaman buah, bunga, dan sayur.',
                'foto' => 'https://images.unsplash.com/photo-1592417817098-8f3d6eb19675?w=500',
                'rating' => 4.8,
                'tags' => 'pupuk, organik, pupuk cair, tani, pertanian',
                'status' => 'aktif',
                'created_at' => date('Y-m-d H:i:s'),
            ],
            [
                'id' => 7,
                'koperasi_id' => 4,
                'kategori_id' => 4,
                'nama_produk' => 'Benih Padi Unggul Gorontalo 5kg',
                'harga' => 90000.00,
                'stok' => 50,
                'deskripsi' => 'Benih padi varietas unggulan tahan hama wereng, menghasilkan bulir padi melimpah. Bersertifikasi Dinas Pertanian.',
                'foto' => 'https://images.unsplash.com/photo-1530595467537-0b5996c41f2d?w=500',
                'rating' => 4.6,
                'tags' => 'benih, padi, beras, tani, pertanian',
                'status' => 'aktif',
                'created_at' => date('Y-m-d H:i:s'),
            ],
        ];
        $this->db->table('products')->insertBatch($products);

        // 6. Orders
        $orders = [
            [
                'id' => 1,
                'invoice' => 'INV-20260528-98F5B1',
                'customer_id' => 7, // Budi
                'koperasi_id' => 2, // Koperasi Merah Putih
                'total' => 185000.00,
                'status' => 'COMPLETED',
                'ongkir' => 15000.00,
                'kurir' => 'KopExpress',
                'alamat' => 'Jl. Merdeka No. 45, Limboto, Gorontalo',
                'created_at' => '2026-05-28 14:22:00',
            ],
            [
                'id' => 2,
                'invoice' => 'INV-20260529-00A2C9',
                'customer_id' => 8, // Siti
                'koperasi_id' => 1, // Koperasi Pegawai A
                'total' => 127000.00,
                'status' => 'PAYMENT_VERIFIED',
                'ongkir' => 15000.00,
                'kurir' => 'POS Indonesia',
                'alamat' => 'Jl. Nani Wartabone No. 100, Gorontalo',
                'created_at' => '2026-05-29 08:30:00',
            ],
        ];
        $this->db->table('orders')->insertBatch($orders);

        // 7. Order Items
        $order_items = [
            [
                'order_id' => 1,
                'product_id' => 3, // Kaos Merah Putih
                'qty' => 1,
                'harga' => 95000.00,
                'subtotal' => 95000.00,
            ],
            [
                'order_id' => 1,
                'product_id' => 5, // Kopi Arabika Pinogu
                'qty' => 1,
                'harga' => 75000.00,
                'subtotal' => 75000.00,
            ],
            [
                'order_id' => 2,
                'product_id' => 1, // Beras Pandan Wangi
                'qty' => 1,
                'harga' => 78000.00,
                'subtotal' => 78000.00,
            ],
            [
                'order_id' => 2,
                'product_id' => 2, // Minyak Goreng Kita
                'qty' => 1,
                'harga' => 34000.00,
                'subtotal' => 34000.00,
            ],
        ];
        $this->db->table('order_items')->insertBatch($order_items);

        // 8. Audit Logs
        $audit_logs = [
            [
                'user_id' => 1,
                'action' => 'Inisialisasi Sistem',
                'notes' => 'Melakukan seeding data awal sistem Koperasi Digital Merah Putih.',
                'created_at' => date('Y-m-d H:i:s'),
            ],
            [
                'user_id' => 2,
                'action' => 'Verifikasi Koperasi',
                'notes' => 'Admin Dinas memverifikasi Koperasi Pegawai Republik Indonesia (KPRI) Gorontalo.',
                'created_at' => date('Y-m-d H:i:s'),
            ],
        ];
        $this->db->table('audit_logs')->insertBatch($audit_logs);

        // Re-enable foreign key checks
        $this->db->enableForeignKeyChecks();
    }
}
