<?php

namespace App\Console\Commands;

use App\Models\User;
use App\Models\Customer;
use App\Models\Order;
use App\Models\OrderItem;
use App\Models\OrderStatusHistory;
use App\Models\SmsLog;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Hash;

class CleanDatabaseCommand extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'db:clean {--force : Force the operation without confirmation}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Clean database and create default admin user for production deployment';

    /**
     * Execute the console command.
     */
    public function handle()
    {
        if (!$this->option('force')) {
            if (!$this->confirm('This will delete ALL demo data (orders, customers, users, SMS logs). Are you sure?')) {
                $this->info('Operation cancelled.');
                return 0;
            }
        }

        $this->info('🧹 Cleaning database for production deployment...');

        // Delete all demo data
        $this->info('Deleting SMS logs...');
        SmsLog::truncate();

        $this->info('Deleting order status history...');
        OrderStatusHistory::truncate();

        $this->info('Deleting order items...');
        OrderItem::truncate();

        $this->info('Deleting orders...');
        Order::truncate();

        $this->info('Deleting customers...');
        Customer::truncate();

        $this->info('Deleting all users...');
        User::truncate();

        // Create default admin user
        $this->info('Creating default admin user...');
        $admin = User::create([
            'name' => 'Slarry Laundry Admin',
            'email' => 'Ayodejiademide@gmail.com',
            'password' => Hash::make('oluwamuyiwa'),
            'role' => 'admin',
            'phone' => null,
            'is_active' => true,
            'email_verified_at' => now(),
        ]);

        $this->info('✅ Database cleaned successfully!');
        $this->info('✅ Default admin user created:');
        $this->info("   Email: {$admin->email}");
        $this->info("   Password: oluwamuyiwa");
        $this->info("   Role: {$admin->role}");

        $this->warn('⚠️  Remember to change the default password after first login!');

        return 0;
    }
}
