<?php

namespace Database\Seeders;

use Illuminate\Database\Seeder;
use App\Models\Branch;
use App\Models\Category;
use App\Models\Product;
use App\Models\User;
use Illuminate\Support\Facades\Hash;

class QuickDataSeeder extends Seeder
{
    public function run(): void
    {
        $this->command->info('Adding sample data...');

        // Create branches if they don't exist
        if (Branch::count() === 0) {
            Branch::create([
                'name' => 'Main Branch',
                'code' => 'MB001',
                'address' => '123 Main Street',
                'phone' => '555-0100',
                'email' => 'main@creamyutopia.com',
                'is_active' => true,
            ]);
            $this->command->info('✓ Created 1 branch');
        }

        // Create categories if they don't exist
        if (Category::count() === 0) {
            Category::create([
                'name' => 'Ice Cream',
                'slug' => 'ice-cream',
                'description' => 'Delicious ice cream flavors',
                'is_active' => true,
            ]);
            $this->command->info('✓ Created 1 category');
        }

        // Create products if they don't exist
        $category = Category::first();
        if (Product::count() === 0 && $category) {
            $products = [
                ['name' => 'Vanilla', 'slug' => 'vanilla', 'base_price' => 50, 'max_scoops' => 3],
                ['name' => 'Chocolate', 'slug' => 'chocolate', 'base_price' => 55, 'max_scoops' => 3],
                ['name' => 'Strawberry', 'slug' => 'strawberry', 'base_price' => 60, 'max_scoops' => 3],
                ['name' => 'Mango', 'slug' => 'mango', 'base_price' => 50, 'max_scoops' => 3],
                ['name' => 'Mint Chip', 'slug' => 'mint-chip', 'base_price' => 65, 'max_scoops' => 3],
            ];

            foreach ($products as $product) {
                Product::create([
                    'category_id' => $category->id,
                    'name' => $product['name'],
                    'slug' => $product['slug'],
                    'description' => 'Delicious ' . $product['name'] . ' ice cream',
                    'base_price' => $product['base_price'],
                    'max_scoops' => $product['max_scoops'],
                    'is_active' => true,
                ]);
            }
            $this->command->info('✓ Created ' . count($products) . ' products');
        }

        // Create additional user
        $branch = Branch::first();
        if ($branch && !User::where('email', 'cashier@test.com')->exists()) {
            $user = User::create([
                'name' => 'POS Cashier',
                'email' => 'cashier@test.com',
                'password' => Hash::make('password'),
                'branch_id' => $branch->id,
            ]);
            $user->assignRole('pos-operator');
            $this->command->info('✓ Created cashier user (email: cashier@test.com, password: password)');
        }

        // Assign all products to all branches
        $branches = Branch::all();
        $products = Product::all();

        if ($branches->count() > 0 && $products->count() > 0) {
            foreach ($branches as $branch) {
                foreach ($products as $product) {
                    // Check if already assigned
                    $exists = \DB::table('branch_products')
                        ->where('branch_id', $branch->id)
                        ->where('product_id', $product->id)
                        ->exists();

                    if (!$exists) {
                        \DB::table('branch_products')->insert([
                            'branch_id' => $branch->id,
                            'product_id' => $product->id,
                            'is_available' => true,
                            'custom_price' => null,
                            'created_at' => now(),
                            'updated_at' => now(),
                        ]);
                    }
                }
            }
            $this->command->info('✓ Assigned ' . $products->count() . ' products to ' . $branches->count() . ' branches');
        }

        $this->command->info('✅ Sample data added successfully!');
        $this->command->info('💡 Login credentials:');
        $this->command->info('   Admin: admin@creamy-utopia.com / 123456');
        $this->command->info('   Cashier: cashier@test.com / password');
    }
}
