<?php

namespace Database\Seeders;

use App\Models\User;
use App\Models\Category;
use App\Models\Product;
use App\Models\Area;
use App\Models\Table;
use App\Models\Permission;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\DB;

class DatabaseSeeder extends Seeder
{
    public function run(): void
    {
        $this->call(SettingSeeder::class);

        $modules = Permission::getModules();
        foreach ($modules as $slug => $name) {
            Permission::firstOrCreate(
                ['slug' => $slug . '.access'],
                [
                    'name' => 'Acceder a ' . $name,
                    'module' => $slug,
                    'description' => 'Permite acceder al módulo de ' . $name,
                ]
            );
        }

        $superAdmin = User::where('email', 'admin@admin.com')->first();
        if ($superAdmin) {
            $superAdmin->update([
                'role' => 'superadmin',
                'is_system_admin' => true,
            ]);
        } else {
            User::create([
                'name' => 'Administrador del Sistema',
                'email' => 'admin@admin.com',
                'password' => Hash::make('password'),
                'role' => 'superadmin',
                'is_system_admin' => true,
            ]);
        }

        $adminUser = User::where('email', 'negocio@admin.com')->first();
        if (!$adminUser) {
            User::create([
                'name' => 'Administrador del Negocio',
                'email' => 'negocio@admin.com',
                'password' => Hash::make('password'),
                'role' => 'admin',
                'is_system_admin' => false,
            ]);
        }

        $gerenteUser = User::where('email', 'gerente@restaurante.com')->first();
        if (!$gerenteUser) {
            User::create([
                'name' => 'Gerente General',
                'email' => 'gerente@restaurante.com',
                'password' => Hash::make('password'),
                'role' => 'gerente',
                'is_system_admin' => false,
            ]);
        }

        $cashierUser = User::where('email', 'cajero@restaurante.com')->first();
        if (!$cashierUser) {
            User::create([
                'name' => 'Cajero Demo',
                'email' => 'cajero@restaurante.com',
                'password' => Hash::make('password'),
                'role' => 'cashier',
                'is_system_admin' => false,
            ]);
        }

        $waiterUser = User::where('email', 'mozo@restaurante.com')->first();
        if (!$waiterUser) {
            User::create([
                'name' => 'Mozo Demo',
                'email' => 'mozo@restaurante.com',
                'password' => Hash::make('password'),
                'role' => 'waiter',
                'is_system_admin' => false,
            ]);
        }

        $kitchenUser = User::where('email', 'cocina@restaurante.com')->first();
        if (!$kitchenUser) {
            User::create([
                'name' => 'Cocina Demo',
                'email' => 'cocina@restaurante.com',
                'password' => Hash::make('password'),
                'role' => 'kitchen',
                'is_system_admin' => false,
            ]);
        }

        $roleDefaultPermissions = [
            'admin' => [
                'dashboard.access',
                'reports.access',
                'pos.access',
                'reservations.access',
                'sales.access',
                'kitchen.access',
                'clients.access',
                'categories.access',
                'products.access',
                'inventory_logs.access',
                'tables.access',
                'users.access',
                'settings.access',
                'expenses.access',
            ],
            'gerente' => [
                'dashboard.access',
                'reports.access',
                'pos.access',
                'reservations.access',
                'sales.access',
                'kitchen.access',
                'clients.access',
                'users.access',
                'expenses.access',
            ],
            'cashier' => [
                'dashboard.access',
                'pos.access',
                'reservations.access',
                'sales.access',
                'expenses.access',
            ],
            'waiter' => [
                'pos.access',
                'reservations.access',
            ],
            'kitchen' => [
                'kitchen.access',
            ],
        ];

        foreach ($roleDefaultPermissions as $role => $perms) {
            DB::table('role_permissions')->where('role', $role)->delete();
            foreach ($perms as $slug) {
                $perm = Permission::where('slug', $slug)->first();
                if ($perm) {
                    DB::table('role_permissions')->insert([
                        'role' => $role,
                        'permission_id' => $perm->id,
                        'created_at' => now(),
                        'updated_at' => now(),
                    ]);
                }
            }
        }

        $salon = Area::firstOrCreate(
            ['name' => 'Salón Principal'],
            []
        );
        $terraza = Area::firstOrCreate(
            ['name' => 'Terraza'],
            []
        );

        $mesasSalon = Table::where('area_id', $salon->id)->count();
        if ($mesasSalon < 5) {
            for ($i = $mesasSalon + 1; $i <= 5; $i++) {
                Table::create([
                    'area_id' => $salon->id,
                    'name' => 'Mesa ' . $i,
                    'seats' => 4,
                    'status' => 'available'
                ]);
            }
        }

        $mesasTerraza = Table::where('area_id', $terraza->id)->count();
        if ($mesasTerraza < 3) {
            for ($i = $mesasTerraza + 1; $i <= 3; $i++) {
                Table::create([
                    'area_id' => $terraza->id,
                    'name' => 'T-' . $i,
                    'seats' => 2,
                    'status' => 'available'
                ]);
            }
        }

        $catBebidas = Category::firstOrCreate(
            ['name' => 'Bebidas'],
            ['is_active' => true]
        );
        $catComidas = Category::firstOrCreate(
            ['name' => 'Hamburguesas'],
            ['is_active' => true]
        );
        $catPostres = Category::firstOrCreate(
            ['name' => 'Postres'],
            ['is_active' => true]
        );

        Product::firstOrCreate(
            ['name' => 'Hamburguesa Clásica'],
            [
                'category_id' => $catComidas->id,
                'price' => 12.50,
                'stock' => 50,
                'is_active' => true
            ]
        );
        Product::firstOrCreate(
            ['name' => 'Hamburguesa Doble Queso'],
            [
                'category_id' => $catComidas->id,
                'price' => 15.00,
                'stock' => 40,
                'is_active' => true
            ]
        );
        Product::firstOrCreate(
            ['name' => 'Coca Cola 500ml'],
            [
                'category_id' => $catBebidas->id,
                'price' => 3.50,
                'stock' => 100,
                'is_active' => true
            ]
        );
        Product::firstOrCreate(
            ['name' => 'Limonada Frozen'],
            [
                'category_id' => $catBebidas->id,
                'price' => 5.00,
                'stock' => null,
                'is_active' => true
            ]
        );
        Product::firstOrCreate(
            ['name' => 'Cheesecake Fresa'],
            [
                'category_id' => $catPostres->id,
                'price' => 8.00,
                'stock' => 10,
                'is_active' => true
            ]
        );
    }
}
