<?php

namespace Database\Seeders;

use App\Models\{User, Category, Product, Setting};
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\Hash;

class DatabaseSeeder extends Seeder
{
    public function run(): void
    {
        // ======== USERS ========
        User::create([
            'name'      => 'Administrator',
            'username'  => 'admin',
            'email'     => 'admin@pos.local',
            'password'  => Hash::make('admin123'),
            'role'      => 'admin',
            'is_active' => true,
        ]);

        User::create([
            'name'      => 'Kasir 1',
            'username'  => 'kasir1',
            'email'     => 'kasir1@pos.local',
            'password'  => Hash::make('kasir123'),
            'role'      => 'kasir',
            'pin'       => '1234',
            'is_active' => true,
        ]);

        User::create([
            'name'      => 'Kasir 2',
            'username'  => 'kasir2',
            'email'     => 'kasir2@pos.local',
            'password'  => Hash::make('kasir123'),
            'role'      => 'kasir',
            'pin'       => '5678',
            'is_active' => true,
        ]);

        User::create([
            'name'      => 'Manajer',
            'username'  => 'manajer',
            'email'     => 'manajer@pos.local',
            'password'  => Hash::make('manajer123'),
            'role'      => 'manajer',
            'is_active' => true,
        ]);

        // ======== CATEGORIES ========
        $categories = [
            ['name' => 'Makanan',    'color' => '#f59e0b', 'icon' => '🍔', 'sort_order' => 1],
            ['name' => 'Minuman',    'color' => '#3b82f6', 'icon' => '🥤', 'sort_order' => 2],
            ['name' => 'Snack',      'color' => '#10b981', 'icon' => '🍿', 'sort_order' => 3],
            ['name' => 'Rokok',      'color' => '#6b7280', 'icon' => '🚬', 'sort_order' => 4],
            ['name' => 'Sembako',    'color' => '#8b5cf6', 'icon' => '🛒', 'sort_order' => 5],
            ['name' => 'Kebersihan', 'color' => '#ec4899', 'icon' => '🧴', 'sort_order' => 6],
            ['name' => 'Lainnya',    'color' => '#64748b', 'icon' => '📦', 'sort_order' => 7],
        ];

        $catIds = [];
        foreach ($categories as $cat) {
            $c = Category::create(array_merge($cat, ['slug' => \Str::slug($cat['name'])]));
            $catIds[$cat['name']] = $c->id;
        }

        // ======== PRODUCTS ========
        $products = [
            // Makanan
            ['name' => 'Nasi Goreng',    'sku' => 'MKN-001', 'category' => 'Makanan',    'cost' => 8000,  'price' => 15000, 'stock' => 100, 'unit' => 'porsi'],
            ['name' => 'Mie Goreng',      'sku' => 'MKN-002', 'category' => 'Makanan',    'cost' => 7000,  'price' => 13000, 'stock' => 100, 'unit' => 'porsi'],
            ['name' => 'Roti Bakar',      'sku' => 'MKN-003', 'category' => 'Makanan',    'cost' => 4000,  'price' => 8000,  'stock' => 50,  'unit' => 'pcs'],
            // Minuman
            ['name' => 'Aqua 600ml',      'sku' => 'MNM-001', 'category' => 'Minuman',    'cost' => 2500,  'price' => 4000,  'stock' => 200, 'unit' => 'botol'],
            ['name' => 'Es Teh Manis',    'sku' => 'MNM-002', 'category' => 'Minuman',    'cost' => 2000,  'price' => 5000,  'stock' => 999, 'unit' => 'gelas'],
            ['name' => 'Kopi Hitam',      'sku' => 'MNM-003', 'category' => 'Minuman',    'cost' => 3000,  'price' => 6000,  'stock' => 999, 'unit' => 'gelas'],
            ['name' => 'Teh Botol Sosro', 'sku' => 'MNM-004', 'category' => 'Minuman',    'cost' => 4500,  'price' => 7000,  'stock' => 60,  'unit' => 'botol'],
            // Snack
            ['name' => 'Chitato 68gr',    'sku' => 'SNK-001', 'category' => 'Snack',      'cost' => 8000,  'price' => 10000, 'stock' => 80,  'unit' => 'pcs'],
            ['name' => 'Oreo',            'sku' => 'SNK-002', 'category' => 'Snack',      'cost' => 9500,  'price' => 12000, 'stock' => 60,  'unit' => 'pcs'],
            // Rokok
            ['name' => 'Sampoerna Mild',  'sku' => 'RKK-001', 'category' => 'Rokok',      'cost' => 21000, 'price' => 23000, 'stock' => 100, 'unit' => 'bungkus'],
            ['name' => 'Gudang Garam',    'sku' => 'RKK-002', 'category' => 'Rokok',      'cost' => 20000, 'price' => 22000, 'stock' => 80,  'unit' => 'bungkus'],
            // Sembako
            ['name' => 'Beras 5kg',       'sku' => 'SMB-001', 'category' => 'Sembako',    'cost' => 65000, 'price' => 75000, 'stock' => 30,  'unit' => 'sak'],
            ['name' => 'Minyak Goreng 1L','sku' => 'SMB-002', 'category' => 'Sembako',    'cost' => 15000, 'price' => 18000, 'stock' => 50,  'unit' => 'botol'],
            ['name' => 'Gula Pasir 1kg',  'sku' => 'SMB-003', 'category' => 'Sembako',    'cost' => 14000, 'price' => 16000, 'stock' => 40,  'unit' => 'kg'],
            // Kebersihan
            ['name' => 'Sabun Lifebuoy',  'sku' => 'KBR-001', 'category' => 'Kebersihan', 'cost' => 3500,  'price' => 5000,  'stock' => 60,  'unit' => 'pcs'],
            ['name' => 'Shampoo Sachet',  'sku' => 'KBR-002', 'category' => 'Kebersihan', 'cost' => 1000,  'price' => 1500,  'stock' => 200, 'unit' => 'sachet'],
        ];

        foreach ($products as $p) {
            Product::create([
                'name'          => $p['name'],
                'sku'           => $p['sku'],
                'category_id'   => $catIds[$p['category']],
                'cost_price'    => $p['cost'],
                'selling_price' => $p['price'],
                'stock'         => $p['stock'],
                'unit'          => $p['unit'],
                'min_stock'     => 5,
                'track_stock'   => true,
                'is_active'     => true,
            ]);
        }

        // ======== SETTINGS ========
        $settings = [
            ['key' => 'store_name',     'value' => 'Toko Maju Jaya',       'type' => 'string',  'group' => 'store',   'label' => 'Nama Toko'],
            ['key' => 'store_address',  'value' => 'Jl. Contoh No. 1',     'type' => 'string',  'group' => 'store',   'label' => 'Alamat'],
            ['key' => 'store_phone',    'value' => '08123456789',           'type' => 'string',  'group' => 'store',   'label' => 'No. Telepon'],
            ['key' => 'store_email',    'value' => 'toko@example.com',      'type' => 'string',  'group' => 'store',   'label' => 'Email'],
            ['key' => 'tax_enabled',    'value' => '0',                     'type' => 'boolean', 'group' => 'finance', 'label' => 'Aktifkan PPN'],
            ['key' => 'tax_percent',    'value' => '11',                    'type' => 'integer', 'group' => 'finance', 'label' => 'Persen PPN'],
            ['key' => 'receipt_footer', 'value' => 'Terima kasih sudah berbelanja!', 'type' => 'string', 'group' => 'receipt', 'label' => 'Footer Struk'],
            ['key' => 'receipt_width',  'value' => '48',                    'type' => 'integer', 'group' => 'receipt', 'label' => 'Lebar Struk (karakter)'],
        ];

        foreach ($settings as $s) {
            Setting::create($s);
        }
    }
}
