<?php

namespace App\Console\Commands;

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

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

    /**
     * @var string
     */
    protected $description = 'Delete all sales, returns, exchanges, customer orders, and production orders';

    public function handle(): int
    {
        if (! $this->option('force') && ! $this->confirm(
            'This will permanently delete all sales (including returns and exchanges), sale payments, customer orders, production orders, stock movements, and reset finished-goods stock. Purchases, products, and customers are kept. Continue?',
            false,
        )) {
            return self::SUCCESS;
        }

        $result = ClearSalesOrdersProductionData::run();

        $this->components->info(sprintf(
            'Removed %d sale(s), %d customer order(s), and %d production order(s). Reset stock on %d factory product(s).',
            $result['sales'],
            $result['customer_orders'],
            $result['production_orders'],
            $result['factory_products_reset'],
        ));

        $this->components->twoColumnDetail(
            'Also removed',
            sprintf(
                '%d sale line(s), %d sale pack(s), %d sale payment(s), %d order line(s), %d material usage row(s), and %d stock movement(s).',
                $result['sale_items'],
                $result['sale_packs'],
                $result['sale_payments'],
                $result['customer_order_items'],
                $result['production_materials'],
                $result['stock_movements'],
            ),
        );

        return self::SUCCESS;
    }
}
