<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        // Drop existing piece tables
        Schema::dropIfExists('manufacturing_run_pieces');
        Schema::dropIfExists('warehouse_pieces');

        // Create stock_pieces table
        Schema::create('stock_pieces', function (Blueprint $table) {
            $table->id();
            $table->foreignId('manufacturing_run_id')->constrained('manufacturing_runs')->cascadeOnDelete();
            $table->foreignId('account_finished_goods_id')->constrained('accounts')->restrictOnDelete();
            $table->foreignId('warehouse_id')->constrained('warehouses')->restrictOnDelete();
            $table->integer('piece_quantity')->comment('Number of pieces of this type');
            $table->enum('piece_type', ['complete', 'partial'])->default('complete')->comment('Type of piece: complete or partial');
            $table->decimal('length', 15, 4)->comment('Length of each piece in base unit');
            $table->enum('status', ['available', 'reserved', 'sold', 'b_grade'])->default('available')->comment('Piece status');
            $table->enum('grade', ['a_grade', 'b_grade'])->default('a_grade')->comment('Piece grade quality');
            $table->text('notes')->nullable()->comment('Additional notes about the piece');
            $table->timestamps();

            // Indexes
            $table->index(['manufacturing_run_id', 'account_finished_goods_id'], 'sp_run_goods_idx');
            $table->index(['warehouse_id', 'status']);
            $table->index(['warehouse_id', 'grade']);
            $table->index(['account_finished_goods_id', 'status']);
        });

        // Add piece-related columns to stock_ledgers
        Schema::table('stock_ledgers', function (Blueprint $table) {
            $table->decimal('length', 15, 4)->nullable()->after('quantity')->comment('Length of pieces (for manufacturing)');
            $table->integer('piece_quantity')->nullable()->after('length')->comment('Number of pieces (for manufacturing)');
            $table->foreignId('manufacturing_run_id')->nullable()->after('piece_quantity')->constrained('manufacturing_runs')->cascadeOnDelete();
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        // Remove piece-related columns from stock_ledgers
        Schema::table('stock_ledgers', function (Blueprint $table) {
            $table->dropForeign(['manufacturing_run_id']);
            $table->dropColumn(['length', 'piece_quantity', 'manufacturing_run_id']);
        });

        // Drop stock_pieces table
        Schema::dropIfExists('stock_pieces');

        // Recreate original tables (simplified)
        Schema::create('manufacturing_run_pieces', function (Blueprint $table) {
            $table->id();
            $table->foreignId('manufacturing_run_id')->constrained('manufacturing_runs')->cascadeOnDelete();
            $table->integer('piece_number');
            $table->decimal('length', 15, 4);
            $table->enum('status', ['complete', 'partial', 'b_grade'])->default('complete');
            $table->text('notes')->nullable();
            $table->timestamps();
        });

        Schema::create('warehouse_pieces', function (Blueprint $table) {
            $table->id();
            $table->foreignId('warehouse_id')->constrained('warehouses')->cascadeOnDelete();
            $table->foreignId('item_id')->constrained('accounts')->cascadeOnDelete();
            $table->integer('piece_number');
            $table->decimal('length', 15, 4);
            $table->enum('status', ['available', 'reserved', 'sold', 'b_grade'])->default('available');
            $table->enum('grade', ['a_grade', 'b_grade'])->default('a_grade');
            $table->text('notes')->nullable();
            $table->timestamps();
        });
    }
};
