<?php

namespace Database\Seeders;

use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Hash;
use Carbon\Carbon;

class UsersSeeder extends Seeder
{
    public function run()
    {
        $now = Carbon::now();

        // Create Super Admin
        $superAdminId = DB::table('users')->insertGetId([
            'name' => 'Super Admin',
            'email' => 'superadmin@juris.com',
            'password' => Hash::make('password'),
            'email_verified_at' => $now,
            'created_at' => $now,
            'updated_at' => $now
        ]);

        // Create Admin User
        $adminId = DB::table('users')->insertGetId([
            'name' => 'Admin User',
            'email' => 'admin@juris.com',
            'password' => Hash::make('password'),
            'email_verified_at' => $now,
            'created_at' => $now,
            'updated_at' => $now
        ]);

        // Create General Users
        $user1Id = DB::table('users')->insertGetId([
            'name' => 'John Doe',
            'email' => 'user1@juris.com',
            'password' => Hash::make('password'),
            'email_verified_at' => $now,
            'created_at' => $now,
            'updated_at' => $now
        ]);

        $user2Id = DB::table('users')->insertGetId([
            'name' => 'Jane Smith',
            'email' => 'user2@juris.com',
            'password' => Hash::make('password'),
            'email_verified_at' => $now,
            'created_at' => $now,
            'updated_at' => $now
        ]);

        $this->command->info('✅ Users seeded successfully!');
        $this->command->info("   Super Admin: superadmin@juris.com / password");
        $this->command->info("   Admin: admin@juris.com / password");
        $this->command->info("   User 1: user1@juris.com / password");
        $this->command->info("   User 2: user2@juris.com / password");

        // Check if Content Portal exists, create if not
        $contentPortal = DB::table('portals')->where('key', 'content')->first();
        
        if (!$contentPortal) {
            $contentPortalId = DB::table('portals')->insertGetId([
                'key' => 'content',
                'name' => 'Content Management',
                'description' => 'Manage legal documents and legislation',
                'url' => 'http://localhost:5177',
                'icon' => 'BookOpen',
                'enabled' => true,
                'show_in_user_dashboard' => true,
                'order' => 3,
                'created_at' => $now,
                'updated_at' => $now
            ]);
            $this->command->info('✅ Content Portal created!');
        } else {
            $contentPortalId = $contentPortal->id;
            $this->command->info('✅ Content Portal already exists, using existing portal.');
        }

        // Assign Content Portal to all users with appropriate roles
        DB::table('portal_user')->insert([
            [
                'user_id' => $superAdminId,
                'portal_id' => $contentPortalId,
                'role_scope' => 'portal_admin',
                'created_at' => $now,
                'updated_at' => $now
            ],
            [
                'user_id' => $adminId,
                'portal_id' => $contentPortalId,
                'role_scope' => 'portal_admin',
                'created_at' => $now,
                'updated_at' => $now
            ],
            [
                'user_id' => $user1Id,
                'portal_id' => $contentPortalId,
                'role_scope' => 'user',
                'created_at' => $now,
                'updated_at' => $now
            ],
            [
                'user_id' => $user2Id,
                'portal_id' => $contentPortalId,
                'role_scope' => 'user',
                'created_at' => $now,
                'updated_at' => $now
            ]
        ]);

        $this->command->info('✅ Portal access granted to all users!');
        $this->command->newLine();
        $this->command->info('📊 Total: 4 users created and assigned to Content Management portal');
    }
}
