<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use App\Models\MenuItem;
use App\Models\Product;

class SyncMenuItemsToProducts extends Command
{
    protected $signature = 'sync:menu-items-to-products';
    protected $description = 'Sync all existing menu items to manufacturing products';

    public function handle()
    {
        $menuItems = MenuItem::all();
        $count = 0;

        foreach ($menuItems as $menuItem) {
            $product = Product::updateOrCreate(
                ['menu_item_id' => $menuItem->id],
                [
                    'name' => $menuItem->name,
                    'description' => $menuItem->description,
                    'type' => 'finished',
                    'price' => $menuItem->price,
                    'cost' => $menuItem->cost_price ?? 0,
                    'is_active' => $menuItem->is_available,
                    'sku' => 'MENU-' . str_pad($menuItem->id, 6, '0', STR_PAD_LEFT),
                    'unit_of_measure' => 'pcs',
                    'stock_quantity' => 0,
                    'min_stock_level' => 0,
                ]
            );
            
            if ($product) {
                $count++;
                $this->info("Synced: {$menuItem->name} -> Product ID: {$product->id}");
            }
        }

        $this->info("Successfully synced {$count} menu items to products.");
        return 0;
    }
}