<?php

namespace Database\Seeders;

use Illuminate\Database\Seeder;
use App\Models\Restaurant;
use App\Models\Branch;
use App\Models\User;
use App\Models\Category;
use App\Models\MenuItem;
use App\Models\Table;
use App\Models\InventoryItem;
use App\Models\Customer;
use Illuminate\Support\Facades\Hash;

class RestaurantSeeder extends Seeder
{
    public function run(): void
    {
        // Create Restaurant
        $restaurant = Restaurant::create([
            'name' => 'Mama Njeri\'s Kitchen',
            'slug' => 'mama-njeris-kitchen',
            'address' => '123 Kenyatta Avenue, Nairobi',
            'phone' => '+254712345678',
            'email' => 'info@mamanjeri.co.ke',
            'vat_rate' => 16.00,
            'currency' => 'KES',
            'settings' => [
                'timezone' => 'Africa/Nairobi',
                'receipt_footer' => 'Thank you for dining with us!',
                'auto_print_kitchen' => true,
            ],
        ]);

        // Create Branch
        $branch = Branch::create([
            'restaurant_id' => $restaurant->id,
            'name' => 'Main Branch',
            'code' => 'MAIN001',
            'address' => '123 Kenyatta Avenue, Nairobi',
            'phone' => '+254712345678',
            'latitude' => -1.2921,
            'longitude' => 36.8219,
        ]);

        // Create Admin User
        $admin = User::create([
            'restaurant_id' => $restaurant->id,
            'branch_id' => $branch->id,
            'name' => 'Admin User',
            'email' => 'admin@mamanjeri.co.ke',
            'password' => Hash::make('password'),
            'phone' => '+254712345678',
            'role' => 'admin',
            'permissions' => ['*'],
        ]);

        // Create Staff Users
        User::create([
            'restaurant_id' => $restaurant->id,
            'branch_id' => $branch->id,
            'name' => 'John Cashier',
            'email' => 'cashier@mamanjeri.co.ke',
            'password' => Hash::make('password'),
            'phone' => '+254712345679',
            'role' => 'cashier',
        ]);

        User::create([
            'restaurant_id' => $restaurant->id,
            'branch_id' => $branch->id,
            'name' => 'Mary Kitchen',
            'email' => 'kitchen@mamanjeri.co.ke',
            'password' => Hash::make('password'),
            'phone' => '+254712345680',
            'role' => 'kitchen',
        ]);

        // Create Tables
        $sections = ['dining', 'bar', 'outdoor'];
        for ($i = 1; $i <= 15; $i++) {
            Table::create([
                'branch_id' => $branch->id,
                'name' => 'Table ' . $i,
                'section' => $sections[($i - 1) % 3],
                'capacity' => rand(2, 8),
                'status' => 'available',
            ]);
        }

        // Create Categories
        $categories = [
            ['name' => 'Main Dishes', 'slug' => 'main-dishes', 'color' => '#EF4444'],
            ['name' => 'Appetizers', 'slug' => 'appetizers', 'color' => '#F59E0B'],
            ['name' => 'Beverages', 'slug' => 'beverages', 'color' => '#3B82F6'],
            ['name' => 'Desserts', 'slug' => 'desserts', 'color' => '#8B5CF6'],
            ['name' => 'Traditional', 'slug' => 'traditional', 'color' => '#10B981'],
        ];

        foreach ($categories as $categoryData) {
            Category::create([
                'restaurant_id' => $restaurant->id,
                'name' => $categoryData['name'],
                'slug' => $categoryData['slug'],
                'color' => $categoryData['color'],
                'sort_order' => array_search($categoryData, $categories),
            ]);
        }

        // Create Menu Items
        $menuItems = [
            // Main Dishes
            ['name' => 'Nyama Choma', 'category' => 'main-dishes', 'price' => 800, 'prep_time' => 25, 'stations' => ['grill']],
            ['name' => 'Chicken Curry', 'category' => 'main-dishes', 'price' => 650, 'prep_time' => 20, 'stations' => ['main']],
            ['name' => 'Fish Fillet', 'category' => 'main-dishes', 'price' => 750, 'prep_time' => 15, 'stations' => ['main']],
            ['name' => 'Beef Stew', 'category' => 'main-dishes', 'price' => 600, 'prep_time' => 30, 'stations' => ['main']],
            
            // Traditional
            ['name' => 'Ugali', 'category' => 'traditional', 'price' => 100, 'prep_time' => 10, 'stations' => ['main']],
            ['name' => 'Sukuma Wiki', 'category' => 'traditional', 'price' => 150, 'prep_time' => 15, 'stations' => ['main']],
            ['name' => 'Githeri', 'category' => 'traditional', 'price' => 200, 'prep_time' => 20, 'stations' => ['main']],
            ['name' => 'Mukimo', 'category' => 'traditional', 'price' => 180, 'prep_time' => 25, 'stations' => ['main']],
            
            // Appetizers
            ['name' => 'Samosas', 'category' => 'appetizers', 'price' => 50, 'prep_time' => 5, 'stations' => ['main']],
            ['name' => 'Spring Rolls', 'category' => 'appetizers', 'price' => 80, 'prep_time' => 8, 'stations' => ['main']],
            ['name' => 'Chicken Wings', 'category' => 'appetizers', 'price' => 300, 'prep_time' => 12, 'stations' => ['grill']],
            
            // Beverages
            ['name' => 'Tusker Beer', 'category' => 'beverages', 'price' => 250, 'prep_time' => 1, 'stations' => ['bar']],
            ['name' => 'Coca Cola', 'category' => 'beverages', 'price' => 120, 'prep_time' => 1, 'stations' => ['bar']],
            ['name' => 'Fresh Juice', 'category' => 'beverages', 'price' => 200, 'prep_time' => 3, 'stations' => ['bar']],
            ['name' => 'Kenyan Tea', 'category' => 'beverages', 'price' => 80, 'prep_time' => 5, 'stations' => ['bar']],
            
            // Desserts
            ['name' => 'Ice Cream', 'category' => 'desserts', 'price' => 150, 'prep_time' => 2, 'stations' => ['bar']],
            ['name' => 'Chocolate Cake', 'category' => 'desserts', 'price' => 300, 'prep_time' => 5, 'stations' => ['main']],
        ];

        foreach ($menuItems as $itemData) {
            $category = Category::where('slug', $itemData['category'])->first();
            
            MenuItem::create([
                'restaurant_id' => $restaurant->id,
                'category_id' => $category->id,
                'name' => $itemData['name'],
                'slug' => \Str::slug($itemData['name']),
                'price' => $itemData['price'],
                'prep_time' => $itemData['prep_time'],
                'kitchen_stations' => $itemData['stations'],
                'track_inventory' => false,
            ]);
        }

        // Create Inventory Items
        $inventoryItems = [
            ['name' => 'Beef', 'sku' => 'BEEF001', 'category' => 'ingredient', 'unit' => 'kg', 'stock' => 50, 'min' => 10, 'cost' => 800],
            ['name' => 'Chicken', 'sku' => 'CHKN001', 'category' => 'ingredient', 'unit' => 'kg', 'stock' => 30, 'min' => 8, 'cost' => 600],
            ['name' => 'Fish', 'sku' => 'FISH001', 'category' => 'ingredient', 'unit' => 'kg', 'stock' => 20, 'min' => 5, 'cost' => 900],
            ['name' => 'Rice', 'sku' => 'RICE001', 'category' => 'ingredient', 'unit' => 'kg', 'stock' => 100, 'min' => 20, 'cost' => 120],
            ['name' => 'Maize Flour', 'sku' => 'MAIZE001', 'category' => 'ingredient', 'unit' => 'kg', 'stock' => 80, 'min' => 15, 'cost' => 100],
            ['name' => 'Cooking Oil', 'sku' => 'OIL001', 'category' => 'ingredient', 'unit' => 'liters', 'stock' => 25, 'min' => 5, 'cost' => 300],
            ['name' => 'Onions', 'sku' => 'ONION001', 'category' => 'ingredient', 'unit' => 'kg', 'stock' => 15, 'min' => 3, 'cost' => 80],
            ['name' => 'Tomatoes', 'sku' => 'TOMATO001', 'category' => 'ingredient', 'unit' => 'kg', 'stock' => 12, 'min' => 3, 'cost' => 100],
            ['name' => 'Coca Cola', 'sku' => 'COKE001', 'category' => 'beverage', 'unit' => 'bottles', 'stock' => 200, 'min' => 50, 'cost' => 60],
            ['name' => 'Tusker Beer', 'sku' => 'BEER001', 'category' => 'beverage', 'unit' => 'bottles', 'stock' => 150, 'min' => 30, 'cost' => 150],
            ['name' => 'Takeaway Containers', 'sku' => 'CONT001', 'category' => 'packaging', 'unit' => 'pieces', 'stock' => 500, 'min' => 100, 'cost' => 15],
            ['name' => 'Napkins', 'sku' => 'NAP001', 'category' => 'packaging', 'unit' => 'packs', 'stock' => 50, 'min' => 10, 'cost' => 80],
        ];

        foreach ($inventoryItems as $itemData) {
            InventoryItem::create([
                'restaurant_id' => $restaurant->id,
                'branch_id' => $branch->id,
                'name' => $itemData['name'],
                'sku' => $itemData['sku'],
                'category' => $itemData['category'],
                'unit' => $itemData['unit'],
                'current_stock' => $itemData['stock'],
                'minimum_stock' => $itemData['min'],
                'cost_per_unit' => $itemData['cost'],
                'supplier' => 'Local Supplier',
            ]);
        }

        // Create Sample Customers
        $customers = [
            ['name' => 'James Mwangi', 'phone' => '+254701234567', 'email' => 'james@example.com'],
            ['name' => 'Grace Wanjiku', 'phone' => '+254702345678', 'email' => 'grace@example.com'],
            ['name' => 'Peter Kiprotich', 'phone' => '+254703456789', 'email' => 'peter@example.com'],
            ['name' => 'Mary Akinyi', 'phone' => '+254704567890', 'email' => 'mary@example.com'],
            ['name' => 'David Mutua', 'phone' => '+254705678901', 'email' => 'david@example.com'],
        ];

        foreach ($customers as $customerData) {
            Customer::create([
                'restaurant_id' => $restaurant->id,
                'name' => $customerData['name'],
                'phone' => $customerData['phone'],
                'email' => $customerData['email'],
                'total_orders' => rand(1, 20),
                'total_spent' => rand(1000, 50000),
                'last_order_at' => now()->subDays(rand(1, 30)),
            ]);
        }
    }
}