<?php

namespace Database\Seeders;

use App\Models\User;
use App\Models\Category;
use App\Models\Product;
use App\Models\Customer;
use App\Models\Supplier;
use App\Models\ExpenseCategory;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\Hash;

class DatabaseSeeder extends Seeder
{
    public function run()
    {
        // Create admin user only if it doesn't exist
        if (!User::where('email', 'admin@pos.com')->exists()) {
            User::create([
                'name' => 'Admin User',
                'email' => 'admin@pos.com',
                'password' => Hash::make('password'),
            ]);
            $this->command->info('Admin user created successfully!');
        } else {
            $this->command->info('Admin user already exists, skipping...');
        }
        
        // Create categories only if they don't exist
        $categories = [
            ['name' => 'Electronics', 'slug' => 'electronics', 'description' => 'Electronic products', 'is_active' => true],
            ['name' => 'Clothing', 'slug' => 'clothing', 'description' => 'Clothing and apparel', 'is_active' => true],
            ['name' => 'Food', 'slug' => 'food', 'description' => 'Food items', 'is_active' => true],
            ['name' => 'Beverages', 'slug' => 'beverages', 'description' => 'Drinks and beverages', 'is_active' => true],
        ];
        
        foreach ($categories as $categoryData) {
            if (!Category::where('slug', $categoryData['slug'])->exists()) {
                Category::create($categoryData);
            }
        }
        $this->command->info('Categories seeded successfully!');
        
        // Create products only if they don't exist
        $products = [
            [
                'name' => 'Smartphone', 
                'sku' => 'PH001', 
                'barcode' => '1234567890123',
                'category_id' => 1, 
                'purchase_price' => 300, 
                'selling_price' => 499, 
                'stock' => 50,
                'min_stock' => 10,
                'is_active' => true
            ],
            [
                'name' => 'Laptop', 
                'sku' => 'LP001', 
                'barcode' => '1234567890124',
                'category_id' => 1, 
                'purchase_price' => 800, 
                'selling_price' => 1299, 
                'stock' => 30,
                'min_stock' => 5,
                'is_active' => true
            ],
            [
                'name' => 'T-Shirt', 
                'sku' => 'CL001', 
                'barcode' => '1234567890125',
                'category_id' => 2, 
                'purchase_price' => 10, 
                'selling_price' => 25, 
                'stock' => 100,
                'min_stock' => 20,
                'is_active' => true
            ],
            [
                'name' => 'Coffee', 
                'sku' => 'FD001', 
                'barcode' => '1234567890126',
                'category_id' => 3, 
                'purchase_price' => 5, 
                'selling_price' => 12, 
                'stock' => 200,
                'min_stock' => 50,
                'is_active' => true
            ],
            [
                'name' => 'Jeans', 
                'sku' => 'CL002', 
                'barcode' => '1234567890127',
                'category_id' => 2, 
                'purchase_price' => 25, 
                'selling_price' => 59, 
                'stock' => 75,
                'min_stock' => 15,
                'is_active' => true
            ],
            [
                'name' => 'Headphones', 
                'sku' => 'PH002', 
                'barcode' => '1234567890128',
                'category_id' => 1, 
                'purchase_price' => 20, 
                'selling_price' => 49, 
                'stock' => 150,
                'min_stock' => 30,
                'is_active' => true
            ],
        ];
        
        foreach ($products as $productData) {
            if (!Product::where('sku', $productData['sku'])->exists()) {
                Product::create($productData);
            }
        }
        $this->command->info('Products seeded successfully!');
        
        // Create customers only if they don't exist
        $customers = [
            [
                'name' => 'John Doe',
                'email' => 'john@example.com',
                'phone' => '1234567890',
                'address' => '123 Main St, New York, NY 10001',
                'points' => 0,
                'total_purchases' => 0
            ],
            [
                'name' => 'Jane Smith',
                'email' => 'jane@example.com',
                'phone' => '0987654321',
                'address' => '456 Oak Ave, Los Angeles, CA 90001',
                'points' => 0,
                'total_purchases' => 0
            ],
            [
                'name' => 'Bob Johnson',
                'email' => 'bob@example.com',
                'phone' => '5555555555',
                'address' => '789 Pine Rd, Chicago, IL 60601',
                'points' => 0,
                'total_purchases' => 0
            ],
        ];
        
        foreach ($customers as $customerData) {
            if (!Customer::where('email', $customerData['email'])->exists()) {
                Customer::create($customerData);
            }
        }
        $this->command->info('Customers seeded successfully!');
        
        // Create suppliers only if they don't exist
        $suppliers = [
            [
                'name' => 'Tech Distributors Inc.',
                'email' => 'sales@techdist.com',
                'phone' => '1112223333',
                'address' => '100 Tech Park, Silicon Valley, CA 94043',
                'company' => 'Tech Distributors'
            ],
            [
                'name' => 'Fashion Wholesale Co.',
                'email' => 'info@fashionwholesale.com',
                'phone' => '4445556666',
                'address' => '200 Fashion Ave, New York, NY 10018',
                'company' => 'Fashion Wholesale'
            ],
            [
                'name' => 'Food Supply Ltd.',
                'email' => 'orders@foodsupply.com',
                'phone' => '7778889999',
                'address' => '300 Food Street, Chicago, IL 60601',
                'company' => 'Food Supply'
            ],
        ];
        
        foreach ($suppliers as $supplierData) {
            if (!Supplier::where('email', $supplierData['email'])->exists()) {
                Supplier::create($supplierData);
            }
        }
        $this->command->info('Suppliers seeded successfully!');
        
        // Create expense categories
        $expenseCategories = [
            ['name' => 'Rent', 'slug' => 'rent', 'description' => 'Monthly rent payments'],
            ['name' => 'Utilities', 'slug' => 'utilities', 'description' => 'Electricity, water, internet bills'],
            ['name' => 'Salaries', 'slug' => 'salaries', 'description' => 'Employee salaries'],
            ['name' => 'Marketing', 'slug' => 'marketing', 'description' => 'Advertising and marketing expenses'],
            ['name' => 'Maintenance', 'slug' => 'maintenance', 'description' => 'Equipment and building maintenance'],
            ['name' => 'Transportation', 'slug' => 'transportation', 'description' => 'Delivery and transport costs'],
            ['name' => 'Office Supplies', 'slug' => 'office-supplies', 'description' => 'Stationery and office supplies'],
            ['name' => 'Other', 'slug' => 'other', 'description' => 'Other miscellaneous expenses'],
        ];
        
        foreach ($expenseCategories as $categoryData) {
            if (!ExpenseCategory::where('slug', $categoryData['slug'])->exists()) {
                ExpenseCategory::create($categoryData);
            }
        }
        $this->command->info('Expense categories seeded successfully!');
        
        $this->command->info('====================================');
        $this->command->info('Database seeding completed successfully!');
        $this->command->info('====================================');
        $this->command->info('Admin Login: admin@pos.com');
        $this->command->info('Admin Password: password');
        $this->command->info('====================================');
    }
}