<?php

namespace App\Console\Commands;

use App\Models\Role;
use App\Models\Setting;
use App\Models\User;
use App\Services\MemberCodeService;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Hash;
use Throwable;

class CreateDemoAdminCommand extends Command
{
    protected $signature = 'app:create-demo-admin
        {--name=Demo Admin : Admin display name}
        {--email=admin@demo.local : Admin email}
        {--mobile=9999999999 : Admin mobile}
        {--password=Admin@12345 : Admin password}
        {--slug=admin : Admin login slug}';

    protected $description = 'Create or update a demo super-admin account';

    public function handle(MemberCodeService $memberCodeService): int
    {
        try {
            $name = (string) $this->option('name');
            $email = (string) $this->option('email');
            $mobile = (string) $this->option('mobile');
            $password = (string) $this->option('password');
            $slug = (string) $this->option('slug');

            if ($name === '' || $email === '' || $mobile === '' || $password === '') {
                $this->error('Name, email, mobile, and password are required.');

                return self::INVALID;
            }

            $user = User::query()->where('email', $email)->first();
            if (! $user) {
                $user = User::query()->create([
                    'member_code' => $memberCodeService->generateMemberCode(),
                    'referral_code' => $memberCodeService->generateReferralCode(),
                    'name' => $name,
                    'email' => $email,
                    'mobile' => $mobile,
                    'password' => Hash::make($password),
                    'role' => 'admin',
                    'status' => 'active',
                    'joined_at' => now(),
                    'email_verified_at' => now(),
                ]);
                $this->info('Demo admin created.');
            } else {
                $user->fill([
                    'name' => $name,
                    'mobile' => $mobile,
                    'password' => Hash::make($password),
                    'role' => 'admin',
                    'status' => 'active',
                ])->save();
                $this->info('Existing admin updated.');
            }

            $superAdminRole = Role::query()->where('code', 'super_admin')->first();
            if ($superAdminRole) {
                $user->roles()->syncWithoutDetaching([$superAdminRole->id]);
            }

            if ($slug !== '') {
                Setting::setValue('admin_slug', $slug);
            }

            $this->line('---------------------------------------------');
            $this->line('Demo Admin Login');
            $this->line('URL: '.url('/'.setting('admin_slug', 'admin')));
            $this->line('Email: '.$email);
            $this->line('Password: '.$password);
            $this->line('---------------------------------------------');

            return self::SUCCESS;
        } catch (Throwable $exception) {
            $this->error('Could not create demo admin: '.$exception->getMessage());
            $this->line('Check DB connection in .env and ensure MySQL is running, then run this command again.');

            return self::FAILURE;
        }
    }
}
