<?php

namespace Database\Seeders;

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

class UserSeeder extends Seeder
{
    public function run(): void
    {
        $users = [
            [
                'name'              => 'Super Admin',
                'email'             => 'admin@megablog.com',
                'password'          => Hash::make('password'),
                'role'              => 'super_admin',
                'bio'               => 'MegaBlog platform administrator.',
                'is_active'         => true,
                'email_verified_at' => now(),
            ],
            [
                'name'              => 'Editor',
                'email'             => 'editor@megablog.com',
                'password'          => Hash::make('password'),
                'role'              => 'editor',
                'bio'               => 'Content editor at MegaBlog.',
                'is_active'         => true,
                'email_verified_at' => now(),
            ],
            [
                'name'              => 'Crawler Bot',
                'email'             => 'crawler@megablog.com',
                'password'          => Hash::make('crawler_secret_2024!'),
                'role'              => 'author',
                'bio'               => 'Automated crawler that fetches and publishes news articles.',
                'is_active'         => true,
                'email_verified_at' => now(),
            ],
            [
                'name'              => 'Sports Writer',
                'email'             => 'sports@megablog.com',
                'password'          => Hash::make('password'),
                'role'              => 'author',
                'bio'               => 'Sports correspondent covering global athletics.',
                'is_active'         => true,
                'email_verified_at' => now(),
            ],
            [
                'name'              => 'Tech Writer',
                'email'             => 'tech@megablog.com',
                'password'          => Hash::make('password'),
                'role'              => 'author',
                'bio'               => 'Technology journalist covering startups and innovation.',
                'is_active'         => true,
                'email_verified_at' => now(),
            ],
        ];

        foreach ($users as $user) {
            DB::table('users')->updateOrInsert(
                ['email' => $user['email']],
                array_merge($user, [
                    'created_at' => now(),
                    'updated_at' => now(),
                ])
            );
        }

        // Create an API key for the crawler bot
        $crawlerUser = DB::table('users')->where('email', 'crawler@megablog.com')->first();
        if ($crawlerUser) {
            DB::table('api_keys')->updateOrInsert(
                ['name' => 'Python Crawler'],
                [
                    'user_id'     => $crawlerUser->id,
                    'name'        => 'Python Crawler',
                    'key'         => hash('sha256', 'megablog-crawler-key-' . env('APP_KEY')),
                    'permissions' => json_encode(['posts:write', 'posts:read', 'sources:read']),
                    'is_active'   => true,
                    'created_at'  => now(),
                    'updated_at'  => now(),
                ]
            );
        }

        $this->command->info('✓ Users seeded (' . count($users) . ')');
        $this->command->info('  admin@megablog.com     → super_admin  / password: password');
        $this->command->info('  editor@megablog.com    → editor       / password: password');
        $this->command->info('  crawler@megablog.com   → author       / API key generated');
        $this->command->warn('  ⚠ Change passwords before going live!');
    }
}
