<?php

namespace App\Console\Commands;

use App\Support\ClearCustomerOrderData;
use Illuminate\Console\Command;

class ClearCustomerOrdersCommand extends Command
{
    /**
     * @var string
     */
    protected $signature = 'customer-orders:clear {--force : Run without confirmation}';

    /**
     * @var string
     */
    protected $description = 'Delete all customer orders, production orders, and material usage (customers and products are kept)';

    public function handle(): int
    {
        if (! $this->option('force') && ! $this->confirm(
            'This will permanently delete all customer orders, production orders, and reset finished-goods stock. Raw material stock will be recalculated from purchases only. Continue?',
            false,
        )) {
            return self::SUCCESS;
        }

        $result = ClearCustomerOrderData::run();

        $this->components->info(sprintf(
            'Removed %d customer order(s), %d order line(s), %d production order(s), %d material usage row(s), and %d production stock movement(s). Reset stock on %d factory product(s).',
            $result['customer_orders'],
            $result['customer_order_items'],
            $result['production_orders'],
            $result['production_materials'],
            $result['production_stock_movements'],
            $result['factory_products_reset'],
        ));

        return self::SUCCESS;
    }
}
